What's the structure of Telegram's updates

2019-08-04 23:05发布

I'm trying to program a Telegram bot using webhook in a Flask app with telepot, in PythonAnywhere. And so, I want to know what's the structure of the updates comming from Telegram, so as to know what's there and how is it called, and use it in the bot, essentially.

I've tried to log the message it receives to the console (though I'm not sure where the console should be on PythonAnywhere), and also to write in a file in the same server, through python, but that's not working either.

#This that seemed easy didn't work either in the Flask web app
with open('log.txt', 'a') as f:
    f.write('Is this working?')

It feels like I'm missing some easy information everyone takes for granted, but I can't figure out what that is.

1条回答
戒情不戒烟
2楼-- · 2019-08-04 23:26

There was indeed something I didn't notice. Posting in case it helps anyone.

On the web app section of PythonAnywhere there are three Log Files links where you can see the kinds of things that would apear on the console in a regular Python app.

Those links look like this:

username.eu.pythonanywhere.com.access.log 
username.eu.pythonanywhere.com.error.log
username.eu.pythonanywhere.com.server.log #no .eu on the american PythonAnywhere

And server.log is where console print statements end up.

Also, regular messages from Telegram users look like this when they arrive to Flask:

{
'update_id': 123456789, 
'message': {
    'message_id': 42, 
    'from': {
        'id': 42424242, 
        'is_bot': False, 
        'first_name': 'Joaquim', 
        'last_name': 'Pernil Rinoceronts', 
        'username': 'Juqim', 
        'language_code': 'ca'
        }, 
    'chat': {
        'id': 42424242, 
        'first_name': 'Joaquim',
        'last_name': 'Pernil Rinoceronts', 
        'username': 'Juqim', 
        'type': 'private'
        }, 
    'date': 1562247903, 
    'text': 'Patata'
    }
}

Stickers have their info where 'text' would be:

'sticker': {
    'width': 512, 
    'height': 512, 
    'emoji': '                                                                    
查看更多
登录 后发表回答