Skip to content

Commit

Permalink
feat(telegram): slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Aug 7, 2023
1 parent 953f1c6 commit e4bfc6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
26 changes: 26 additions & 0 deletions adapters/telegram/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,32 @@ export class TelegramBot<T extends TelegramBot.Config = TelegramBot.Config> exte
user.avatar = `${endpoint}/${file.file_path}`
}
}

async updateCommands(commands: Universal.Command[]) {
const languages = this.ctx.i18n.fallback([])

Check failure on line 289 in adapters/telegram/src/bot.ts

View workflow job for this annotation

GitHub Actions / build

Property 'i18n' does not exist on type 'Context'.
const order = languages.filter(v => v.length === 2)
const languageSubset = order.map(v => languages.filter(l => l.startsWith(v)))
type LangCode = string
const result = {} as Record<LangCode, Telegram.BotCommand[]>

Check failure on line 293 in adapters/telegram/src/bot.ts

View workflow job for this annotation

GitHub Actions / build

Property 'i18n' does not exist on type 'Context'.
for (const subset of languageSubset) {
const code6391 = subset.find(v => v.length === 2)
result[code6391] ||= []
for (const cmd of commands) {
const { name, description } = cmd
const cmdDesc = subset.map(lang => description[lang]).filter(v => v)?.[0] ?? cmd.name
result[code6391].push({ command: name, description: cmdDesc })
}
}
for (const lang of Object.keys(result)) {
await this.internal.setMyCommands({
commands: result[lang],
language_code: lang,
})
}
await this.internal.setMyCommands({
commands: commands.map(({ name }) => ({ command: name, description: name })),
})
}
}

TelegramBot.prototype.platform = 'telegram'
Expand Down
9 changes: 8 additions & 1 deletion adapters/telegram/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ 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 (message && !isBotCommand) {
session.type = update.message || update.channel_post ? 'message' : 'message-updated'
await bot.adaptMessage(message, session)
} else if (isBotCommand) {
session.type = 'message'
const p = bot.ctx.root.config.prefix

Check failure on line 55 in adapters/telegram/src/utils.ts

View workflow job for this annotation

GitHub Actions / build

Property 'prefix' does not exist on type 'Config'.

Check failure on line 55 in adapters/telegram/src/utils.ts

View workflow job for this annotation

GitHub Actions / build

Property 'prefix' does not exist on type 'Config'.
const prefix = (Array.isArray(p) ? p[0] : p) ?? ''
await bot.adaptMessage(message, session)
session.content = prefix + session.content.slice(1)
} else if (update.chat_join_request) {
session.timestamp = update.chat_join_request.date * 1000
session.type = 'guild-member-request'
Expand Down

0 comments on commit e4bfc6a

Please sign in to comment.