Search code examples
pythontelegram-botpython-telegram-bot

get member information from group in python-telegram-bot


i am working on part of the telegram bot coding with python :

def ok(bot,update):
    print update.message


handler = MessageHandler(Filters.all,ok)
updater.dispatcher.add_handler(handler)

updater.start_polling()
updater.idle()

when i was sent a pm directly to my bot i can print member username with:

print update.message["chat"]["username"]

result of :

print update.message

when sent directly message to robot :

{'delete_chat_photo': False, 'new_chat_photo': [], 'from': {'username': u'un_detectable', 'first_name': u'Sina \U0001f3a9', 'is_bot': False, 'id': 207541477, 'language_code': u'en-US'}, 'text': u'salaaam', 'entities': [], 'channel_chat_created': False, 'new_chat_members': [], 'supergroup_chat_created': False, 'chat': {'username': u'un_detectable', 'first_name': u'Sina \U0001f3a9', 'type': u'private', 'id': 207541477}, 'photo': [], 'date': 1505902164, 'group_chat_created': False, 'message_id': 297, 'new_chat_member': None}

and in chat field username is exist and as i said can be print

but when i was sent a pm in group ( my bot perm is admin ) i cant access to member username

result of :

print update.message

when sent a message in chat group :

{'delete_chat_photo': False, 'new_chat_photo': [], 'from': {'username': u'un_detectable', 'first_name': u'Sina \U0001f3a9', 'is_bot': False, 'id': 207541477, 'language_code': u'en-US'}, 'text': u'salaaaam', 'entities': [], 'channel_chat_created': False, 'new_chat_members': [], 'supergroup_chat_created': False, 'chat': {'type': u'supergroup', 'id': -1001139540291L, 'title': u'test'}, 'photo': [], 'date': 1505902183, 'group_chat_created': False, 'message_id': 201, 'new_chat_member': None}

and in chat field username is dosent exist i want to when one of the memebrs wrote the message in group , save my bot him message + username in database but i cant access to username of the group members ! can someone help me?


Solution

  • In private message, chat is equal to user, that's why you can see username of the chat as well as the from_user field.

    In a group, chat refers to the group, and from_user refers to the user who sent out the message. If you want to log the usernames of the users, you should find them in update.message.from_user.username.