-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
71 lines (61 loc) · 2.6 KB
/
bot.js
File metadata and controls
71 lines (61 loc) · 2.6 KB
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
/**
* Created by Igor.Suhoverhov on 11.04.2016.
*/
var TelegramBot = require('node-telegram-bot-api');
var token = '211353716:AAEUu013rJ5stdYkMtDtHl1vfmiyHfvHlPM';
var bot = new TelegramBot(token, {polling: true});
console.log('bot server started...');
var User = require('./libs/mongoose').User;
bot.on('message', function (msg) {
var chatId = msg.chat.id;
console.log(msg);
if (msg.text.indexOf("friend") == 1){
var friendUserId = msg.text.split(' ')[1];
User.findOne({id: friendUserId}, function (err, user) {
if (!user) {
bot.sendMessage(chatId, "К сожалению, пользователь ещё не обращался к боту. Необходимо, чтобы он добавился в систему.");
}
else {
User.findOne({id: chatId}, function (err, user) {
if (!user) {
}
else {
user.current_friend_id = friendUserId;
user.save(function (err, newUser) {
if (err) {
console.log("Something goes wrong with user " + newUser.id);
} else {
console.log("Data is updated for id" + newUser.id)
}
});
}
});
}
});
}
else {
User.findOne({id: chatId}, function (err, user) {
if (!user) {
console.log('Not found');
bot.sendMessage(chatId, "Привет, " + msg.chat.first_name + "! Твой ID " + chatId + " Передай его собеседнику! ");
var newUser = new User({id: chatId, first_name: msg.chat.first_name});
newUser.save(function (err, newUser) {
if (err) {
console.log("Something goes wrong with user " + newUser.id);
} else {
console.log("Data is saved for id" + newUser.id)
}
});
}
else {
console.log('Found success');
if (user.current_friend_id != null) {
bot.sendMessage(user.current_friend_id, msg.text);
}
else {
bot.sendMessage(chatId, "Привет, " + msg.chat.first_name + "! Давно не виделись, не желаешь с кем-нибудь пообщаться?");
}
}
});
}
});