arnaucube
arnaucube/echo-botnetPublic Self-hosted repository
echo-botnet/streamTweets.go
streamTweets.goRaw
  1package main
  2
  3import (
  4	"fmt"
  5	"log"
  6	"math/rand"
  7	"strconv"
  8	"strings"
  9	"time"
 10
 11	"github.com/dghubble/go-twitter/twitter"
 12)
 13
 14func isRT(tweet *twitter.Tweet) bool {
 15	tweetWords := strings.Split(tweet.Text, " ")
 16	for i := 0; i < len(tweetWords); i++ {
 17		if tweetWords[i] == "RT" {
 18			return true
 19		}
 20	}
 21	return false
 22}
 23func isFromOwnBot(flock Botnet, tweet *twitter.Tweet) bool {
 24	for i := 0; i < len(flock.ScreenNames); i++ {
 25		if flock.ScreenNames[i] == tweet.User.ScreenName {
 26			return true
 27		}
 28	}
 29	return false
 30}
 31
 32func getRandomReplyFromReplies(replies []string) string {
 33	rand.Seed(time.Now().UnixNano())
 34	random := rand.Intn(len(replies))
 35	return replies[random]
 36}
 37
 38func replyTweet(client *twitter.Client, text string, inReplyToStatusID int64) {
 39	tweet, httpResp, err := client.Statuses.Update(text, &twitter.StatusUpdateParams{
 40		InReplyToStatusID: inReplyToStatusID,
 41	})
 42	if err != nil {
 43		fmt.Println(err)
 44	}
 45	if httpResp.Status != "200 OK" {
 46		c.Red("error: " + httpResp.Status)
 47		c.Purple("maybe twitter has blocked the account, CTRL+C, wait 15 minutes and try again")
 48	}
 49	fmt.Print("tweet posted: ")
 50	c.Green(tweet.Text)
 51}
 52
 53func startStreaming(botnet Botnet, bot *twitter.Client, botScreenName string, keywords []string, replies []string) {
 54	// Convenience Demux demultiplexed stream messages
 55	demux := twitter.NewSwitchDemux()
 56	demux.Tweet = func(tweet *twitter.Tweet) {
 57		if isRT(tweet) == false && isFromOwnBot(botnet, tweet) == false {
 58			//processTweet(botnetUser, botScreenName, keywords, tweet)
 59			fmt.Println("[bot @" + botScreenName + "] - New tweet detected:")
 60			c.Yellow(tweet.Text)
 61			reply := getRandomReplyFromReplies(replies)
 62			fmt.Print("reply: ")
 63			c.Green(reply)
 64			fmt.Println(tweet.User.ScreenName)
 65			fmt.Println(tweet.ID)
 66			replyTweet(bot, "@"+tweet.User.ScreenName+" "+reply, tweet.ID)
 67			waitMinutes(1)
 68		}
 69	}
 70	demux.DM = func(dm *twitter.DirectMessage) {
 71		fmt.Println(dm.SenderID)
 72	}
 73	demux.Event = func(event *twitter.Event) {
 74		fmt.Printf("%#v\n", event)
 75	}
 76
 77	fmt.Println("Starting Stream...")
 78	// FILTER
 79	filterParams := &twitter.StreamFilterParams{
 80		Track:         keywords,
 81		StallWarnings: twitter.Bool(true),
 82	}
 83	stream, err := bot.Streams.Filter(filterParams)
 84	if err != nil {
 85		log.Fatal(err)
 86	}
 87	// Receive messages until stopped or stream quits
 88	demux.HandleChan(stream.Messages)
 89
 90	// Wait for SIGINT and SIGTERM (HIT CTRL-C)
 91	/*ch := make(chan os.Signal)
 92	signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
 93	log.Println(<-ch)
 94
 95	fmt.Println("Stopping Stream...")
 96	stream.Stop()*/
 97}
 98func streamTweets(botnet Botnet, keywords []string, replies []string) {
 99	fmt.Println("total keywords: " + strconv.Itoa(len(keywords)))
100	c.Purple("keywords to follow: ")
101	fmt.Println(keywords)
102	c.Green("Starting to stream tweets")
103	for i := 0; i < len(botnet.Clients); i++ {
104		go startStreaming(botnet, botnet.Clients[i], botnet.ScreenNames[i], keywords, replies)
105		//wait 35 seconds to start the next bot
106		waitSeconds(35)
107	}
108}