diff --git a/config.json b/config.json index 16e0fcf..0217ec7 100644 --- a/config.json +++ b/config.json @@ -16,10 +16,10 @@ "UntappdToken": "", "UntappdFreq": 3600, - "TwitterConsumerKey": "", - "TwitterConsumerSecret": "", - "TwitterUserKey": "", - "TwitterUserSecret": "", + "TwitterConsumerKey": "9c2EpWSOv4NIb413Sl6ipg", + "TwitterConsumerSecret": "JmAAAXFcYr6nUM5Jhq3d5oTSzZUmgtJwwUfptob2WsU", + "TwitterUserKey": "644233-KsPtbllqrx8ziCptfyEUxggVqguwKkpPeg2Nis3zXY", + "TwitterUserSecret": "rPiQC7fs9L5j9biMhJD41O7avJitW2z1TjpmoOZc", "WelcomeMsgs": [ "Real men use screen, %s.", diff --git a/config/config.go b/config/config.go index 7fe3884..6a45c86 100644 --- a/config/config.go +++ b/config/config.go @@ -7,21 +7,25 @@ import "io/ioutil" // Config stores any system-wide startup information that cannot be easily configured via // the database type Config struct { - DbName string - DbServer string - Channels []string - MainChannel string - Plugins []string - Nick, Server, Pass string - Version string - CommandChar string - QuoteChance float64 - QuoteTime int - LogLength int - Admins []string - UntappdToken string - UntappdFreq int - WelcomeMsgs []string + DbName string + DbServer string + Channels []string + MainChannel string + Plugins []string + Nick, Server, Pass string + Version string + CommandChar string + QuoteChance float64 + QuoteTime int + LogLength int + Admins []string + UntappdToken string + UntappdFreq int + WelcomeMsgs []string + TwitterConsumerKey string + TwitterConsumerSecret string + TwitterUserKey string + TwitterUserSecret string } // Readconfig loads the config data out of a JSON file located in cfile diff --git a/plugins/twitter.go b/plugins/twitter.go new file mode 100644 index 0000000..8858a39 --- /dev/null +++ b/plugins/twitter.go @@ -0,0 +1,131 @@ +package plugins + +import ( + "bitbucket.org/phlyingpenguin/godeepintir/bot" + "github.com/astrata/twitter" + "github.com/garyburd/go-oauth/oauth" + "labix.org/v2/mgo" + "labix.org/v2/mgo/bson" + "log" + "net/url" + "strings" + "time" +) + +// Plugin to squak tweets at the channel. + +type TwitterPlugin struct { + Bot *bot.Bot + Coll *mgo.Collection + Client *twitter.Client +} + +type twitterUser struct { + Id bson.ObjectId `bson:"_id,omitempty"` + User string + Tweets []string +} + +// NewTwitterPlugin creates a new TwitterPlugin with the Plugin interface +func NewTwitterPlugin(bot *bot.Bot) *TwitterPlugin { + return &TwitterPlugin{ + Bot: bot, + Coll: bot.Db.C("twitter"), + } +} + +func (p *TwitterPlugin) say(message bot.Message, body string) bool { + p.Bot.SendMessage(message.Channel, body) + return true +} + +// Check for commands accepted +// follow, unfollow +func (p *TwitterPlugin) checkCommand(message bot.Message) bool { + parts := strings.Split(message.Body, " ") + if parts[0] == "follow" { + if len(parts) != 2 { + return p.say(message, "I don't get it.") + } + + user := parts[1] + res := p.Coll.Find(bson.M{"user": user}) + + if count, _ := res.Count(); count > 0 { + return p.say(message, "I'm already following "+user+"!") + } + + p.Coll.Insert(twitterUser{User: user}) + } else if parts[0] == "unfollow" { + if len(parts) != 2 { + return p.say(message, "I don't get it.") + } + + user := parts[1] + + p.Coll.Remove(bson.M{"user": user}) + return p.say(message, "Fuck "+user) + } + + return false +} + +// Message responds to the bot hook on recieving messages. +// This function returns true if the plugin responds in a meaningful way to the users message. +// Otherwise, the function returns false and the bot continues execution of other plugins. +func (p *TwitterPlugin) Message(message bot.Message) bool { + + if message.Command { + return p.checkCommand(message) + } + + return false +} + +// LoadData imports any configuration data into the plugin. This is not strictly necessary other +// than the fact that the Plugin interface demands it exist. This may be deprecated at a later +// date. +func (p *TwitterPlugin) LoadData() { + // This bot has no data to load + p.Client = twitter.New(&oauth.Credentials{ + p.Bot.Config.TwitterConsumerKey, + p.Bot.Config.TwitterConsumerSecret, + }) + + p.Client.SetAuth(&oauth.Credentials{ + p.Bot.Config.TwitterUserKey, + p.Bot.Config.TwitterUserSecret, + }) + + _, err := p.Client.VerifyCredentials(nil) + + if err != nil { + log.Println("Could not auth with twitter:", err) + } else { + go p.checkMessages() + } +} + +// Help responds to help requests. Every plugin must implement a help function. +func (p *TwitterPlugin) Help(channel string, parts []string) { + p.Bot.SendMessage(channel, "Tell me to follow or unfollow twitter users!") +} + +// Empty event handler because this plugin does not do anything on event recv +func (p *TwitterPlugin) Event(kind string, message bot.Message) bool { + return false +} + +func (p *TwitterPlugin) checkMessages() { + for { + time.Sleep(time.Minute * 30) + var u url.Values + u["screen_name"] = []string{"phlyingpenguin"} + data, err := p.Client.UserTimeline(u) + if err != nil { + log.Println(err) + } else { + log.Println(data) + } + } +}