Skip to content

Commit

Permalink
feat(telegram): support slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 20, 2023
2 parents 0802652 + e4bfc6a commit d1e090d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
29 changes: 29 additions & 0 deletions adapters/telegram/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,35 @@ export class TelegramBot<T extends TelegramBot.Config = TelegramBot.Config> exte
avatar: url,
}
}

async updateCommands(commands: Universal.Command[]) {
const result = {} as Record<string, Telegram.BotCommand[]>
for (const cmd of commands) {
const { name, description } = cmd
const languages = {} as Record<string, string>
for (const locale in description) {
if (!locale || !description[locale]) continue
const lang = locale.slice(0, 2)
languages[lang] ||= description[locale]
}
for (const lang in languages) {
result[lang] ??= []
result[lang].push({ command: name, description: languages[lang] })
}
}
for (const lang in result) {
await this.internal.setMyCommands({
commands: result[lang],
language_code: lang,
})
}
await this.internal.setMyCommands({
commands: commands.map(({ name, description }) => ({
command: name,
description: description[''] || name,
})),
})
}
}

export namespace TelegramBot {
Expand Down
7 changes: 6 additions & 1 deletion adapters/telegram/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ export async function handleUpdate(update: Telegram.Update, bot: TelegramBot) {
Object.assign(session.telegram, update)

const message = update.message || update.edited_message || update.channel_post || update.edited_channel_post
if (message) {
const isBotCommand = update.message && update.message.entities?.[0].type === 'bot_command'
if (isBotCommand) {
session.type = 'interaction/command'
await bot.adaptMessage(message, session)
session.content = session.content.slice(1)
} else if (message) {
session.type = update.message || update.channel_post ? 'message' : 'message-updated'
await bot.adaptMessage(message, session)
} else if (update.chat_join_request) {
Expand Down

0 comments on commit d1e090d

Please sign in to comment.