add twitter
This commit is contained in:
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