-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (54 loc) · 1.59 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
// Source code based on the example:
// https://github.com/michimani/gotwi/blob/main/_examples/2_post_delete_tweet/main.go
import (
"context"
"fmt"
"os"
"github.com/michimani/gotwi"
"github.com/michimani/gotwi/tweet/managetweet"
"github.com/michimani/gotwi/tweet/managetweet/types"
)
func main() {
// Check expected secrets are set in the environment variables
accessToken := os.Getenv("TW_ACCESS_TOKEN")
accessSecret := os.Getenv("TW_ACCESS_SECRET")
if accessToken == "" || accessSecret == "" {
fmt.Fprintln(os.Stderr, "Please set the TW_ACCESS_TOKEN and TW_ACCESS_SECRET environment variables.")
os.Exit(1)
}
// Check the comment to tweet has been supplied
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Please pass the comment to tweet as the only argument.")
os.Exit(1)
}
client, err := newOAuth1Client(accessToken, accessSecret)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
tweetId, err := tweet(client, os.Args[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
fmt.Println("tweet id", tweetId)
}
func newOAuth1Client(accessToken, accessSecret string) (*gotwi.Client, error) {
in := &gotwi.NewClientInput{
AuthenticationMethod: gotwi.AuthenMethodOAuth1UserContext,
OAuthToken: accessToken,
OAuthTokenSecret: accessSecret,
}
return gotwi.NewClient(in)
}
func tweet(c *gotwi.Client, text string) (string, error) {
p := &types.CreateInput{
Text: gotwi.String(text),
}
res, err := managetweet.Create(context.Background(), c, p)
if err != nil {
return "", err
}
return gotwi.StringValue(res.Data.ID), nil
}