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

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
}