-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode.ts
65 lines (52 loc) · 1.35 KB
/
node.ts
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
import { Bot, Context, session, SessionFlavor } from "grammy";
import { I18n, I18nFlavor } from "@grammyjs/i18n";
interface SessionData {
apples: number;
}
type MyContext =
& Context
& I18nFlavor
& SessionFlavor<SessionData>;
const bot = new Bot<MyContext>(""); // <-- put your bot token here (https://t.me/BotFather)
bot.use(session({
initial: () => ({ apples: 0 }),
}));
const i18n = new I18n<MyContext>({
defaultLocale: "en",
useSession: true,
directory: "locales",
globalTranslationContext(ctx) {
return {
first_name: ctx.from?.first_name ?? "",
};
},
});
bot.use(i18n);
bot.command("start", async (ctx) => {
await ctx.reply(ctx.t("greeting"));
});
bot.command(["en", "de", "ku", "ckb", "ru"], async (ctx) => {
const locale = ctx.msg.text.substring(1).split(" ")[0];
await ctx.i18n.setLocale(locale);
await ctx.reply(ctx.t("language-set"));
});
// Add apple to cart
bot.command("add", async (ctx) => {
ctx.session.apples++;
await ctx.reply(ctx.t("cart", {
apples: ctx.session.apples,
}));
});
bot.command("cart", async (ctx) => {
await ctx.reply(ctx.t("cart", {
apples: ctx.session.apples,
}));
});
bot.command("checkout", async (ctx) => {
ctx.session.apples = 0;
await ctx.reply(ctx.t("checkout"));
});
bot.command("multiline", async (ctx) => {
await ctx.reply(ctx.t("multiline"));
});
bot.start();