-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.go
46 lines (40 loc) · 890 Bytes
/
twitter.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
package main
import (
"encoding/base64"
"io/ioutil"
"net/url"
"os"
"strconv"
"github.com/ChimeraCoder/anaconda"
"github.com/joho/godotenv"
)
type TwitterClient struct {
API *anaconda.TwitterApi
}
func createTwitterClient() *TwitterClient {
godotenv.Load()
return &TwitterClient{
API: anaconda.NewTwitterApiWithCredentials(
os.Getenv("ACCESS_TOKEN"),
os.Getenv("ACCESS_TOKEN_SECRET"),
os.Getenv("API_KEY"),
os.Getenv("API_SECRET_KEY")),
}
}
func (t *TwitterClient) PostImage(imagePath string, text string) (err error) {
data, err := ioutil.ReadFile(imagePath)
if err != nil {
return
}
mediaResponse, err := t.API.UploadMedia(base64.StdEncoding.EncodeToString(data))
if err != nil {
return
}
v := url.Values{}
v.Set("media_ids", strconv.FormatInt(mediaResponse.MediaID, 10))
_, err = t.API.PostTweet(text, v)
if err != nil {
return
}
return
}