-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
197 lines (158 loc) · 5.34 KB
/
bot.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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const { Telegraf } = require('telegraf');
const LocalSession = require('telegraf-session-local');
const _ = require('lodash');
const GUIDES = require('./guides.json');
const config = require('./config');
const replyText = require('./replyText');
const { getFriendDataByName } = require('./helpers');
const { getUserById, getUserByName } = require('./db');
const questions = require('./questions.json');
// Создаем объект Telegraf.js
const bot = new Telegraf(config.token);
bot.use((new LocalSession({ database: 'user_data.json' })).middleware());
bot.command('remove', (ctx) => {
ctx.replyWithMarkdown(`Removing session from database: \`${JSON.stringify(ctx.session)}\``);
ctx.session = null;
});
bot.start((ctx) => {
console.log(ctx.from);
console.log(ctx.message);
if (ctx.session.user) {
return;
}
ctx.reply(replyText.hello);
});
bot.command('friends', async (ctx) => {
if (!ctx.session.user) {
return;
}
const
friends = _.get(ctx, 'session.friends', []),
current = _.get(ctx, 'session.current');
let msgText = friends.map(({name}) => name).join('\n');
if (current) {
msgText += config.onlyPhoto ? '\nНекто' : `\n${current.name}`;
}
const msg = await ctx.reply(msgText);
ctx.session.meta = {
friendsMsgId: msg.message_id,
};
});
bot.on('message', (ctx, next) => {
const
replyMessageId = _.get(ctx, 'message.reply_to_message.message_id');
const friendId = _.get(ctx, `session.messageMap.${replyMessageId}`);
if (friendId) {
const
pathToStorage = `session.friendsStorage.${friendId}`;
const current = _.get(ctx, pathToStorage, []);
_.set(ctx, pathToStorage, [...current, ctx.message.text]);
return;
}
return next();
});
bot.on('message', async (ctx, next) => {
const
replyMessageId = _.get(ctx, 'message.reply_to_message.message_id', '');
const friendsMsgId = _.get(ctx, 'session.meta.friendsMsgId');
const msgText = ctx.message.text.trim().toLowerCase();
if (replyMessageId === friendsMsgId) {
try {
const user = msgText !== 'некто' ? getFriendDataByName(msgText, ctx) : ctx.session.current;
const savedData = _.get(ctx, `session.friendsStorage.${user.id}`, []).join('\n___\n');
await ctx.reply(savedData ? savedData : 'Нет записей');
} catch (err) {
await ctx.reply(err.message);
}
return;
}
return next();
});
bot.on('text', async (ctx, next) => {
try {
if (ctx.session.tmp_user || ctx.session.user) {
return next();
}
ctx.session.tmp_user = getUserByName(ctx.message.text);
await ctx.reply(replyText.inputSecret);
} catch (err) {
ctx.reply(err.message);
}
});
bot.on('text', async (ctx, next) => {
const { user, tmp_user } = ctx.session;
if (user || !tmp_user) {
return next();
}
if (ctx.message.text.trim().toLowerCase() === ctx.session.tmp_user.secret_code.trim().toLowerCase()) {
ctx.session.user = ctx.session.tmp_user;
ctx.session.guide = GUIDES[ctx.session.user.id];
ctx.session.score = 0;
ctx.session.friends = [];
ctx.session.friendsStorage = {};
ctx.session.messageMap = {};
await ctx.reply(`Привет, добро пожаловать в бот, ${ctx.session.user.name}!
Сейчас тебе будут приходить фото участников нашего летника.
Твоя задача проста:
1) Найди его на турбазе и познакомься с ним
2) Узнай ответ на вопрос, предложенный ботом или выполни задание
3) Узнай его секретный код и отправь боту, чтобы получить следующее фото`);
next();
} else {
await ctx.reply('Неверный код!');
await ctx.reply('Введите ФИО снова');
}
ctx.session.tmp_user = null;
});
bot.on('text', async (ctx, next) => {
const { user, current } = ctx.session;
if (!user || !current) {
return next();
}
const { text } = ctx.message;
if (text.trim().toLowerCase() === current.secret_code.trim().toLowerCase()) {
await ctx.reply('Круто!');
ctx.session.friends && ctx.session.friends.push(ctx.session.current);
ctx.session.current = null;
ctx.session.score += 1;
return next();
}
await ctx.reply('Неверно');
});
bot.on('text', async (ctx, next) => {
const { user, guide } = ctx.session;
if (!user || !guide.length) {
return next();
}
const
userId = guide.shift();
const anotherUser = getUserById(userId);
const message = `Твоя следующая цель: ${anotherUser.name}`;
ctx.session.current = anotherUser;
if (anotherUser.image) {
const msg = await ctx.replyWithPhoto(
{ url: anotherUser.image },
{ caption: config.onlyPhoto ? 'Твоя следующая цель' : message },
);
ctx.session.messageMap[msg.message_id] = anotherUser.id;
} else {
await ctx.reply(message);
}
await ctx.reply(questions[~~(Math.random() * questions.length)]);
});
bot.on('text', async (ctx, next) => {
const { user, guide, current } = ctx.session;
if (!user || guide.length || current) {
return next();
}
await ctx.reply(`Поздравляю🎉
Теперь ты знаешь всех в этом лагере😱`);
});
bot.launch();
console.log('===== BOT JOB HAS BEEN STARTED ======');
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'));
process.once('SIGTERM', () => bot.stop('SIGTERM'));