-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
107 lines (99 loc) · 3.22 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
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
const { Composer, Telegram, Markup } = require('micro-bot')
const Router = require('router')
const serveStatic = require('serve-static')
const bodyParser = require('body-parser')
const finalhandler = require('finalhandler')
const url = require('url')
const jws = require('jws')
const { readdirSync, statSync } = require('fs')
const gamesRoot = `${__dirname}/public/games`
const games = readdirSync(gamesRoot).filter((name) => statSync(`${gamesRoot}/${name}`).isDirectory())
// Bot staff
const startMessage = `
*MicroGames* for *MegaFun*
Just tap "Play with friends", then choose a chat and select a game.
`
const inlineAnswer = games.map((game) => {
return {
type: 'game',
id: game,
game_short_name: game,
reply_markup: Markup.inlineKeyboard([
Markup.gameButton('🎮 Play now'),
Markup.urlButton('⭐️ Rate MicroGames', 'https://telegram.me/storebot?start=microgamesbot')
], {columns: 1})
}
})
const extra = Markup.inlineKeyboard([
Markup.switchToCurrentChatButton('🎮 Play now', ''),
Markup.switchToChatButton('🏆 Play with friends', ''),
Markup.urlButton('⭐️ Rate MicroGames', 'https://telegram.me/storebot?start=microgamesbot')
], { columns: 1 }).extra()
const bot = new Composer()
bot.gameQuery((ctx) => {
const game = ctx.callbackQuery.game_short_name
const token = jws.sign({
header: { alg: 'HS512' },
payload: {
game: game,
user: ctx.from.id,
imessage: ctx.callbackQuery.inline_message_id,
message: (ctx.callbackQuery.message || {}).message_id,
chat: (ctx.chat || {}).id
},
secret: process.env.SIGN_SECRET
})
return ctx.answerCallbackQuery(null, `${process.env.NOW_URL}/games/${game}/?token=${token}`)
})
bot.on('inline_query', (ctx) => ctx.answerInlineQuery(inlineAnswer))
bot.on('text', (ctx) => ctx.replyWithMarkdown(startMessage, extra))
// HTTP staff
const telegram = new Telegram(process.env.BOT_TOKEN)
const router = Router()
router.use(serveStatic('public'))
const score = router.route('/score')
score.all((req, res, next) => {
const token = url.parse(req.headers.referer).query.slice(6)
if (!jws.verify(token, 'HS512', process.env.SIGN_SECRET)) {
res.statusCode = 403
return res.end()
}
req.microGame = JSON.parse(jws.decode(token).payload)
next()
})
score.all(bodyParser.json())
score.get((req, res) => {
const { user, imessage, chat, message } = req.microGame
telegram.getGameHighScores(user, imessage, chat, message)
.then((scores) => {
res.setHeader('content-type', 'application/json')
res.statusCode = 200
res.end(JSON.stringify(scores, null, true))
})
.catch((err) => {
console.log(err)
res.statusCode = 500
res.end()
})
})
score.post((req, res) => {
const scoreValue = parseInt(req.body.score)
if (scoreValue <= 0) {
res.statusCode = 400
return res.end()
}
const { user, imessage, chat, message } = req.microGame
telegram.setGameScore(user, scoreValue, imessage, chat, message, true)
.then(() => {
res.statusCode = 200
res.end()
})
.catch((err) => {
res.statusCode = err.code || 500
res.end(err.description)
})
})
module.exports = {
botHandler: bot,
requestHandler: (req, res) => router(req, res, finalhandler(req, res))
}