-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_client.go
85 lines (72 loc) · 2.07 KB
/
tweet_client.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"bufio"
"log"
"os"
"unicode/utf8"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
type Credentials struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
}
func main() {
creds := Credentials{
AccessToken: os.Getenv("TWITTER_ACCESS_TOKEN"),
AccessTokenSecret: os.Getenv("TWITTER_ACCESS_TOKEN_SECRET"),
ConsumerKey: os.Getenv("TWITTER_CONSUMER_KEY"),
ConsumerSecret: os.Getenv("TWITTER_CONSUMER_KEY_SECRET"),
}
client, err := getClient(&creds)
if err != nil {
log.Println("Error getting Twitter Client")
log.Println(err)
os.Exit(-1)
}
// Say something
println("What's up?")
tweetContent := getUserInput()
// Send a Tweet
_, _, err = client.Statuses.Update(tweetContent, nil)
if err != nil {
log.Println("Error getting Twitter Client")
log.Println(err)
os.Exit(-1)
} else {
println("->Woosh")
}
}
func getUserInput() string {
// Note hard coded input buffer in the kernel of 1024 bytes. If your input goes beyond program is unresponsive.
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
userInput := scanner.Text()
if utf8.RuneCountInString(userInput) > 256 {
println("Over char limit")
getUserInput()
}
return userInput
}
func getClient(creds *Credentials) (*twitter.Client, error) {
// Pass in your consumer key (API Key) and your Consumer Secret (API Secret)
config := oauth1.NewConfig(creds.ConsumerKey, creds.ConsumerSecret)
// Pass in your Access Token and your Access Token Secret
token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
// Verify Credentials
verifyParams := &twitter.AccountVerifyParams{
SkipStatus: twitter.Bool(true),
IncludeEmail: twitter.Bool(true),
}
// we can retrieve the user and verify if the credentials
// we have used successfully allow us to log in!
_, _, err := client.Accounts.VerifyCredentials(verifyParams)
if err != nil {
return nil, err
}
return client, nil
}