echo-botnet/readConfigTokensAndConnect.go
readConfigTokensAndConnect.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//Config stores the data from json botsConfig.json file
13type Config struct {
14 Title string `json:"title"`
15 ConsumerKey string `json:"consumer_key"`
16 ConsumerSecret string `json:"consumer_secret"`
17 AccessTokenKey string `json:"access_token_key"`
18 AccessTokenSecret string `json:"access_token_secret"`
19}
20
21//Botnet stores each bot configured
22type Botnet struct {
23 ScreenNames []string
24 Clients []*twitter.Client
25}
26
27func readConfigTokensAndConnect() (botnet Botnet) {
28 var config []Config
29 var clients []*twitter.Client
30
31 file, e := ioutil.ReadFile("botsConfig.json")
32 if e != nil {
33 fmt.Println("error:", e)
34 }
35 content := string(file)
36 json.Unmarshal([]byte(content), &config)
37 fmt.Println("botnetConfig.json read comlete")
38
39 fmt.Print("connecting to twitter api --> ")
40 for i := 0; i < len(config); i++ {
41 configu := oauth1.NewConfig(config[i].ConsumerKey, config[i].ConsumerSecret)
42 token := oauth1.NewToken(config[i].AccessTokenKey, config[i].AccessTokenSecret)
43 httpClient := configu.Client(oauth1.NoContext, token)
44 // twitter client
45 client := twitter.NewClient(httpClient)
46 clients = append(clients, client)
47 botnet.ScreenNames = append(botnet.ScreenNames, config[i].Title)
48 }
49 botnet.Clients = clients
50
51 fmt.Println("connection successful")
52
53 return botnet
54}