skip to Main Content

I am trying to return the user id of new members who join a telegram group, I can get the JSON data but I cannot retrieve values from a level deeper:

code:

def new_chat_members(self, bot, update):
        print(update)
        print(update['message'])
        print(update['message']['from'])
        print(update['message'].get('from'))

So the first print(update) returns me data, the second print(update['message']['from']) returns me data too, however when I want to return the third print, I get a return value of none. The last print returns me an error.

The type of the first and second print is:

<class 'telegram.update.Update'>
<class 'telegram.message.Message'>

Output for the first print:

{'update_id': 130834599, 'message': {'message_id': 193, 'date': 1574900654, 'chat': {'id': -1001323635373, 'type': 'supergroup', 'title': 'testbot'}, 'entities': [], 'caption_entities': [], 'photo': [], 'new_chat_members': [{'id': 677494285, 'first_name': 'Mahdi', 'is_bot': False, 'last_name': 'Khan', 'username': 'Samstestacc', 'language_code': 'en'}], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 677494285, 'first_name': 'Mahdi', 'is_bot': False, 'last_name': 'Khan', 'username': 'Samstestacc', 'language_code': 'en'}}, '_effective_message': {'message_id': 193, 'date': 1574900654, 'chat': {'id': -1001323635373, 'type': 'supergroup', 'title': 'testbot'}, 'entities': [], 'caption_entities': [], 'photo': [], 'new_chat_members': [{'id': 677494285, 'first_name': 'Mahdi', 'is_bot': False, 'last_name': 'Khan', 'username': 'Samstestacc', 'language_code': 'en'}], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 677494285, 'first_name': 'Mahdi', 'is_bot': False, 'last_name': 'Khan', 'username': 'Samstestacc', 'language_code': 'en'}}}

Output for the second print:

{'message_id': 188, 'date': 1574900008, 'chat': {'id': -1001323635373, 'type': 'supergroup', 'title': 'testbot'}, 'entities': [], 'caption_entities': [], 'photo': [], 'new_chat_members': [{'id': 677494285, 'first_name': 'Mahdi', 'is_bot': False, 'last_name': 'Khan', 'username': 'Samstestacc', 'language_code': 'en'}], 'new_chat_photo': [], 'delete_chat_photo': False, 'group_chat_created': False, 'supergroup_chat_created': False, 'channel_chat_created': False, 'from': {'id': 677494285, 'first_name': 'Mahdi', 'is_bot': False, 'last_name': 'Khan', 'username': 'Samstestacc', 'language_code': 'en'}}

Could someone help me fix this and reply why I am getting a none output. Thanks

2

Answers


  1. You can either extend dict in your telegram.update.Update and telegram.update.Message classes, something like:

    Make sure you use the dict syntax to add your properties…

    
    class Update(dict):
        def __init__(self, message):
            self['message'] = message
    
    

    or you can implement a get method yourself:

    
    class Update:
        def get(self, key):
            if hasattr(self, key):
                return getattr(self, key)
    
    
    Login or Signup to reply.
  2. As telegram source here ,there is a to_dict method,which you can get the dictionary from the Message class.I think this is what you can do.

    def new_chat_members(self, bot, update):
    
            message_dictionary = update['message'].to_dict()
            print(message_dictionary.get('from'))
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search