twitterDM-to-matrix/twitterConfigClient.go
twitterConfigClient.goRaw
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7
8 "github.com/dghubble/go-twitter/twitter"
9 "github.com/dghubble/oauth1"
10)
11
12//TwitterConfig stores the data from json twitterConfig.json file
13type TwitterConfig struct {
14 ScreenName string `json:"screenName"`
15 ConsumerKey string `json:"consumer_key"`
16 ConsumerSecret string `json:"consumer_secret"`
17 AccessToken string `json:"access_token"`
18 AccessTokenSecret string `json:"access_token_secret"`
19}
20
21var twitterConfig TwitterConfig
22
23func readTwitterConfigTokensAndConnect() (client *twitter.Client) {
24
25 file, e := ioutil.ReadFile("twitterConfig.json")
26 if e != nil {
27 fmt.Println("error:", e)
28 }
29 content := string(file)
30 json.Unmarshal([]byte(content), &twitterConfig)
31 fmt.Println("twitterConfig.json read comlete")
32
33 fmt.Print("connecting to twitter api --> ")
34 configu := oauth1.NewConfig(twitterConfig.ConsumerKey, twitterConfig.ConsumerSecret)
35 token := oauth1.NewToken(twitterConfig.AccessToken, twitterConfig.AccessTokenSecret)
36 httpClient := configu.Client(oauth1.NoContext, token)
37 // twitter client
38 client = twitter.NewClient(httpClient)
39
40 fmt.Println("connection successful")
41
42 return client
43}