add twitter

This commit is contained in:
2025-03-15 19:07:16 +09:00
parent aea31da9c0
commit ac532ddcb5
4 changed files with 63 additions and 4 deletions

7
go.mod
View File

@@ -5,7 +5,8 @@ go 1.24.1
require github.com/bwmarrin/discordgo v0.28.1 require github.com/bwmarrin/discordgo v0.28.1
require ( require (
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.5.3 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect github.com/michimani/gotwi v0.17.0
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect golang.org/x/crypto v0.36.0 // indirect
golang.org/x/sys v0.31.0 // indirect
) )

View File

@@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"tweetdistributor/discord" "tweetdistributor/discord"
"tweetdistributor/output"
) )
func main() { func main() {
@@ -13,9 +14,10 @@ func main() {
tweetchannel := make(chan string, 1) tweetchannel := make(chan string, 1)
d.BeginRead(tweetchannel) d.BeginRead(tweetchannel)
d.Write("Hello, World!") output := output.TwitterOutput(os.Getenv("TW_ACCESS_TOKEN"), os.Getenv("TW_ACCESS_SECRET"))
for tweet := range tweetchannel { for tweet := range tweetchannel {
fmt.Println(tweet) fmt.Println(tweet)
output.Write(tweet)
} }
} }

5
output/output.go Normal file
View File

@@ -0,0 +1,5 @@
package output
type OutputInterface interface {
Write(string) error
}

51
output/twitter.go Normal file
View File

@@ -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
}