-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrat.js
211 lines (187 loc) · 7.07 KB
/
brat.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
'use strict';
var path = require('path');
var request = require('request');
var fs = require('fs');
// var config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
var TOKEN = '234324399:AAFrM5QwF_j4_qq1QWYrTf_H9HZ_0j9EYjs';
var baseRequest = request.defaults({
baseUrl: 'https://api.telegram.org/bot' + TOKEN + '/'
});
var noop = function (err) {
if(err) { console.log(err); }
};
var callMethod = function (methodName, params, cb) {
cb = cb || noop;
var req = {uri: methodName, method: 'POST'};
if (Object.keys(params).length) {
req.formData = params;
}
baseRequest(req, function (err, response, body) {
// console.log(err, body);
if (err) {
return cb(err);
}
cb(err, JSON.parse(body));
});
};
var getUpdatesOffset = 0;
var getUpdates = function (cb) {
var params = {offset: getUpdatesOffset, timeout: 60};
callMethod('getUpdates', params, function (err, data) {
if (err) {
return cb(err);
}
if (data.result.length) {
getUpdatesOffset = data.result[data.result.length - 1].update_id + 1;
}
cb(err, data);
});
}
var handlers = {
'ping': function (message) {
callMethod('sendMessage', {chat_id: message.chat.id, text: 'pong'});
},
'cat': function (message) {
request('http://random.cat/meow', function (err, res, body) {
if (err) {
console.log(err);
return callMethod('sendMessage', {chat_id: message.chat.id, text: 'Something went wrong, try again!'});
}
var imageUrl = JSON.parse(body).file;
var file = request(imageUrl);
var params = {
chat_id: message.chat.id,
caption: imageUrl
};
if (path.extname(imageUrl) === '.gif') {
callMethod('sendChatAction', {chat_id: message.chat.id, action: 'upload_document'});
params.document = file;
callMethod('sendDocument', params);
} else {
callMethod('sendChatAction', {chat_id: message.chat.id, action: 'upload_photo'});
params.photo = file;
callMethod('sendPhoto', params);
}
});
},
'1': function (message) {
request('https://pp.vk.me/c631529/v631529444/1d29e/VyaGHxyHcMY.jpg', function (err, res, body) {
//if (err) {
//console.log(err);
//return callMethod('sendMessage', {chat_id: message.chat.id, text: 'Something went wrong, try again!'});
//}
var imageUrl = 'https://pp.vk.me/c631529/v631529444/1d29e/VyaGHxyHcMY.jpg';
var file = request(imageUrl);
var params = {
chat_id: message.chat.id,
caption: imageUrl
};
//if (path.extname(imageUrl) === '.gif') {
//callMethod('sendChatAction', {chat_id: message.chat.id, action: 'upload_document'});
//params.document = file;
//callMethod('sendDocument', params);
//} else {
callMethod('sendChatAction', {chat_id: message.chat.id, action: 'upload_photo'});
params.photo = file;
callMethod('sendPhoto', params);
//}
});
},
'bratishka': function (message) {
request('https://pp.vk.me/c631529/v631529444/1d29e/VyaGHxyHcMY.jpg', function (err, res, body) {
//if (err) {
//console.log(err);
//return callMethod('sendMessage', {chat_id: message.chat.id, text: 'Something went wrong, try again!'});
//}
var params = {
chat_id: message.chat.id
};
//if (path.extname(imageUrl) === '.gif') {
//callMethod('sendChatAction', {chat_id: message.chat.id, action: 'upload_document'});
//params.document = file;
//callMethod('sendDocument', params);
//} else {
return callMethod('sendMessage', {chat_id: message.chat.id, text: 'да-да?'});
//}
});
},
'bratishka off': function (message) {
request('https://pp.vk.me/c631529/v631529444/1d29e/VyaGHxyHcMY.jpg', function (err, res, body) {
//if (err) {
//console.log(err);
//return callMethod('sendMessage', {chat_id: message.chat.id, text: 'Something went wrong, try again!'});
//}
var params = {
chat_id: message.chat.id
};
//if (path.extname(imageUrl) === '.gif') {
//callMethod('sendChatAction', {chat_id: message.chat.id, action: 'upload_document'});
//params.document = file;
//callMethod('sendDocument', params);
//} else {
return callMethod('sendMessage', {chat_id: message.chat.id, text: 'Пока! :('});
//}
});
},
'bratishka on': function (message) {
request('https://pp.vk.me/c631529/v631529444/1d29e/VyaGHxyHcMY.jpg', function (err, res, body) {
//if (err) {
//console.log(err);
//return callMethod('sendMessage', {chat_id: message.chat.id, text: 'Something went wrong, try again!'});
//}
var params = {
chat_id: message.chat.id
};
//if (path.extname(imageUrl) === '.gif') {
//callMethod('sendChatAction', {chat_id: message.chat.id, action: 'upload_document'});
//params.document = file;
//callMethod('sendDocument', params);
//} else {
return callMethod('sendMessage', {chat_id: message.chat.id, text: 'Привет! :)'});
//}
});
}
};
var commands = Object.keys(handlers);
var messageHandler = function (update) {
if (!update.message || !update.message.text) {
return console.log('unhandled update', update);
}
var text = update.message.text.toLowerCase();
var regV = /сис(ек|ьки|ечки|и|яндры)/; // шаблон
var result = text.match(regV); // поиск шаблона в юрл
if (result){
return handlers['1'](update.message);
}
var brat = /братишк(а|и)/;
result = text.match(brat);
if (result){
return handlers['bratishka'](update.message);
}
if (commands.indexOf(update.message.text) !== -1) {
console.log(update.message.text);
return handlers[update.message.text](update.message);
}
var text = 'Sorry, I only understand commands' + commands.join(' ');
//callMethod('sendMessage', {chat_id: update.message.chat.id, text: text});
};
var runBot = function () {
getUpdates(function (err, data) {
if (err) {
console.log(err);
return runBot();
}
if (!data.ok) {
console.log(data);
return runBot();
}
data.result.map(messageHandler);
runBot();
});
};
callMethod('getMe', {}, function (err, data) {
if (err) {
throw err;
}
runBot();
});