Files
tweetdistributor/output/twitter.go
2025-03-16 19:24:56 +09:00

56 lines
1.2 KiB
Go

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
}
func (to *twitteroutput) GetName() string {
return "twitter"
}