|
2 | 2 | # gets tweet from user
|
3 | 3 | #
|
4 | 4 | # Dependencies:
|
5 |
| -# None |
| 5 | +# "twit": "1.1.6" |
| 6 | +# "underscore": "1.4.4" |
6 | 7 | #
|
7 | 8 | # Configuration:
|
8 |
| -# None |
| 9 | +# HUBOT_TWITTER_CONSUMER_KEY |
| 10 | +# HUBOT_TWITTER_CONSUMER_SECRET |
| 11 | +# HUBOT_TWITTER_ACCESS_TOKEN |
| 12 | +# HUBOT_TWITTER_ACCESS_TOKEN_SECRET |
9 | 13 | #
|
10 | 14 | # Commands:
|
11 | 15 | # hubot twitter <twitter username> - Show last tweet from <twitter username>
|
| 16 | +# hubot twitter <twitter username> <n> - Cycle through tweet with <n> starting w/ latest |
12 | 17 | #
|
13 | 18 | # Author:
|
14 | 19 | # KevinTraver
|
15 | 20 | #
|
| 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 | + |
16 | 30 | 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