Skip to content

Commit

Permalink
admin conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pd committed Jun 24, 2024
1 parent acba14b commit edaecac
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
Binary file modified bun.lockb
Binary file not shown.
20 changes: 20 additions & 0 deletions src/bot/conversations/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Conversation } from '@grammyjs/conversations'
import { createConversation } from '@grammyjs/conversations'
import type { Context } from '#root/bot/context.js'

export const ADMIN_CONVERSATION = 'approve'

export function adminConversation(channelId: string) {
return createConversation(
async (conversation: Conversation<Context>, ctx: Context) => {
await ctx.reply('Send photo id')
const { message } = await conversation.waitFor('message:text')
const fileId = message.text

if (fileId) {
await ctx.api.sendPhoto(channelId, fileId)
}
},
ADMIN_CONVERSATION,
)
}
1 change: 1 addition & 0 deletions src/bot/conversations/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './send.js'
export * from './admin.js'
45 changes: 25 additions & 20 deletions src/bot/conversations/send.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
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(adminId: string) {
return createConversation(
async (conversation: Conversation<Context>, ctx: Context) => {
await conversation.run(i18n)

const replyMsg = 'Please send me the photo!';
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: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(replyMsg)
}
const photoMessage = await conversation.waitFor('message:photo')
const fileId = photoMessage.message.photo?.pop()?.file_id

if (fileId) {
await ctx.reply(`Thanks for the photo!`)

await conversation.external(() => ctx.api.sendPhoto(
adminId,
fileId,
{ caption: `@${ctx.from?.username || ctx.from?.first_name} /approve` },
))

await conversation.external(() => ctx.api.sendMessage(
adminId,
fileId,
))

await conversation.external(() => ctx.api.sendMessage(
adminId,
'--',
))
}
else {
await ctx.reply(replyMsg)
}
},
SEND_CONVERSATION,
Expand Down
5 changes: 5 additions & 0 deletions src/bot/features/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Context } from '#root/bot/context.js'
import { isAdmin } from '#root/bot/filters/index.js'
import { setCommandsHandler } from '#root/bot/handlers/index.js'
import { logHandle } from '#root/bot/helpers/logging.js'
import { ADMIN_CONVERSATION } from '#root/bot/conversations/index.js'

const composer = new Composer<Context>()

Expand All @@ -16,4 +17,8 @@ feature.command(
setCommandsHandler,
)

feature.command(ADMIN_CONVERSATION, logHandle('command-admin'), (ctx) => {
return ctx.conversation.enter(ADMIN_CONVERSATION)
})

export { composer as adminFeature }
3 changes: 3 additions & 0 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { updateLogger } from '#root/bot/middlewares/index.js'
import { config } from '#root/config.js'
import { logger } from '#root/logger.js'
import { sendConversation } from '#root/bot/conversations/index.js'
import { adminConversation } from '#root/bot/conversations/admin.js'

interface Options {
sessionStorage?: StorageAdapter<SessionData>
Expand All @@ -37,6 +38,7 @@ export function createBot(token: string, options: Options = {}) {
})
const protectedBot = bot.errorBoundary(errorHandler)
const [adminId] = JSON.parse(`${Bun.env.BOT_ADMINS}`)
const channelId = `${Bun.env.BOT_CHANNEL_ID}`

// Middlewares
bot.api.config.use(parseMode('HTML'))
Expand All @@ -56,6 +58,7 @@ export function createBot(token: string, options: Options = {}) {
protectedBot.use(i18n)
protectedBot.use(conversations())
protectedBot.use(sendConversation(adminId))
protectedBot.use(adminConversation(channelId))

// Handlers
protectedBot.use(welcomeFeature)
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import z from 'zod'
import { parseEnv, port } from 'znv'
import { API_CONSTANTS } from 'grammy'
import Bun from 'bun';
import Bun from 'bun'

function createConfigFromEnvironment(environment: NodeJS.ProcessEnv) {
const config = parseEnv(environment, {
Expand Down

0 comments on commit edaecac

Please sign in to comment.