Get Reply Message from Telegram Bot API
Asked Answered
H

4

8

I'm developing a Bot that I want users to call in a reply to a previous message. So you would reply to a message with the bot command.

For Example
User 1: Hello World
User 2: (Reply to Hello World) /command test message

Right now I am only able to grab the text sent directly in the command ("test message"), but not the first message ("Hello World"). According to their documentation, I should be able to get it from reply_to_message. However, all I'm seeing in my logs from the webhook is this.

event: {
  body: {
    update_id: 5632431,
    message: {
      message_id: 43,
      from: {
        id: < my_user_id > ,
        first_name: 'User 2',
        username: 'user_2_username',
        language_code: 'en'
      },
      chat: {
        id: < chat_id > ,
        title: < chat_name > ,
        type: 'group',
        all_members_are_administrators: true
      },
      date: 1498342725,
      text: '/command test message',
      entities: [{
        type: 'bot_command',
        offset: 0,
        length: 5
      }]
    }
  }
}

Am I doing something wrong? Anyone have experience getting a reply message? Any help would be appreciated.

Hibbitts answered 24/6, 2017 at 23:7 Comment(0)
L
4

Goto @BotFather, and turn off privacy mode

/setprivacy — Set which messages your bot will receive when added to a group. With privacy mode disabled, the bot will receive all messages.

You might need re-add your bot to group after set this.

Ladylike answered 25/6, 2017 at 0:3 Comment(1)
This seems like a nuclear option when you're only interested in the reply portion thoughGolightly
O
0

When a bot is in privacy mode then it will only receive (copied from docs):

  • Messages that start with a slash ‘/’ (see Commands above)
  • Replies to the bot's own messages
  • Service messages (people added or removed from the group, etc.)
  • Messages from channels where it's a member

This does not include messages that were replied to.

Privacy mode is enabled by default for all bots, except bots that were added to the group as admins (bot admins always receive all messages).

So the only way to get messages being replied to is to disable privacy mode as Sean suggested.

Obscure answered 17/4, 2020 at 8:59 Comment(0)
F
0

I was dealing with the same problem recently, two things worked for me:

1 - Disable Privacy Mode and, 2 - Using @botname for mentions (no /botname), that way I was able to get reply_to_message field.

Fustanella answered 30/11, 2020 at 20:43 Comment(0)
F
-2

Sharing my code; try this, it should succeed.

$chatID = $this->getChatID();

$sendto = API_URL . "sendmessage?chat_id=" . $chatID . "&text=" . urlencode($msg) . "&reply_to_message_id=" . $messageID;
        file_get_contents($sendto);
Flagella answered 23/5, 2018 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.