|
2 | 2 | # Display a random tweet from twitter about a subject
|
3 | 3 | #
|
4 | 4 | # Dependencies:
|
5 |
| -# None |
| 5 | +# "ntwitter" : "https://github.com/sebhildebrandt/ntwitter/tarball/master", |
6 | 6 | #
|
7 | 7 | # 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 |
9 | 12 | #
|
10 | 13 | # Commands:
|
11 | 14 | # hubot <keyword> tweet - Returns a link to a tweet about <keyword>
|
12 | 15 | #
|
| 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 | +# |
13 | 21 | # Author:
|
14 |
| -# atmos |
| 22 | +# atmos, technicalpickles |
| 23 | + |
| 24 | +ntwitter = require 'ntwitter' |
| 25 | +inspect = require('util').inspect |
15 | 26 |
|
16 | 27 | 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 | + |
17 | 36 | 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}" |
27 | 68 | else
|
28 | 69 | msg.reply "No one is tweeting about that."
|
0 commit comments