Adding twitter plugin (non-functional)

This commit is contained in:
Chris Sexton 2013-02-20 22:24:51 -05:00
parent 2910277286
commit 0bc60518ee
3 changed files with 154 additions and 19 deletions

View File

@ -16,10 +16,10 @@
"UntappdToken": "<Your Token>", "UntappdToken": "<Your Token>",
"UntappdFreq": 3600, "UntappdFreq": 3600,
"TwitterConsumerKey": "<Your Consumer Key>", "TwitterConsumerKey": "9c2EpWSOv4NIb413Sl6ipg",
"TwitterConsumerSecret": "<Your Consumer Secret>", "TwitterConsumerSecret": "JmAAAXFcYr6nUM5Jhq3d5oTSzZUmgtJwwUfptob2WsU",
"TwitterUserKey": "<Your User Key>", "TwitterUserKey": "644233-KsPtbllqrx8ziCptfyEUxggVqguwKkpPeg2Nis3zXY",
"TwitterUserSecret": "<Your User Secret>", "TwitterUserSecret": "rPiQC7fs9L5j9biMhJD41O7avJitW2z1TjpmoOZc",
"WelcomeMsgs": [ "WelcomeMsgs": [
"Real men use screen, %s.", "Real men use screen, %s.",

View File

@ -22,6 +22,10 @@ type Config struct {
UntappdToken string UntappdToken string
UntappdFreq int UntappdFreq int
WelcomeMsgs []string WelcomeMsgs []string
TwitterConsumerKey string
TwitterConsumerSecret string
TwitterUserKey string
TwitterUserSecret string
} }
// Readconfig loads the config data out of a JSON file located in cfile // Readconfig loads the config data out of a JSON file located in cfile

131
plugins/twitter.go Normal file
View File

@ -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)
}
}
}