Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit aaacf9c

Browse files
author
Tom Bell
committed
Merge pull request #983 from caselas/update_twitter
Updated twitter.coffee for v1.1
2 parents b723c06 + 981612b commit aaacf9c

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

src/scripts/twitter.coffee

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,59 @@
22
# gets tweet from user
33
#
44
# Dependencies:
5-
# None
5+
# "twit": "1.1.6"
6+
# "underscore": "1.4.4"
67
#
78
# Configuration:
8-
# None
9+
# HUBOT_TWITTER_CONSUMER_KEY
10+
# HUBOT_TWITTER_CONSUMER_SECRET
11+
# HUBOT_TWITTER_ACCESS_TOKEN
12+
# HUBOT_TWITTER_ACCESS_TOKEN_SECRET
913
#
1014
# Commands:
1115
# hubot twitter <twitter username> - Show last tweet from <twitter username>
16+
# hubot twitter <twitter username> <n> - Cycle through tweet with <n> starting w/ latest
1217
#
1318
# Author:
1419
# KevinTraver
1520
#
21+
22+
_ = require "underscore"
23+
Twit = require "twit"
24+
config =
25+
consumer_key: process.env.HUBOT_TWITTER_CONSUMER_KEY
26+
consumer_secret: process.env.HUBOT_TWITTER_CONSUMER_SECRET
27+
access_token: process.env.HUBOT_TWITTER_ACCESS_TOKEN
28+
access_token_secret: process.env.HUBOT_TWITTER_ACCESS_TOKEN_SECRET
29+
1630
module.exports = (robot) ->
17-
robot.respond /(twitter|lasttweet) (.+)$/i, (msg) ->
18-
username = msg.match[2]
19-
msg.http("http://api.twitter.com/1/statuses/user_timeline/#{escape(username)}.json?count=1&include_rts=true")
20-
.get() (err, res, body) ->
21-
response = JSON.parse body
22-
if response[0]
23-
msg.send response[0]["text"]
24-
else
25-
msg.send "Error"
31+
twit = undefined
32+
33+
robot.respond /(twitter|lasttweet)\s+(\S+)\s?(\d?)/i, (msg) ->
34+
unless config.consumer_key
35+
msg.send "Please set the HUBOT_TWITTER_STREAM_CONSUMER_KEY environment variable."
36+
return
37+
unless config.consumer_secret
38+
msg.send "Please set the HUBOT_TWITTER_STREAM_CONSUMER_SECRET environment variable."
39+
return
40+
unless config.access_token
41+
msg.send "Please set the HUBOT_TWITTER_STREAM_ACCESS_TOKEN environment variable."
42+
return
43+
unless config.access_token_secret
44+
msg.send "Please set the HUBOT_TWITTER_ACCESS_TOKEN_SECRET environment variable."
45+
return
46+
47+
unless twit
48+
twit = new Twit config
49+
50+
username = msg.match[2]
51+
if msg.match[3] then count = msg.match[3] else count = 1
52+
53+
twit.get "statuses/user_timeline",
54+
screen_name: escape(username)
55+
count: count
56+
include_rts: false
57+
exclude_replies: true
58+
, (err, reply) ->
59+
return msg.send "Error" if err
60+
return msg.send _.unescape(_.last(reply)['text']) if reply[0]['text']

0 commit comments

Comments
 (0)