fix(client): automatically derive gallery for inline result

This commit is contained in:
teidesu 2022-07-13 05:09:58 +03:00
parent 2c6238798f
commit 42c5d29167
2 changed files with 290 additions and 264 deletions

View file

@ -28,7 +28,10 @@ export async function answerInlineQuery(
* Whether the results should be displayed as a gallery instead
* of a vertical list. Only applicable to some media types.
*
* Defaults to `true`
* In some cases changing this may lead to the results not being
* displayed by the client.
*
* Default is derived automatically based on result types
*/
gallery?: boolean
@ -94,16 +97,14 @@ export async function answerInlineQuery(
): Promise<void> {
if (!params) params = {}
const tlResults = await Promise.all(
results.map((it) => BotInline._convertToTl(this, it, params!.parseMode))
)
const [gallery, tlResults] = await BotInline._convertToTl(this, results, params!.parseMode)
await this.call({
_: 'messages.setInlineBotResults',
queryId,
results: tlResults,
cacheTime: params.cacheTime ?? 300,
gallery: params.gallery ?? true,
gallery: params.gallery ?? gallery,
private: params.private,
nextOffset: params.nextOffset,
switchPm: params.switchPm

View file

@ -714,9 +714,9 @@ export namespace BotInline {
/** @internal */
export async function _convertToTl(
client: TelegramClient,
obj: InputInlineResult,
results: InputInlineResult[],
parseMode?: string | null
): Promise<tl.TypeInputBotInlineResult> {
): Promise<[boolean, tl.TypeInputBotInlineResult[]]> {
const normalizeThumb = (
obj: InputInlineResult,
fallback?: string
@ -748,7 +748,16 @@ export namespace BotInline {
}
}
if (obj.type === 'article') {
const items: tl.TypeInputBotInlineResult[] = []
let isGallery = false
let forceVertical = false
for (const obj of results) {
switch (obj.type) {
case 'article': {
forceVertical = true
let sendMessage: tl.TypeInputBotInlineMessage
if (obj.message) {
sendMessage = await BotInlineMessage._convertToTl(
@ -786,7 +795,7 @@ export namespace BotInline {
}
}
return {
items.push({
_: 'inputBotInlineResult',
id: obj.id,
type: obj.type,
@ -808,10 +817,10 @@ export namespace BotInline {
? normalizeThumb(obj)
: obj.thumb,
sendMessage,
})
continue
}
}
if (obj.type === 'game') {
case 'game': {
let sendMessage: tl.TypeInputBotInlineMessage
if (obj.message) {
sendMessage = await BotInlineMessage._convertToTl(
@ -830,12 +839,23 @@ export namespace BotInline {
}
}
return {
items.push({
_: 'inputBotInlineResultGame',
id: obj.id,
shortName: obj.shortName,
sendMessage,
})
continue
}
case 'gif':
case 'photo':
case 'sticker':
isGallery = true
break
case 'audio':
case 'contact':
case 'voice':
forceVertical = true
}
let sendMessage: tl.TypeInputBotInlineMessage
@ -1018,7 +1038,7 @@ export namespace BotInline {
}
if (!media || media._ === 'inputWebDocument') {
return {
items.push({
_: 'inputBotInlineResult',
id: obj.id,
type: obj.type,
@ -1027,20 +1047,22 @@ export namespace BotInline {
content: media,
thumb: normalizeThumb(obj, media?.url),
sendMessage,
}
})
continue
}
if (media._ === 'inputPhoto') {
return {
items.push({
_: 'inputBotInlineResultPhoto',
id: obj.id,
type: obj.type,
photo: media,
sendMessage,
}
})
continue
}
return {
items.push({
_: 'inputBotInlineResultDocument',
id: obj.id,
type: obj.type,
@ -1048,6 +1070,9 @@ export namespace BotInline {
description,
document: media,
sendMessage,
})
}
return [isGallery && !forceVertical, items]
}
}