Why Telegram bot answerInlineQuery response Bad Request: STICKER_DOCUMENT_INVALID?
Asked Answered
S

2

6

Trying to answer inline query with the sticker cause error 400 Bad Request: STICKER_DOCUMENT_INVALID.

My response looks like following

const sticker = {
  type: 'sticker',
  id: '0',
  sticker_file_id: 'file_id_obtained_from_upload_sticker_method'
};

ctx.telegram.answerInlineQuery(query.id, [sticker]);

In according to Telegram documentation here and Telegraf documentation here everything seems to be right. In reality:

{
  code: 400,
  response: {
    ok: false,
    error_code: 400,
    description: 'Bad Request: STICKER_DOCUMENT_INVALID'
  },
  description: 'Bad Request: STICKER_DOCUMENT_INVALID',
  parameters: {},
  on: {
    method: 'answerInlineQuery',
    payload: { inline_query_id: '3997901566750938248', results: [Array] }
  }
}

No answer in google nor official documentation. Anyone has similar experience?

The sticker was previously successfully uploaded with method uploadStickerFile provided url to request image file.

Septavalent answered 19/4, 2020 at 9:33 Comment(3)
did you try with sticker_id that stored in sticker set in telegram, maybe it just answer with a sticker in telegram sticker set. As uploadstickerfile in Telegram official doc "Use this method to upload a .PNG file with a sticker for later use in createNewStickerSet and addStickerToSet methods"Devault
good point and seems logic. I tried two cases: A: 1. upload png -> recieve file_id. 2. answer directly without adding to set (unsuccessfull). 3. add to set by id (successfull). B: 1 upload png -> recieve file_id. 2. add sticker to set (successfull). 3 answer with sticker file id (unsuccessfull)...Septavalent
I have the same issue. Did you manage to figure out how to solve it?Meenen
D
0

Upload png and add it to sticker just return you a file_id for png file. but you have to use sendSticker method or fetch the .WEBP in sticker set with getStickerSet method and get file_unique_id. Take a look at Sticker in Telegram Doc and difference between file_id and file_unique_id.

Devault answered 6/5, 2020 at 14:26 Comment(1)
previously used method uploadStickerFile got me file_unique_id as well, but when I use it in answerInlineQuery as sticker_id I got different but error TelegramError: 400: Bad Request: wrong remote file identifier specified: can't unserialize it. Wrong last symbolSeptavalent
F
0

uploadStickerFile technically still uploads regular files, not stickers, for a file to become a sticker, you need to upload it to a stickerset using createNewStickerSet or addStickerToSet, then get a new file_id (stickers in stickerSet have a new file_id) using getStickerSet, and then specify the new file_id in InlineQueryResultCachedSticker.

Here is a sample code using telegraf:

import { Telegraf, Input } from "telegraf";

const bot = new Telegraf("TOKEN");

let stickerId = null;

bot.command('start', async (ctx) => {
  await ctx.telegram.sendMessage(
    ctx.message.chat.id,
    `Hello, first you need to create sticker using /create, then use @${bot.botInfo.username} to test InlineQueryResultCachedSticker`
  );
});

bot.command('create', async (ctx) => {
  const stickerSetName = `test_by_${bot.botInfo.username}`;

  // Upload file
  const file = await ctx.uploadStickerFile(
    Input.fromLocalFile("./sticker.webp", "sticker.webp"),
    "static"
  );

  // Add file to stickerSet
  await ctx.telegram.createNewStickerSet(ctx.from.id, stickerSetName, "Test", {
    stickers: [{ sticker: file.file_id, emoji_list: ["🙂"] }],
    sticker_type: "regular",
    sticker_format: "static",
  });

  // Get stickerSet
  const stickerSet = await ctx.telegram.getStickerSet(stickerSetName);

  // Save new file_id
  stickerId = stickerSet.stickers[0].file_id;

  await ctx.telegram.sendMessage(
    ctx.message.chat.id,
    `Now try @${bot.botInfo.username}`
  );
});

bot.on("inline_query", async (ctx) => {
  if (stickerId === null) return;

  // Return new file_id from stickerSet
  await ctx.telegram.answerInlineQuery(ctx.inlineQuery.id, [
    { type: "sticker", id: "0", sticker_file_id: stickerId },
  ]);
});

bot.launch();

process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));

To make the code work replace TOKEN with the bot token and put sticker.webp in the bot folder. The sticker must meet the requirements of Telegram.

Finedrawn answered 9/8 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.