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:14] 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 [[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: La estructura de un mensaje:
  
-<code json>+<code>
 { {
     'message_id': 1000,      'message_id': 1000, 
     'date': 1613300902,      'date': 1613300902, 
     'chat': {     'chat': {
-        'id': 4904395+        'id': 123456
         'type': 'private',          'type': 'private', 
-        'username': 'lilipendo',  +        'username': 'foo',  
-        'first_name': 'JM'+        'first_name': 'bar'
     },      }, 
     'entities': [],      'entities': [], 
Línea 59: Línea 137:
     'channel_chat_created': False,      'channel_chat_created': False, 
     'from': {     'from': {
-        'id': 685775461,  +        'id': 654321,  
-        'first_name': 'micoshi', +        '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,          'is_bot': True, 
-        'username': 'micoshibot'+        'username': 'Unbot'
     }     }
 } }
 </code> </code>
informatica/programacion/python/modulos/python-telegram-bot.1613301259.txt.gz · Última modificación: por tempwin