Skip to content

Commit 5baf295

Browse files
Fix tweet.coffee for v1.1 API
After the API retirement, this stopped working. This updates it to use the ntwitter package, and follows the same configuration conventions as tweet-mention.coffee for re-use. It does depend on a fork of ntwitter though, and will need to be so until AvianFlu/ntwitter#110 is fixed
1 parent a5d225c commit 5baf295

File tree

1 file changed

+53
-12
lines changed

1 file changed

+53
-12
lines changed

src/scripts/tweet.coffee

+53-12
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,68 @@
22
# Display a random tweet from twitter about a subject
33
#
44
# Dependencies:
5-
# None
5+
# "ntwitter" : "https://github.com/sebhildebrandt/ntwitter/tarball/master",
66
#
77
# Configuration:
8-
# None
8+
# HUBOT_TWITTER_CONSUMER_KEY
9+
# HUBOT_TWITTER_CONSUMER_SECRET
10+
# HUBOT_TWITTER_ACCESS_TOKEN_KEY
11+
# HUBOT_TWITTER_ACCESS_TOKEN_SECRET
912
#
1013
# Commands:
1114
# hubot <keyword> tweet - Returns a link to a tweet about <keyword>
1215
#
16+
# Notes:
17+
# There's an outstanding issue on AvianFlu/ntwitter#110 for search and the v1.1 API.
18+
# sebhildebrandt is a fork that is working, so we recommend that for now. This
19+
# can be removed after the issue is fixed and a new release cut, along with updating the dependency
20+
#
1321
# Author:
14-
# atmos
22+
# atmos, technicalpickles
23+
24+
ntwitter = require 'ntwitter'
25+
inspect = require('util').inspect
1526

1627
module.exports = (robot) ->
28+
auth =
29+
consumer_key: process.env.HUBOT_TWITTER_CONSUMER_KEY
30+
consumer_secret: process.env.HUBOT_TWITTER_CONSUMER_SECRET
31+
access_token_key: process.env.HUBOT_TWITTER_ACCESS_TOKEN_KEY
32+
access_token_secret: process.env.HUBOT_TWITTER_ACCESS_TOKEN_SECRET
33+
34+
twit = undefined
35+
1736
robot.respond /(.*) tweet/i, (msg) ->
18-
search = escape(msg.match[1])
19-
msg.http('http://search.twitter.com/search.json')
20-
.query(q: search)
21-
.get() (err, res, body) ->
22-
tweets = JSON.parse(body)
23-
24-
if tweets.results? and tweets.results.length > 0
25-
tweet = msg.random tweets.results
26-
msg.send "http://twitter.com/#!/#{tweet.from_user}/status/#{tweet.id_str}"
37+
unless auth.consumer_key
38+
msg.send "Please set the HUBOT_TWITTER_CONSUMER_KEY environment variable."
39+
return
40+
unless auth.consumer_secret
41+
msg.send "Please set the HUBOT_TWITTER_CONSUMER_SECRET environment variable."
42+
return
43+
unless auth.access_token_key
44+
msg.send "Please set the HUBOT_TWITTER_ACCESS_TOKEN_KEY environment variable."
45+
return
46+
unless auth.access_token_secret
47+
msg.send "Please set the HUBOT_TWITTER_ACCESS_TOKEN_SECRET environment variable."
48+
return
49+
50+
51+
twit ?= new ntwitter auth
52+
53+
54+
twit.verifyCredentials (err, data) ->
55+
if err
56+
msg.send "Encountered a problem verifing twitter credentials:(", inspect err
57+
return
58+
59+
q = escape(msg.match[1])
60+
twit.search q, (err, data) ->
61+
if err
62+
msg.send "Encountered a problem twitter searching :(", inspect err
63+
return
64+
65+
if data.statuses? and data.statuses.length > 0
66+
status = msg.random data.statuses
67+
msg.send "http://twitter.com/#!/#{status.user.screen_name}/status/#{status.id_str}"
2768
else
2869
msg.reply "No one is tweeting about that."

0 commit comments

Comments
 (0)