1
+ # Description
2
+ # Filters out Twitter stream and displays tweets
3
+ #
4
+ # Dependencies:
5
+ # "twit": "1.1.6"
6
+ #
7
+ # Configuration:
8
+ # HUBOT_TWITTER_STREAM_CONSUMER_KEY
9
+ # HUBOT_TWITTER_STREAM_CONSUMER_SECRET
10
+ # HUBOT_TWITTER_STREAM_ACCESS_TOKEN
11
+ # HUBOT_TWITTER_ACCESS_TOKEN_SECRET
12
+ #
13
+ # Commands:
14
+ # hubot twitter stream <filter> - Connects to Twitter stream and filters tweets according to <filter>
15
+ # hubot stop twitter stream - Disconnects from Twitter stream
16
+ #
17
+ # Notes:
18
+ # Only one stream can be active at the same time. The filter operates on the <track> parameter of
19
+ # Twitter statuses/filter endpoint. See https://dev.twitter.com/docs/api/1.1/post/statuses/filter
20
+ # for additional details.
21
+ #
22
+ # Author:
23
+ # matteoagosti
24
+
25
+ Twit = require " twit"
26
+ config =
27
+ consumer_key : process .env .HUBOT_TWITTER_STREAM_CONSUMER_KEY
28
+ consumer_secret : process .env .HUBOT_TWITTER_STREAM_CONSUMER_SECRET
29
+ access_token : process .env .HUBOT_TWITTER_STREAM_ACCESS_TOKEN
30
+ access_token_secret : process .env .HUBOT_TWITTER_ACCESS_TOKEN_SECRET
31
+
32
+ module .exports = (robot ) ->
33
+ twit = undefined
34
+ stream = undefined
35
+
36
+ robot .respond / twitter stream (. * )/ i , (msg ) ->
37
+ unless config .consumer_key
38
+ msg .send " Please set the HUBOT_TWITTER_STREAM_CONSUMER_KEY environment variable."
39
+ return
40
+ unless config .consumer_secret
41
+ msg .send " Please set the HUBOT_TWITTER_STREAM_CONSUMER_SECRET environment variable."
42
+ return
43
+ unless config .access_token
44
+ msg .send " Please set the HUBOT_TWITTER_STREAM_ACCESS_TOKEN environment variable."
45
+ return
46
+ unless config .access_token_secret
47
+ msg .send " Please set the HUBOT_TWITTER_ACCESS_TOKEN_SECRET environment variable."
48
+ return
49
+
50
+ filter = msg .match [1 ]
51
+ unless filter
52
+ msg .send " Please, specify the Twitter stream filter"
53
+ return
54
+
55
+ unless twit
56
+ twit = new Twit config
57
+
58
+ if stream
59
+ stream .stop ();
60
+
61
+ stream = twit .stream (" statuses/filter" ,
62
+ track : filter
63
+ )
64
+
65
+ msg .send " Thank you, I'll filter out Twitter stream as requested: #{ filter} "
66
+
67
+ stream .on " tweet" , (tweet ) ->
68
+ msg .send " https://twitter.com/#{ tweet .user .screen_name } /status/#{ tweet .id_str } "
69
+ stream .on " disconnect" , (disconnectMessage ) ->
70
+ msg .send " I've got disconnected from Twitter stream. Apparently the reason is: #{ disconnectMessage} "
71
+ stream .on " reconnect" , (request , response , connectInterval ) ->
72
+ msg .send " I'll reconnect to Twitter stream in #{ connectInterval} ms"
73
+
74
+ robot .respond / stop twitter stream/ i , (msg ) ->
75
+ if stream
76
+ stream .stop ()
77
+ msg .send " Ok, I'm now disconnected from Twitter stream"
0 commit comments