简体   繁体   中英

Receiving service messages in a group chat using Telegram Bot

I am trying to create a bot in my group to help me track the group users who have invited other users into the group.

I have disabled the privacy mode so the bot can receive all messages in a group chat. However, it seems to be that update.message only gets messages supplied by other users but not service messages like Alice has added Bob into the group

Is there any way that I can get these service messages as well?

Thanks for helping!

I suppose you are using python-telegram-bot library.

You can add a handler with a specific filter to listen to service messages:

from telegram.ext import MessageHandler, Filters

def callback_func(bot, update):
    # here you receive a list of new members (User Objects) in a single service message
    new_members = update.message.new_chat_members
    # do your stuff here:
    for member in new_members:
        print(member.username)

def main():
    ...
    dispatcher.add_handler(MessageHandler(Filters.status_update.new_chat_members, callback_func)

There are several more service message types your bot may receive using the Filters module, check them out here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM