Search code examples
telegram-botphp-telegram-bottelegram-webhook

api Telegram webhook : how can delete new_chat_participant message


I created a bot via webhook method of api telegram and It's okay and working.

but I want know how can delete any new_chat_participant messages before sending message by members.

You know that telegram don't send request to your hook url until have not any message with members !!!!!!!

I need just message_id for example when a member add an another to supper group.

enter image description here


Solution

  • Set /setprivacy to disable in @BotFather to receive the whole actions and messages from your group. (In order to delete these messages, the bot must has access to messages in the group, needs to be administrator).

    When new member has been added to group, you will receive a json in your webhook something like this:

    { update_id: 123123123,
      message:
        { message_id: 2599,         // Pay attention to this message id
          from: {
            ...
          },
          chat: {
            id: -987372183          // This is your group's id
            ...
          },
          date: 1582378239,
          new_chat_participant: {        // Field when new member is added to group
    
          }
          ...
    }
    

    Now you need to send a post request to Telegram to delete this message (action). The request is:

    request.post("https://api.telegram.org/botYOUR_BOT_TOKEN/deleteMessage?chat_id=GROUP_ID&message_id=MESSAGE_ID_RECEIVED", ... )

    Hope this helps.