-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (49 loc) · 1.96 KB
/
index.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
const TelegramApi = require('node-telegram-bot-api')
const { options } = require('nodemon/lib/config')
const token = "1917307536:AAEhCsfHSVjl2FLKXtTmMEG_UwmVaUhCUkk"
const {gameOptions, againOptions} = require('./options')
const bot = new TelegramApi(token, {polling: true})
const chats = {}
bot.setMyCommands([
{command: '/start', description: 'initial greeting'},
{command: '/info', description: 'get user info'},
{command: '/game', description: 'play the game'}
])
const startGame = async (chatId) => {
await bot.sendMessage(chatId, 'I imagine the number from 0 to 9');
const randomNumber = Math.floor(Math.random() * 10)
chats[chatId] = randomNumber;
await bot.sendMessage(chatId, 'please guess the number', gameOptions)
}
const start = () => {
bot.on('message', async msg => {
console.log(msg)
const text = msg.text;
const chatId = msg.chat.id;
if(text === '/start') {
await bot.sendSticker(chatId, 'https://telegramchannels.me/storage/stickers/robertdowneys1ick3r/big_robertdowneys1ick3r_1.png')
return bot.sendMessage(chatId, `welcome to my Bot, ${msg.chat.first_name}`)
}
if(text === '/info') {
return bot.sendMessage(chatId, `your nickname is ${msg.chat.username}`)
}
if(text === '/game') {
return startGame(chatId);
}
return bot.sendMessage(chatId, 'can not understand you')
})
bot.on('callback_query', async msg => {
console.log(msg)
const data = msg.data;
const chatId = msg.message.chat.id;
if(data === '/again') {
return startGame(chatId);
}
if(data === chats[chatId]){
return await bot.sendMessage(chatId, `you guessed number ${chats[chatId]} correctly. you won`, againOptions)
} else {
return await bot.sendMessage(chatId, `you lost, your number ${chats[chatId]} is incorrect`, againOptions)
}
})
}
start()