From 4a8476c35ca0c208230d15195250e90daf8b3303 Mon Sep 17 00:00:00 2001 From: Aleksei Pudnikov Date: Sun, 23 Jun 2024 19:02:10 +0200 Subject: [PATCH] send conversations --- src/bot/conversations/index.ts | 2 +- .../conversations/{greeting.ts => send.ts} | 21 ++++++++++--------- src/bot/features/welcome.ts | 8 +++---- src/bot/index.ts | 5 +++-- 4 files changed, 19 insertions(+), 17 deletions(-) rename src/bot/conversations/{greeting.ts => send.ts} (52%) diff --git a/src/bot/conversations/index.ts b/src/bot/conversations/index.ts index de53144..28f287d 100644 --- a/src/bot/conversations/index.ts +++ b/src/bot/conversations/index.ts @@ -1 +1 @@ -export * from './greeting.js' +export * from './send.js' diff --git a/src/bot/conversations/greeting.ts b/src/bot/conversations/send.ts similarity index 52% rename from src/bot/conversations/greeting.ts rename to src/bot/conversations/send.ts index 8ac4113..278e97a 100644 --- a/src/bot/conversations/greeting.ts +++ b/src/bot/conversations/send.ts @@ -3,14 +3,14 @@ import { createConversation } from '@grammyjs/conversations' import type { Context } from '#root/bot/context.js' import { i18n } from '#root/bot/i18n.js' -export const GREETING_CONVERSATION = 'greeting' +export const SEND_CONVERSATION = 'send' -export function greetingConversation() { +export function sendConversation(adminId: string) { return createConversation( async (conversation: Conversation, ctx: Context) => { await conversation.run(i18n) - await ctx.reply('Please send me your name') + await ctx.reply('Please send me the photo') while (true) { ctx = await conversation.wait() @@ -18,17 +18,18 @@ export function greetingConversation() { if (ctx.hasCommand('cancel')) { return ctx.reply('Cancelled') } - else if (ctx.has('message:text')) { - ctx.chatAction = 'typing' - await conversation.sleep(1000) - - await ctx.reply(`Hello, ${ctx.message.text}!`) + else if (ctx.has('message:photo')) { + const fileId = ctx.message?.photo?.pop()?.file_id + if (fileId) { + await ctx.reply(`Thanks for the photo!`) + await ctx.api.sendPhoto(adminId, fileId, { caption: `@${ctx.message?.from.username}` }) + } } else { - await ctx.reply('Please send me your name') + await ctx.reply('Please send me photo') } } }, - GREETING_CONVERSATION, + SEND_CONVERSATION, ) } diff --git a/src/bot/features/welcome.ts b/src/bot/features/welcome.ts index 329a959..7d48b28 100644 --- a/src/bot/features/welcome.ts +++ b/src/bot/features/welcome.ts @@ -1,18 +1,18 @@ import { Composer } from 'grammy' import type { Context } from '#root/bot/context.js' import { logHandle } from '#root/bot/helpers/logging.js' -import { GREETING_CONVERSATION } from '#root/bot/conversations/index.js' +import { SEND_CONVERSATION } from '#root/bot/conversations/index.js' const composer = new Composer() const feature = composer.chatType('private') feature.command('start', logHandle('command-start'), (ctx) => { - return ctx.reply(ctx.t('welcome')) + return ctx.reply('welcome /send') }) -feature.command('greeting', logHandle('command-greeting'), (ctx) => { - return ctx.conversation.enter(GREETING_CONVERSATION) +feature.command(SEND_CONVERSATION, logHandle('command-send'), (ctx) => { + return ctx.conversation.enter(SEND_CONVERSATION) }) export { composer as welcomeFeature } diff --git a/src/bot/index.ts b/src/bot/index.ts index ff50bd8..ba1168a 100644 --- a/src/bot/index.ts +++ b/src/bot/index.ts @@ -22,7 +22,7 @@ import { i18n, isMultipleLocales } from '#root/bot/i18n.js' import { updateLogger } from '#root/bot/middlewares/index.js' import { config } from '#root/config.js' import { logger } from '#root/logger.js' -import { greetingConversation } from '#root/bot/conversations/index.js' +import { sendConversation } from '#root/bot/conversations/index.js' interface Options { sessionStorage?: StorageAdapter @@ -36,6 +36,7 @@ export function createBot(token: string, options: Options = {}) { ContextConstructor: createContextConstructor({ logger }), }) const protectedBot = bot.errorBoundary(errorHandler) + const [adminId] = JSON.parse(`${Bun.env.BOT_ADMINS}`) // Middlewares bot.api.config.use(parseMode('HTML')) @@ -54,7 +55,7 @@ export function createBot(token: string, options: Options = {}) { ) protectedBot.use(i18n) protectedBot.use(conversations()) - protectedBot.use(greetingConversation()) + protectedBot.use(sendConversation(adminId)) // Handlers protectedBot.use(welcomeFeature)