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

Ambos lados, revisión anteriorRevisión previa
Próxima revisión
Revisión previa
informatica:programacion:python:modulos:python-telegram-bot [2021/02/14 12:48] – [Mensajes] 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 ===== ===== Mensajes =====
  
-Los mensajes que se reciben mediante Telegram, este módulo los encapsula en un objeto de tipo ''telegram.Message'' +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. 
-==== Mensajes ====+ 
 +  * ''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> <code>
Línea 98: Línea 141:
         'is_bot': True,          'is_bot': True, 
         'username': 'unbot'         '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> </code>
informatica/programacion/python/modulos/python-telegram-bot.1613303336.txt.gz · Última modificación: por tempwin