Skip to content

Commit

Permalink
fix(telegram): check valid command name, fix koishijs/koishi#1433
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 13, 2024
1 parent 91414cc commit 7c603ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 9 additions & 3 deletions adapters/telegram/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ export class TelegramBot<C extends Context = Context, T extends TelegramBot.Conf

async updateCommands(commands: Universal.Command[]) {
if (!this.config.slash) return
commands = commands.filter((command) => {
// https://core.telegram.org/bots/api#botcommand
return /^[\w-]{1,32}$/g.test(command.name)
})
const result = {} as Record<string, Telegram.BotCommand[]>
for (const cmd of commands) {
const { name, description } = cmd
Expand All @@ -211,8 +215,10 @@ export class TelegramBot<C extends Context = Context, T extends TelegramBot.Conf
languages[lang] ||= description[locale]
}
for (const lang in languages) {
result[lang] ??= []
result[lang].push({ command: name, description: languages[lang] })
(result[lang] ??= []).push({
command: name.toLowerCase().replace(/[^\w]/g, '_'),
description: languages[lang],
})
}
}
for (const lang in result) {
Expand All @@ -223,7 +229,7 @@ export class TelegramBot<C extends Context = Context, T extends TelegramBot.Conf
}
await this.internal.setMyCommands({
commands: commands.map(({ name, description }) => ({
command: name,
command: name.toLowerCase().replace(/[^\w]/g, '_'),
description: description[''] || name,
})),
})
Expand Down
4 changes: 3 additions & 1 deletion adapters/telegram/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export async function handleUpdate(update: Telegram.Update, bot: TelegramBot) {
session.content = command + session.content.slice(group.length)
} else if (message) {
if (update.message?.media_group_id) {
if (!mediaGroupMap.has(update.message.media_group_id)) { mediaGroupMap.set(update.message.media_group_id, [new Date(), []]) }
if (!mediaGroupMap.has(update.message.media_group_id)) {
mediaGroupMap.set(update.message.media_group_id, [new Date(), []])
}

const [, updates] = mediaGroupMap.get(update.message.media_group_id)
session.type = update.message || update.channel_post ? 'message' : 'message-updated'
Expand Down

0 comments on commit 7c603ae

Please sign in to comment.