-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
59 lines (49 loc) · 1.21 KB
/
index.js
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
var Twit = require('twit')
var greet = require('./greeting')
var T = new Twit({
consumer_key: process.env.consumer_key,
consumer_secret: process.env.consumer_secret,
access_token: process.env.access_token,
access_token_secret: process.env.access_token_secret
})
function verifyCredentials(cb) {
T.get(
'account/verify_credentials',
{
include_entities: false,
skip_status: true,
include_email: false
},
cb
)
}
function sendGreeting(user) {
T.post(
'direct_messages/new',
{
user_id: user.id_str,
text: greet(user.name)
},
function(err) {
if (err) {
console.error('error in sendGreeting to user: %s %s %s', user.name, user.screen_name, user.id_str)
console.error(err)
}
}
)
}
var stream = T.stream('user')
verifyCredentials(function(err, res) {
if (err) throw err
var account_id = res.id_str
console.log('credentials ok - running bot')
stream.on('follow', function(json) {
// ignore event, which is fired when we follow someone
if (json.event === 'follow' && json.source.id_str !== account_id) {
sendGreeting(json.source)
}
})
stream.on('error', function(error) {
throw error
})
})