Herramientas de usuario

Herramientas del sitio


informatica:programacion:python:modulos:python-telegram-bot

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Próxima revisión
Revisión previa
informatica:programacion:python:modulos:python-telegram-bot [2021/02/14 12:10] – creado tempwininformatica:programacion:python:modulos:python-telegram-bot [2021/06/23 11:28] (actual) – [Python: python-telegram-bot] tempwin
Línea 6: Línea 6:
   * [[https://python-telegram-bot.readthedocs.io/en/stable/|Documentación 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'':
 +
 +<code>
 +pip install python-telegram-bot
 +</code>
 +===== Hola, mundo =====
 +
 +Ejemplo de código que responde al texto ''/hola'':
 +
 +<code python>
 +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()
 +</code>
 +
 +===== 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 ====
 +
 +<code>
 +{
 +    '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'
 +    }
 +}
 +</code>
 +
 +==== Fotos ====
 +
 +La estructura de un mensaje:
 +
 +<code>
 +{
 +    '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'
 +    }
 +}
 +</code>
 +
 +==== Vídeos ====
 +
 +<code>
 +{
 +    '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'
 +    }
 +}
 +</code>
informatica/programacion/python/modulos/python-telegram-bot.1613301046.txt.gz · Última modificación: por tempwin