This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
/
twitter-mentionv2.coffee
60 lines (53 loc) · 1.69 KB
/
twitter-mentionv2.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Description:
# Continually searches for mentions of word/phrase on twitter
# and reports new tweets
#
# Dependencies:
# "twit": "1.1.x"
#
# Configuration:
# HUBOT_TWITTER_CONSUMER_KEY
# HUBOT_TWITTER_CONSUMER_SECRET
# HUBOT_TWITTER_ACCESS_TOKEN_KEY
# HUBOT_TWITTER_ACCESS_TOKEN_SECRET
# HUBOT_TWITTER_MENTION_QUERY
# HUBOT_TWITTER_MENTION_ROOM
#
# Commands:
# none
#
# Author:
# eric@softwareforgood based on scripts by gkoo and timdorr
#
TWIT = require "twit"
MENTION_ROOM = process.env.HUBOT_TWITTER_MENTION_ROOM || "#general"
MAX_TWEETS = 5
config =
consumer_key: process.env.HUBOT_TWITTER_CONSUMER_KEY
consumer_secret: process.env.HUBOT_TWITTER_CONSUMER_SECRET
access_token: process.env.HUBOT_TWITTER_ACCESS_TOKEN_KEY
access_token_secret: process.env.HUBOT_TWITTER_ACCESS_TOKEN_SECRET
getTwit = ->
unless twit
twit = new TWIT config
module.exports = (robot) ->
robot.brain.on 'loaded', =>
robot.brain.data.last_tweet ||= '1'
doAutomaticSearch(robot)
doAutomaticSearch = (robot) ->
query = process.env.HUBOT_TWITTER_MENTION_QUERY
since_id = robot.brain.data.last_tweet
count = MAX_TWEETS
twit = getTwit()
twit.get 'search/tweets', {q: query, count: count, since_id: since_id}, (err, data) ->
if err
console.log "Error getting tweets: #{err}"
return
if data.statuses? and data.statuses.length > 0
robot.brain.data.last_tweet = data.statuses[0].id_str
for tweet in data.statuses.reverse()
message = "Tweet Alert: http://twitter.com/#{tweet.user.screen_name}/status/#{tweet.id_str}"
robot.messageRoom MENTION_ROOM, message
setTimeout (->
doAutomaticSearch(robot)
), 1000 * 60 * 2