diff --git a/go.mod b/go.mod index 8e64493..499e458 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,8 @@ go 1.24.1 require github.com/bwmarrin/discordgo v0.28.1 require ( - github.com/gorilla/websocket v1.4.2 // indirect - golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect - golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect + github.com/gorilla/websocket v1.5.3 // indirect + github.com/michimani/gotwi v0.17.0 + golang.org/x/crypto v0.36.0 // indirect + golang.org/x/sys v0.31.0 // indirect ) diff --git a/main.go b/main.go index 503cc1b..f28efea 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "tweetdistributor/discord" + "tweetdistributor/output" ) func main() { @@ -13,9 +14,10 @@ func main() { tweetchannel := make(chan string, 1) d.BeginRead(tweetchannel) - d.Write("Hello, World!") + output := output.TwitterOutput(os.Getenv("TW_ACCESS_TOKEN"), os.Getenv("TW_ACCESS_SECRET")) for tweet := range tweetchannel { fmt.Println(tweet) + output.Write(tweet) } } diff --git a/output/output.go b/output/output.go new file mode 100644 index 0000000..f81c8ac --- /dev/null +++ b/output/output.go @@ -0,0 +1,5 @@ +package output + +type OutputInterface interface { + Write(string) error +} diff --git a/output/twitter.go b/output/twitter.go new file mode 100644 index 0000000..b35f260 --- /dev/null +++ b/output/twitter.go @@ -0,0 +1,51 @@ +package output + +import ( + "context" + "fmt" + "os" + + "github.com/michimani/gotwi" + "github.com/michimani/gotwi/tweet/managetweet" + "github.com/michimani/gotwi/tweet/managetweet/types" +) + +type twitteroutput struct { + accessToken string + accessSecret string + client *gotwi.Client +} + +func TwitterOutput(accessToken string, accessSecret string) *twitteroutput { + twitteroutput := &twitteroutput{ + accessToken: accessToken, + accessSecret: accessSecret, + } + errOAuth := twitteroutput.newOAuth1Client(accessToken, accessSecret) + if errOAuth != nil { + fmt.Fprintln(os.Stderr, errOAuth) + os.Exit(1) + } + return twitteroutput +} + +func (to *twitteroutput) newOAuth1Client(accessToken, accessSecret string) error { + in := &gotwi.NewClientInput{ + AuthenticationMethod: gotwi.AuthenMethodOAuth1UserContext, + OAuthToken: accessToken, + OAuthTokenSecret: accessSecret, + } + + client, err := gotwi.NewClient(in) + to.client = client + return err +} + +func (to *twitteroutput) Write(str string) error { + p := &types.CreateInput{ + Text: gotwi.String(str), + } + + _, err := managetweet.Create(context.Background(), to.client, p) + return err +}