-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
58 lines (50 loc) · 1.57 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
// var dotenv = require('dotenv').config()
var TelegramBot = require('node-telegram-bot-api')
var TOKEN = process.env.TELEGRAM_BOT_TOKEN
// Polling: false, we use webhooks instead
var bot = new TelegramBot(TOKEN, { polling: false })
var GAME_SHORT_NAME = 'rainbowrex'
function sendGame (event, lambdaCallback) {
// parse the chat ID so we can respond
var chatId = event.body.message.chat.id
bot.sendGame(
event.body.message.from.id,
GAME_SHORT_NAME
).then(function () {
lambdaCallback(null, '')
}).catch(function (error) {
console.log(error)
})
}
function startGame (event, lambdaCallback) {
var cq = event.body.callback_query
var msgId = cq.message.message_id
var chatId = cq.message.chat.id
var userId = cq.from.id
var gameUrlWithParams = 'https://rainbowrex.surge.sh/?userId=' + userId + '&chatId=' + chatId + '&msgId=' + msgId
if (cq.game_short_name && cq.game_short_name === GAME_SHORT_NAME) {
bot.answerCallbackQuery(
cq.id,
undefined,
false,
{ url: gameUrlWithParams }
).then(function () {
lambdaCallback(null, '')
}).catch(function (error) {
console.log(error)
})
} else {
bot.answerCallbackQuery(
cq.id,
"Sorry, '" + cq.game_short_name + "' is not available.", true
).then(function () {
lambdaCallback(null, '')
}).catch(function (error) {
console.log(error)
})
}
}
exports.handler = function (event, context, lambdaCallback) {
if (event.body.message) sendGame(event, lambdaCallback)
if (event.body.callback_query) startGame(event, lambdaCallback)
}