Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(telegram): slash command, close #137 #142

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions adapters/telegram/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
const { mime, data } = await this.$getFile(filePath)
const base64 = `data:${mime};base64,` + arrayBufferToBase64(data)
return { url: base64 }
}

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

View workflow job for this annotation

GitHub Actions / build

Property 'mime' does not exist on type 'Promise<FileTypeResult>'.

private async setAvatarUrl(user: Universal.User) {
const { photos: [avatar] } = await this.internal.getUserProfilePhotos({ user_id: +user.userId })
Expand All @@ -284,6 +284,32 @@
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 @@
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
Loading