Search code examples
iosclientinlinetelegramtelegram-bot

Telegram (simplest) inline bot send photo 2 times on iOS


I have an inline bot similar to @pic and it works ok, when I type query word I can see pictures appear so I can choose one and send to a chat. The problem is when I do - 2 copies of same result are sent. This happens only on iOS client. On Android, PC and other platforms only one picture is being sent. I have checked all logs (the bot is done in Node.js) and for every request I have a single response. It seems to be a iOS client bug, although @pic bot works fine. Has someone encountered this bug or have an idea of what can cause it?

Example of answerInlineQuery response object

{
"inline_query_id": "817150058382989968",
"results": [
    {
        "type": "photo",
        "id": "se090",
        "photo_url": "http://www.shadowera.com/secardbot361/se090.jpg",
        "thumb_url": "http://www.shadowera.com/secardbot361/se090.jpg",
        "photo_width": 344,
        "photo_height": 480,
        "title": "Tracking Gear",
        "description": "You can view the hands of opposing players.",
        "caption": "king"
    },
    {...

UPDATE: So I have created a simplest possible inline bot in node.js @iosinlinebot (you can try it) AND you have the same exact behaviour: only on iOS devices you will send 2 images to the chat once tapped on the result. Here is the code:

exports.handler = function(event, context) {
        console.log(event);
        const https = require("https");
        let answer = {
            inline_query_id: event.inline_query.id,
            results: [{
                    type: "photo",
                    id: "abcd",
                    photo_url: "https://lh3.googleusercontent.com/jVXglyWWL5J2y1vRN-7Jy3_ozvvZc4w5486IAkbAIrWcNN_vn7YuIvhc1JDtGq43BqGl=s180",
                    thumb_url: "https://lh3.googleusercontent.com/jVXglyWWL5J2y1vRN-7Jy3_ozvvZc4w5486IAkbAIrWcNN_vn7YuIvhc1JDtGq43BqGl=s180",
                    photo_width: 180,
                    photo_height: 180,
                    title: "title",
                    description: "description",
                    caption: "test"
                }],
            cache_time:1 
        };
        let postBody = JSON.stringify(answer);
        let options = {
            hostname: "api.telegram.org",
            port: 443,
            path: "/bot" + process.env.TOKEN + "/answerInlineQuery",
            method: "POST",
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': postBody.length
            }
        };

        let postreq = https.request(options, (res) => {
            res.setEncoding('utf8');
            const body = [];
            res.on('data', (chunk) => body.push(chunk));

            res.on('end', () => {
                let j = body.join('');
                console.log(j);
                //context.done(JSON.parse(j));
            });
        });
        postreq.write(postBody);
        postreq.end();
};

this is an event object (coming from telegram):

{
  "update_id": 12345678,
  "inline_query": {
    "id": "123456789123456789",
    "from": {
      "id": 123456789,
      "is_bot": false,
      "first_name": "Firstname",
      "username": "username",
      "language_code": "it-IT"
    },
    "query": "test",
    "offset": ""
  }
}

UPDATE: Thanks to Sedric Heidarizarei we were able to find the problem. It is a telegram iOS client bug. If InlineQueryResultPhoto object contains caption field, you user will post 2 images to the chat.


Solution

  • It is very important to close the Begin and the End of your regex with ^ and $.
    For example a user with this regex /^[/]start/ can use start and start a and start b as Bot command And will allow Them to receive your photo, But with /^[/]start$/, The user must enter the exact /start Command.

    1: Use This Module: node-telegram-bot-api
    2: And Send Your Photo:

    bot.onText(/^[/]start$/, (msg) => {
      const opts = {
        parse_mode: 'Markdown',
        reply_markup: {
            inline_keyboard: [[{
                    text: '🔙',
                    callback_data: 'back'
                    }]]
            }
      };
      bot.sendPhoto(msg.chat.id, 'AgADBAADn64xBoABCx8L8trMV9eMqgDAAEC', opts); // Your Photo id
    }); 
    

    Notice:
    Open an empty project and just use and check your InlineQueryResultPhoto.

    update:
    That is a Telegram bug for For temporary use, remove caption from your let answer ={}