Skip to content

Commit

Permalink
feat(bot): add start and howgoodami commands (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
evermake committed Mar 17, 2024
1 parent 07eaa9e commit 945a7a4
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 27 deletions.
30 changes: 30 additions & 0 deletions backend/src/bot/handlers/commands/howgoodami.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { handler } from '~/bot/handlers'
import filters from '~/bot/filters'

export default handler((composer) => {
composer
.command('howgoodami')
.filter(filters.pm)
.use(async (ctx) => {
const messageId = ctx.message.message_id

await ctx.reply(ctx.t['HowGoodAmI.Thinking'])

ctx.domain.getStudentBetterThanPercent(ctx.user)
.then((percent) => {
ctx.api.sendMessage(
ctx.chat.id,
ctx.t['HowGoodAmI.Answer'](percent),
{ reply_parameters: { message_id: messageId } },
)
})
.catch((err) => {
ctx.logger.error(err)
ctx.api.sendMessage(
ctx.chat.id,
ctx.t['HowGoodAmI.Failed'],
{ reply_parameters: { message_id: messageId } },
)
})
})
})
7 changes: 7 additions & 0 deletions backend/src/bot/handlers/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import start from './start'
import howgoodami from './howgoodami'

export default {
start,
howgoodami,
}
11 changes: 11 additions & 0 deletions backend/src/bot/handlers/commands/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { handler } from '~/bot/handlers'
import filters from '~/bot/filters'

export default handler((composer) => {
composer
.command('start')
.filter(filters.pm)
.use(async (ctx) => {
await ctx.reply(ctx.t.Welcome)
})
})
10 changes: 10 additions & 0 deletions backend/src/bot/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Composer } from 'grammy'
import type { Ctx } from '~/bot/context'

export function handler(registerFn: (composer: Composer<Ctx>) => void): Composer<Ctx> {
const composer = new Composer<Ctx>()
registerFn(composer)
return composer
}

export { default } from './root'
7 changes: 7 additions & 0 deletions backend/src/bot/handlers/root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import commands from './commands'
import { handler } from '.'

export default handler((composer) => {
composer.use(commands.start)
composer.use(commands.howgoodami)
})
29 changes: 2 additions & 27 deletions backend/src/bot/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Bot } from 'grammy'
import plugins from './plugins'
import handlers from './handlers'
import type { Ctx } from './context'
import type { Logger } from '~/lib/logging'
import type { Domain } from '~/domain'
Expand All @@ -21,33 +22,7 @@ export function createBot({
plugins.domain.install(bot, { domain })
plugins.translations.install(bot)

bot.command('start', async (ctx) => {
ctx.reply(`${ctx.t.Welcome}\n\n${JSON.stringify(ctx.user)}`)
})

bot.command('howgoodami', async (ctx) => {
await ctx.api.sendMessage(
ctx.chat.id,
ctx.t['HowGoodAmI.Thinking'],
)

ctx.domain.getStudentBetterThanPercent(ctx.user!)
.then((percent) => {
ctx.api.sendMessage(
ctx.chat.id,
ctx.t['HowGoodAmI.Answer'](percent),
{ reply_parameters: { message_id: ctx.update.message!.message_id } },
)
})
.catch((err) => {
ctx.logger.error(err)
ctx.api.sendMessage(
ctx.chat.id,
ctx.t['HowGoodAmI.Failed'],
{ reply_parameters: { message_id: ctx.update.message!.message_id } },
)
})
})
bot.use(handlers)

return bot
}

0 comments on commit 945a7a4

Please sign in to comment.