====== Python: python-telegram-bot======
Módulo para interactuar con la API de Telegram a través de Python.
* [[https://python-telegram-bot.org/|Web oficial]]
* [[https://python-telegram-bot.readthedocs.io/en/stable/|Documentación oficial]]
===== Instalación =====
Se puede instalar a través del gestor de módulos de Python ''pip'':
pip install python-telegram-bot
===== Hola, mundo =====
Ejemplo de código que responde al texto ''/hola'':
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
BOT_TOKEN = "1234567890:ABCDEF0123456789ABCDEF"
def hello(update: Update, context: CallbackContext) -> None:
update.message.reply_text(f'Hola, {update.effective_user.first_name}')
updater = Updater(BOT_TOKEN)
updater.dispatcher.add_handler(CommandHandler('hello', hello))
updater.start_polling()
updater.idle()
===== HTML =====
* [[https://core.telegram.org/bots/api#html-style|Etiquetas soportadas por la API oficial de Telegram]]
===== Emojis =====
* [[https://github.com/python-telegram-bot/python-telegram-bot/wiki/Emoji]]
Se pueden introducir los emojis directamente. En la web oficial de Unicode tienen un [[http://www.unicode.org/emoji/charts/full-emoji-list.html|listado emojis]]. Basta con copiar lo que aparece en la columna **Browser** y pegarlo en el código Python.
===== Mensajes =====
Los mensajes que se reciben mediante Telegram, este módulo los encapsula en un objeto de tipo [[https://python-telegram-bot.readthedocs.io/en/stable/telegram.message.html|telegram.Message]]. Dependiendo del tipo de mensaje que se trate, tendrá una serie de atributos y métodos.
* ''update.message.message_id'': identificador del mensaje en esa conversación/chat
* ''update.message.date'': fecha del mensaje (objeto ''datetime'')
* ''update.message.chat.id'': identificador del usuario con el que se habla
* ''update.message.chat.type'': tipo de conversación (''private'', ''group'', ''supergroup''
==== Texto ====
{
'message_id': 1001,
'date': 1613301503,
'chat': {
'id': 123456,
'type': 'private',
'username': 'foo',
'first_name': 'bar'
},
'text': 'ping',
'entities': [],
'caption_entities': [],
'photo': [],
'new_chat_members': [],
'new_chat_photo': [],
'delete_chat_photo': False,
'group_chat_created': False,
'supergroup_chat_created': False,
'channel_chat_created': False,
'from': {
'id': 123456,
'first_name': 'bar',
'is_bot': False,
'username': 'foo',
'language_code': 'es'
}
}
==== Fotos ====
La estructura de un mensaje:
{
'message_id': 1000,
'date': 1613300902,
'chat': {
'id': 123456,
'type': 'private',
'username': 'foo',
'first_name': 'bar'
},
'entities': [],
'caption_entities': [],
'photo': [
{
'file_id': 'AgACAgQAAxkDAAID6GApBKZS-8S0nLV5RG6bEw7xp3__AAINrDEbC8BMUVM-v_GTUAW1H7RJJ10AAwEAAwIAA20AA99BBQABHgQ',
'file_unique_id': 'AQADH7RJJ10AA99BBQAB',
'width': 320,
'height': 180,
'file_size': 4643
},
{
'file_id': 'AgACAgQAAxkDAAID6GApBKZS-8S0nLV5RG6bEw7xp3__AAINrDEbC8BMUVM-v_GTUAW1H7RJJ10AAwEAAwIAA3gAA-BBBQABHgQ',
'file_unique_id': 'AQADH7RJJ10AA-BBBQAB',
'width': 800,
'height': 451,
'file_size': 17870
},
{
'file_id': 'AgACAgQAAxkDAAID6GApBKZS-8S0nLV5RG6bEw7xp3__AAINrDEbC8BMUVM-v_GTUAW1H7RJJ10AAwEAAwIAA3kAA-FBBQABHgQ',
'file_unique_id': 'AQADH7RJJ10AA-FBBQAB',
'width': 1280,
'height': 721,
'file_size': 35816
},
{
'file_id': 'AgACAgQAAxkDAAID6GApBKZS-8S0nLV5RG6bEw7xp3__AAINrDEbC8BMUVM-v_GTUAW1H7RJJ10AAwEAAwIAA3cAA-JBBQABHgQ',
'file_unique_id': 'AQADH7RJJ10AA-JBBQAB',
'width': 1984,
'height': 1117,
'file_size': 61820
}
],
'new_chat_members': [],
'new_chat_photo': [],
'delete_chat_photo': False,
'group_chat_created': False,
'supergroup_chat_created': False,
'channel_chat_created': False,
'from': {
'id': 654321,
'first_name': 'unbot',
'is_bot': True,
'username': 'unbot'
}
}
==== Vídeos ====
{
'message_id': 53,
'date': 1613676565,
'chat': {
'id': 3904192,
'type': 'private',
'username': 'tempwin',
'first_name': 'TW'
},
'entities': [],
'caption_entities': [],
'photo': [],
'video': {
'file_id': 'BAACAgQAAxkDAAM1YC7AFTGFmOESXIpZJef6JiVp6sMAAk0CAAJJ1SxT1FjK5SacHn8eBA',
'file_unique_id': 'AgADTQIAAknVLFM',
'width': 320,
'height': 240,
'duration': 126,
'thumb': {
'file_id':
'AAMCBAADGQMAAzVgLsAVMYWY4RJcilkl5_omJWnqwwACTQIAAknVLFPUWMrlJpwef9DAViVdAAMBAAdtAAMhVQACHgQ',
'file_unique_id': 'AQAD0MBWJV0AAyFVAAI',
'width': 320,
'height': 240,
'file_size': 17512
},
'file_name': 'sample-mp4-file.mp4',
'mime_type': 'video/mp4',
'file_size': 10546620
},
'new_chat_members': [],
'new_chat_photo': [],
'delete_chat_photo': False,
'group_chat_created': False,
'supergroup_chat_created': False,
'channel_chat_created': False,
'from': {
'id': 1471129400,
'first_name': 'Un bot',
'is_bot': True,
'username': 'Unbot'
}
}