问题
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:
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': '😒',
'set_name': 'Ruscamems',
'thumb': {
'file_id': 'AAQEABNgnrsaAAQkkp4QRiVF1rokAAIC',
'file_size': 4840,
'width': 128,
'height': 128
},
'file_id': 'CAADBAADBQADkvulAumgmwOAjdfYAg',
'file_size': 36612
}
Images have 'photo'
instead, and they come in a collection of different sizes:
'photo':[
{
'file_id': 'AgADBAADVrAxG2wj8FCs-f6v7AGFUQvo-RkABFGq4cIH4_MaHXIFAAEC',
'file_size': 2101,
'width': 66,
'height': 90
},
{
#same but bigger (different id too)
},
... #I got 4 sizes.
]
I guess I'll post the callback too and we'll have most of the interesting stuff:
{
'update_id': 123456793,
'callback_query': {
'id': '424242424242424242',
'from': { #Who pressed the Button
'id': 42424242,
'is_bot': False,
'first_name': 'Joaquim',
'last_name': 'Pernil Rinoceronts',
'username': 'Juqim',
'language_code': 'ca'
},
'message': { #What message was the button in
'message_id': 123,
'from': {
'id': 434343434,
'is_bot': True,
'first_name': 'The Bot Name',
'username': 'name_bot'
},
'chat': {
'id': 42424242,
'first_name': 'Joaquim',
'last_name': 'Pernil Rinoceronts',
'username': 'Juqim',
'type': 'private'
},
'date': 1562252792,
'text': 'A viam si funciona això',
'reply_markup': { #Keyboard pressed
'inline_keyboard': [[{'text': 'Cliccami', 'callback_data': 'clicat'}]]
}
},
'chat_instance': '1234123412341234242',
'data': 'clicat' #Callback data (button pressed)
}
}
来源:https://stackoverflow.com/questions/56889594/whats-the-structure-of-telegrams-updates-to-webhook-flask-app