skip to Main Content

In Telegram, when the user presses a button, the bot receives this information in JSON:

'update':
{
    'callback_query':
    {
        'from':
        {
            'id':420220883
        }
    }
}

There are more information in this JSON object, but I’m only interested in the id part.

Now when I want to access it like update.callback_query.from.id, it obviously gives an SyntaxError error, since from is a Python keyword.

So my quetion is; How can I go about using this information?

Note that I have tried using it like a dictionary update['callback_query']['from']['id'], but it gives an odd-looking error.

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    I suppose my question is duplicate!

    The problem has been fixed by the author of the library. I should've simply changed from to from_user!

    I found the answer here.


  2. The data structure you have posted is not a valid JSON nor a valid dict. After converting it to a valid dict you can easily get the ‘id’

    data = {'update':
        {
            'callback_query':
                {
                    'from':
                        {
                            'id': 420220883
                        }
                }
        }}
    
    print(data['update']['callback_query']['from']['id'])
    

    Output

    420220883
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search