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 acba14b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ data/
*.db-journal

.DS_Store
.vercel
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@grammyjs/parse-mode": "1.10.0",
"@grammyjs/types": "3.8.0",
"@hono/node-server": "1.11.2",
"bun": "^1.1.16",
"callback-data": "1.1.1",
"grammy": "1.24.1",
"grammy-guard": "0.5.0",
Expand Down
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'
23 changes: 13 additions & 10 deletions src/bot/conversations/greeting.ts → src/bot/conversations/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,35 @@ 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<Context>, ctx: Context) => {
await conversation.run(i18n)

await ctx.reply('Please send me your name')
const replyMsg = 'Please send me the photo!';

await ctx.reply(replyMsg)

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

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(replyMsg)
}
}
},
GREETING_CONVERSATION,
SEND_CONVERSATION,
)
}
8 changes: 4 additions & 4 deletions src/bot/features/welcome.ts
Original file line number Diff line number Diff line change
@@ -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<Context>()

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 }
5 changes: 3 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 All @@ -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'))
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import z from 'zod'
import { parseEnv, port } from 'znv'
import { API_CONSTANTS } from 'grammy'
import Bun from 'bun';

function createConfigFromEnvironment(environment: NodeJS.ProcessEnv) {
const config = parseEnv(environment, {
Expand Down Expand Up @@ -43,8 +44,8 @@ function createConfigFromEnvironment(environment: NodeJS.ProcessEnv) {

return {
...config,
isDev: Bun.env.NODE_ENV === 'development',
isProd: Bun.env.NODE_ENV === 'production',
isDev: environment.NODE_ENV === 'development',
isProd: environment.NODE_ENV === 'production',
}
}

Expand Down

0 comments on commit acba14b

Please sign in to comment.