add twitter
This commit is contained in:
7
go.mod
7
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
|
||||
)
|
||||
|
||||
4
main.go
4
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)
|
||||
}
|
||||
}
|
||||
|
||||
5
output/output.go
Normal file
5
output/output.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package output
|
||||
|
||||
type OutputInterface interface {
|
||||
Write(string) error
|
||||
}
|
||||
51
output/twitter.go
Normal file
51
output/twitter.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user