Skip to content
Advertisement

Creating a Welcome Bot Using python-telegram-bot

I have been moderating a telegram group for some time and I have had no issues using the python-telegram-bot package. I actually love it. However, I can’t seem to get a functioning “Welcome Message” for when new users join.

Right now, I have tried structuring the function like I do with my command and message handlers:

def welcome(update,context):
    #name = from_user.username
    #update.message.reply_text("""Welcome blah blah blah to the group!""") 

and then calling it it my main function like so:

dp = updater.dispatcher
dp.add_handler(ChatMemberHandler(welcome, ChatMemberHandler.CHAT_MEMBER))
updater.start_polling(allowed_updates=Update.ALL_TYPES)
updater.idle()

But am getting an error: AttributeError: ‘NoneType’ object has no attribute ‘reply_text’

So I guess the “member joined the group” pop up is not considered a message. But I dont know how to extract that event or which handler to use. Any help would be greatly appreciated! Thank you!

Advertisement

Answer

As thethiny already pointed out, chatmember updates have no associated message: update.message will be None, while update.chat_member will be an instance of ChatMemberUpdated. Note that Message.reply_text is just a shortcut for Bot.send_message(chat_id=message.chat.id, ...), so as long as you have the chat_id you can just use e.g. context.bot.send_message – and you can get that chat_id from ChatMemberUpdated.chat. In fact, you can still use PTBs shortcuts, e.g. update.effective_chat.send_message.

Please check out the docs of

as well as the chatmemberbot.py example provided by PTB.


Disclaimer: I’m currently the maintainer of python-telegram-bot

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement