From 58a1ffe32aeee68903812eafcd92384cb3abd410 Mon Sep 17 00:00:00 2001 From: deptyped Date: Tue, 23 Jul 2024 00:36:35 +0300 Subject: [PATCH] Use @grammyjs/runner for polling mode --- README.md | 30 ------------------------------ package-lock.json | 15 +++++++++++++++ package.json | 1 + src/bot/index.ts | 20 ++++++++++++-------- src/main.ts | 24 ++++++++++++++++-------- 5 files changed, 44 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 24de3a03..932b80eb 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,6 @@ Bot starter template based on [grammY](https://grammy.dev/) bot framework. - Examples: - grammY plugins: - [Conversations](#grammy-conversations-grammydevpluginsconversations) - - [Runner](#grammy-runner-grammydevpluginsrunner) - Databases: - [Prisma ORM](#prisma-orm-prismaio) - Runtimes: @@ -183,35 +182,6 @@ npm i @grammyjs/conversations 4. Follow [the usage instructions](https://github.com/bot-base/telegram-bot-template/tree/example/plugin-conversations#usage) in the `example/plugin-conversations` branch. -### grammY runner ([grammy.dev/plugins/runner](https://grammy.dev/plugins/runner)) - -Branch: -[example/plugin-runner](https://github.com/bot-base/telegram-bot-template/tree/example/plugin-runner) -([open diff](https://github.com/bot-base/telegram-bot-template/compare/example/plugin-runner)) - -Use in your project: - -1. Add the template repository as a remote - -```sh -git remote add template git@github.com:bot-base/telegram-bot-template.git -git remote update -``` - -2. Merge example - -```sh -git merge template/example/plugin-runner -X theirs --squash --no-commit --allow-unrelated-histories -``` - -3. Install dependencies - -```sh -npm i @grammyjs/runner -``` - -4. Follow [the usage instructions](https://github.com/bot-base/telegram-bot-template/tree/example/plugin-runner#usage) in the `example/plugin-runner` branch. - ### Prisma ORM ([prisma.io](https://prisma.io)) Branch: diff --git a/package-lock.json b/package-lock.json index e4f5f993..6b5f8c30 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@grammyjs/hydrate": "1.4.1", "@grammyjs/i18n": "1.0.2", "@grammyjs/parse-mode": "1.10.0", + "@grammyjs/runner": "2.0.3", "@grammyjs/types": "3.11.0", "@hono/node-server": "1.12.0", "callback-data": "1.1.1", @@ -912,6 +913,20 @@ "grammy": "^1.20.1" } }, + "node_modules/@grammyjs/runner": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@grammyjs/runner/-/runner-2.0.3.tgz", + "integrity": "sha512-nckmTs1dPWfVQteK9cxqxzE+0m1VRvluLWB8UgFzsjg62w3qthPJt0TYtJBEdG7OedvfQq4vnFAyE6iaMkR42A==", + "dependencies": { + "abort-controller": "^3.0.0" + }, + "engines": { + "node": ">=12.20.0 || >=14.13.1" + }, + "peerDependencies": { + "grammy": "^1.13.1" + } + }, "node_modules/@grammyjs/types": { "version": "3.11.0", "resolved": "https://registry.npmjs.org/@grammyjs/types/-/types-3.11.0.tgz", diff --git a/package.json b/package.json index 93e1f577..539912a9 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@grammyjs/hydrate": "1.4.1", "@grammyjs/i18n": "1.0.2", "@grammyjs/parse-mode": "1.10.0", + "@grammyjs/runner": "2.0.3", "@grammyjs/types": "3.11.0", "@hono/node-server": "1.12.0", "callback-data": "1.1.1", diff --git a/src/bot/index.ts b/src/bot/index.ts index d316963a..74dd6fa2 100644 --- a/src/bot/index.ts +++ b/src/bot/index.ts @@ -3,6 +3,7 @@ import { hydrate } from '@grammyjs/hydrate' import { hydrateReply, parseMode } from '@grammyjs/parse-mode' import type { BotConfig, StorageAdapter } from 'grammy' import { Bot as TelegramBot, session } from 'grammy' +import { sequentialize } from '@grammyjs/runner' import { welcomeFeature } from './features/welcome.js' import { adminFeature } from './features/admin.js' import { languageFeature } from './features/language.js' @@ -25,6 +26,10 @@ interface Options { botConfig?: Omit, 'ContextConstructor'> } +function getSessionKey(ctx: Omit) { + return ctx.chat?.id.toString() +} + export function createBot(token: string, dependencies: Dependencies, options: Options = {}) { const { config, @@ -43,16 +48,17 @@ export function createBot(token: string, dependencies: Dependencies, options: Op // Middlewares bot.api.config.use(parseMode('HTML')) - if (config.isDebug) - protectedBot.use(updateLogger()) - + config.isPollingMode && protectedBot.use(sequentialize(getSessionKey)) + config.isDebug && protectedBot.use(updateLogger()) protectedBot.use(autoChatAction(bot.api)) protectedBot.use(hydrateReply) protectedBot.use(hydrate()) - protectedBot.use( + protectedBot.filter( + ctx => getSessionKey(ctx) !== 'undefined', session({ - initial: () => ({}), + getSessionKey, storage: options.botSessionStorage, + initial: () => ({}), }), ) protectedBot.use(i18n) @@ -60,9 +66,7 @@ export function createBot(token: string, dependencies: Dependencies, options: Op // Handlers protectedBot.use(welcomeFeature) protectedBot.use(adminFeature) - - if (isMultipleLocales) - protectedBot.use(languageFeature) + isMultipleLocales && protectedBot.use(languageFeature) // must be the last handler protectedBot.use(unhandledFeature) diff --git a/src/main.ts b/src/main.ts index 34a2e487..e1a4116c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,7 @@ import process from 'node:process' import { ValiError, flatten } from 'valibot' +import { type RunnerHandle, run } from '@grammyjs/runner' import { createLogger } from './logger.js' import { createBot } from '#root/bot/index.js' import type { PollingConfig, WebhookConfig } from '#root/config.js' @@ -14,21 +15,28 @@ async function startPolling(config: PollingConfig) { config, logger, }) + let runner: undefined | RunnerHandle // graceful shutdown onShutdown(async () => { logger.info('Shutdown') - await bot.stop() + await runner?.stop() }) + await bot.init() + // start bot - await bot.start({ - allowed_updates: config.botAllowedUpdates, - onStart: ({ username }) => - logger.info({ - msg: 'Bot running...', - username, - }), + runner = run(bot, { + runner: { + fetch: { + allowed_updates: config.botAllowedUpdates, + }, + }, + }) + + logger.info({ + msg: 'Bot running...', + username: bot.botInfo.username, }) }