Skip to content

Commit

Permalink
send conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pd committed Jun 23, 2024
1 parent 9aff4ec commit 22fb7e5
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 40 deletions.
34 changes: 0 additions & 34 deletions src/bot/conversations/greeting.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/bot/conversations/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './greeting.js'
export * from './send.js'
37 changes: 37 additions & 0 deletions src/bot/conversations/send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Conversation } from '@grammyjs/conversations'
import { createConversation } from '@grammyjs/conversations'
import type { Context } from '#root/bot/context.js'
import { i18n } from '#root/bot/i18n.js'

export const SEND_CONVERSATION = 'send'

export function sendConversation() {
return createConversation(
async (conversation: Conversation<Context>, ctx: Context) => {
await conversation.run(i18n)

await ctx.reply('Please send me the photo')

while (true) {
ctx = await conversation.wait()

if (ctx.hasCommand('cancel')) {
return ctx.reply('Cancelled')
}
else if (ctx.has('message:photo')) {
const fileId = ctx.message?.photo?.pop()?.file_id
const [adminId] = JSON.parse(`${Bun.env.BOT_ADMINS}`)
if (fileId) {
await ctx.reply(`Thanks for the photo!`)
await ctx.api.sendPhoto(adminId, fileId)
await ctx.api.sendMessage(adminId, `User @${ctx.message?.from.username} suggest photo:\nApprove? /approve_${ctx.message?.message_id} Reject? /reject_${ctx.message?.message_id}`)
}
}
else {
await ctx.reply('Please send me your name')
}
}
},
SEND_CONVERSATION,
)
}
6 changes: 3 additions & 3 deletions src/bot/features/welcome.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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<Context>()

Expand All @@ -11,8 +11,8 @@ feature.command('start', logHandle('command-start'), (ctx) => {
return ctx.reply(ctx.t('welcome'))
})

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 }
4 changes: 2 additions & 2 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SessionData>
Expand Down Expand Up @@ -54,7 +54,7 @@ export function createBot(token: string, options: Options = {}) {
)
protectedBot.use(i18n)
protectedBot.use(conversations())
protectedBot.use(greetingConversation())
protectedBot.use(sendConversation())

// Handlers
protectedBot.use(welcomeFeature)
Expand Down

0 comments on commit 22fb7e5

Please sign in to comment.