forked from mullwar/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin-askUser.js
49 lines (31 loc) · 923 Bytes
/
plugin-askUser.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
const TeleBot = require('../');
const bot = new TeleBot({
token: 'TELEGRAM_BOT_TOKEN',
usePlugins: ['askUser']
});
// On start command
bot.on('/start', msg => {
const id = msg.from.id;
// Ask user name
return bot.sendMessage(id, 'What is your name?', {ask: 'name'});
});
// Ask name event
bot.on('ask.name', msg => {
const id = msg.from.id;
const name = msg.text;
// Ask user age
return bot.sendMessage(id, `Nice to meet you, ${ name }! How old are you?`, {ask: 'age'});
});
// Ask age event
bot.on('ask.age', msg => {
const id = msg.from.id;
const age = Number(msg.text);
if (!age) {
// If incorrect age, ask again
return bot.sendMessage(id, 'Incorrect age. Please, try again!', {ask: 'age'});
} else {
// Last message (don't ask)
return bot.sendMessage(id, `You are ${ age } years old. Great!`);
}
});
bot.start();