You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4 KiB

7 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "github.com/dghubble/go-twitter/twitter"
  7. "github.com/dghubble/oauth1"
  8. )
  9. //Config stores the data from json botsConfig.json file
  10. type Config struct {
  11. Title string `json:"title"`
  12. Consumer_key string `json:"consumer_key"`
  13. Consumer_secret string `json:"consumer_secret"`
  14. Access_token_key string `json:"access_token_key"`
  15. Access_token_secret string `json:"access_token_secret"`
  16. }
  17. //Botnet stores each bot configured
  18. type Botnet struct {
  19. ScreenNames []string
  20. Clients []*twitter.Client
  21. }
  22. func readConfigTokensAndConnect() (botnet Botnet) {
  23. var config []Config
  24. var clients []*twitter.Client
  25. file, e := ioutil.ReadFile("botsConfig.json")
  26. if e != nil {
  27. fmt.Println("error:", e)
  28. }
  29. content := string(file)
  30. json.Unmarshal([]byte(content), &config)
  31. fmt.Println("botnetConfig.json read comlete")
  32. fmt.Print("connecting to twitter api --> ")
  33. for i := 0; i < len(config); i++ {
  34. configu := oauth1.NewConfig(config[i].Consumer_key, config[i].Consumer_secret)
  35. token := oauth1.NewToken(config[i].Access_token_key, config[i].Access_token_secret)
  36. httpClient := configu.Client(oauth1.NoContext, token)
  37. // twitter client
  38. client := twitter.NewClient(httpClient)
  39. clients = append(clients, client)
  40. botnet.ScreenNames = append(botnet.ScreenNames, config[i].Title)
  41. }
  42. botnet.Clients = clients
  43. fmt.Println("connection successfull")
  44. return botnet
  45. }