-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.mjs
56 lines (45 loc) · 1.64 KB
/
bot.mjs
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
/**
* @typedef {import("grammy").Context} Context
* @typedef {import("@grammyjs/types").Chat} Chat
* @typedef {import("grammy").SessionFlavor} SessionFlavor
*
* @typedef {{isGame: boolean, matrix: Array<Array<string>>} & Chat} SessionData
* @typedef {Context & SessionFlavor<SessionData>} BotContext
*/
import {Bot, InlineKeyboard, session} from "grammy";
import {freeStorage} from "@grammyjs/storage-free";
import {createMatrix, matrixToKeyboard, startKeyboard} from "./game.mjs";
import {gameTurn} from "./game.turn.mjs";
export const {
TELEGRAM_BOT_TOKEN: token,
TELEGRAM_SECRET_TOKEN: secretToken = String(token).split(":").pop()
} = process.env;
export const bot = /** @type {Bot<BotContext>} */ new Bot(token);
const safe = bot.errorBoundary(e => console.error(e));
safe.use(session({
initial: () => ({isGame: false}),
storage: freeStorage(bot.token),
}));
safe.command("start", ctx =>
ctx.reply(`Hello!`, {
reply_markup: startKeyboard
})
);
safe.callbackQuery("start", async ctx => {
await ctx.answerCallbackQuery();
await ctx.reply("Choose your mark", {
reply_markup: new InlineKeyboard().text("❌").text("⭕️")
})
return ctx.deleteMessage();
})
safe.callbackQuery(["❌", "⭕️"], async ctx => {
await ctx.answerCallbackQuery({text: `You choiced ${ctx.callbackQuery.data}`});
ctx.session.isGame = true;
ctx.session.matrix = createMatrix()
ctx.session.symbol = ctx.callbackQuery.data
await ctx.reply("Game on", {
reply_markup: matrixToKeyboard(ctx.session.matrix, ctx.session.symbol)
})
return ctx.deleteMessage();
})
safe.on("callback_query:data", gameTurn)