How to receive messages in group chats using telegram bot api

后端 未结 5 468
一向
一向 2020-12-14 14:44

My telegram bot receives messages sent by user to my bot in private chats but not receives messages sent by users in group chats. Any options/api for getting group chat mess

相关标签:
5条回答
  • 2020-12-14 15:27

    Sequence within a BotFather chat:

    You: /setprivacy

    BotFather: Choose a bot to change group messages settings.

    You: @your_name_bot

    BotFather: 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username.

    'Disable' - your bot will receive all messages that people send to groups.

    Current status is: ENABLED

    You: Disable

    BotFather: Success! The new status is: DISABLED. /help

    0 讨论(0)
  • 2020-12-14 15:32

    You can access all avaliable settings from all of your bots by sending /mybots to Botfather. Choose the bot, then Bot Settings and Group Privacy. If its disable (default), you can tap on Turn off.

    Now its possible to receive the chat history using GetUpdates. This can be done via HTTP API or the frameworks. For example, in C# (.NET Core) like this:

    var bot = new TelegramBotClient(ApiToken);
    var updates = bot.GetUpdatesAsync().Result;
    foreach(var update in updates) {
        Console.WriteLine($"{update.ChannelPost.Date} {update.ChannelPost.Text}");
    }
    

    But keep in mind that this feature has some kind of perfect forward secrecy implemented. So you only get messages that were send after group privacy is disabled. As a result, the GetUpdates result is empty until some post was made.

    0 讨论(0)
  • 2020-12-14 15:35

    By default A Bot will receive only messages addressed to it by any user directly via posting by /command@YourBot any message you send. After that it vill be available via getUpdates API call. In browser it will be:

    https://api.telegram.org/botToken/getupdates
    

    Find the related message in output JSON and grab chatId. It will allow you to answer back with:

    https://api.telegram.org/botToken/sendmessage?chat_id=123456788&text=My Answer
    
    0 讨论(0)
  • 2020-12-14 15:41

    Make your bot by admin in group.

    0 讨论(0)
  • 2020-12-14 15:46

    Talk to @botfather and disable the privacy mode.

    0 讨论(0)
提交回复
热议问题