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 * Whether the results should be displayed as a gallery instead
* of a vertical list. Only applicable to some media types. * 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 gallery?: boolean
@ -94,16 +97,14 @@ export async function answerInlineQuery(
): Promise<void> { ): Promise<void> {
if (!params) params = {} if (!params) params = {}
const tlResults = await Promise.all( const [gallery, tlResults] = await BotInline._convertToTl(this, results, params!.parseMode)
results.map((it) => BotInline._convertToTl(this, it, params!.parseMode))
)
await this.call({ await this.call({
_: 'messages.setInlineBotResults', _: 'messages.setInlineBotResults',
queryId, queryId,
results: tlResults, results: tlResults,
cacheTime: params.cacheTime ?? 300, cacheTime: params.cacheTime ?? 300,
gallery: params.gallery ?? true, gallery: params.gallery ?? gallery,
private: params.private, private: params.private,
nextOffset: params.nextOffset, nextOffset: params.nextOffset,
switchPm: params.switchPm switchPm: params.switchPm

View file

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