-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
239 lines (156 loc) · 4.97 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
var exec = require('child_process').exec
var fs = require('fs')
var url = require('url')
var menu = require('./menu')
var VK = require('./vk')
var Q = require('q')
var _ = require('lodash')
var production = (process.env.NODE_ENV === 'production')
console.log('vk-bot> initializing...', 'NODE_ENV:', process.env.NODE_ENV)
optionsFile = __dirname + '/options.json'
var options = JSON.parse(fs.readFileSync(optionsFile).toString())
authFile = __dirname + '/auth.json'
var auth = JSON.parse(fs.readFileSync(authFile).toString())
var vk = new VK({
appID: options.vk_app_id,
// appSecret: options.vk_app_secret,
// mode: 'oauth'
login: auth.login,
password: auth.password,
scope: 'messages,photos',
proxy: options.proxy
});
var tokenFile = __dirname + '/token.json'
fs.readFile(tokenFile, function (err, data) {
if (err) {
console.log('ERROR: no token file, need to authorize')
} else {
vk.setToken(JSON.parse(data.toString()))
}
});
var listening = null;
var last_message_id = null;
var commands = {
auth_old: function () {
var _url = vk.authUrl('messages,photos')
// exec('open ' + _url)
exec('echo "'+ _url +'" | pbcopy' )
console.log('Auth url:\n')
console.log(_url + '\n')
console.log('[ copied to clipboard ]')
},
auth: function () {
vk.updateToken(function (token) {
fs.writeFile(tokenFile, JSON.stringify(token), function (err) {
if (err) { console.log('ERROR: token not saved', err) }
})
})
},
// "code (.*)": function (match) {
// vk.setToken({ code: match[1] });
// },
"token (.*)": function (match) {
vk.setToken({
value: match[1]
}, true);
},
// evl: function () {
// eval js in bot context?
// },
status: function () {
console.log('VK_APP_ID:', vk.options.appID)
console.log('token:', vk.token && vk.token.value)
console.log('token expires:', vk.token && new Date(vk.token.expires))
console.log('listening:', !!listening)
},
listen: function () {
if (!listening) {
if (!vk.checkToken()) {
return commands.auth()
}
console.log('listening...\n')
listening = setInterval(function () {
var options = { count: 100, v: '5.21' }
if (last_message_id) {
options.last_message_id = last_message_id;
// options.time_offset = 6
}
vk.request('messages.get', options);
}, 60 * 1000)
} else {
clearInterval(listening);
listening = null;
}
}
}
var phrases = require('./phrases')
var knownCommands = _(phrases).pluck('pattern').map(function(i) {
return '"' + i.toString().replace(/[a-z\/\(\)\|]*/gim,'') + '?"'
}).join(', ')
var onMessages = function(data) {
if (data.error) {
if (data.error.error_code === 5) {
commands.auth()
}
return console.log('ERROR messages.get :', data.error)
}
var messages = [];
if (data.response && data.response.items) messages = data.response.items
if (messages.length) !production && console.log('\nmessages.get ', messages.length)
messages.forEach(function(msg) {
if (msg.message) msg = msg.message
if (msg.id > last_message_id) last_message_id = msg.id
if (!msg.read_state) {
var unknown = true
var isChat = msg.chat_id !== undefined
phrases.forEach(function(p) {
if (msg.body.match(p.pattern)) {
!production && console.log('message:', msg.body)
unknown = false
var coords = null;
if (msg.geo) {
coords = msg.geo.coordinates.split(' ')
}
Q.when(p.response(coords), function (value) {
value = value.slice(0, 300) + '...'
!production && console.log('response: (length:' + value.length + ') ', value)
var options = {
message: value
}
if (isChat) {
options.chat_id = msg.chat_id
} else {
options.user_id = msg.user_id
}
vk.request('messages.send', options)
})
}
})
if (unknown && !isChat) {
vk.request('messages.send', {
user_id: msg.user_id,
message: 'Я не понял. Я робот. Я понимаю только: ' + knownCommands + '. http://vk.com/wall258131347_2 [' + Date.now().toString().substr(-3,3) + ']'
})
}
}
});
}
vk.on('done:messages.get', onMessages);
vk.on('done:messages.getDialogs', onMessages);
var getLostMessages = _.debounce(function () {
vk.request('messages.getDialogs', { count: 30, unread: 1, v: '5.21' });
}, 3 * 1000)
vk.on('done:messages.send', function(data) {
getLostMessages()
!production && console.log('done:messages.send', data)
})
setTimeout(function() {
console.log('ready\n')
commands.status()
console.log('\n')
var start_command = commands[process.argv[2]]
if (start_command) start_command()
}, 500)
if (!production) {
var m = menu('vk-bot> ', commands)
}