本文介绍了Telegram.py : 按钮创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Python 制作电报.我想知道如何制作这样的功能按钮

I'm currently making a Telegram using Python. I was wondering how to make a functional buttons like this

https://i.stack.imgur.com/e3eMb.png

我想让 /command 命令具有显示这是一个按钮"的按钮.我该怎么做?这是我的帮助代码:

I would like to make /command command to have button that says "This is a button". How do I make it? Here's my code to help :

# Modules needed
import time
from time import sleep
import random
import logging

from telegram.ext import Updater, CommandHandler, MessageHandler, Filters

# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)


# /command
def command(update, context):
    update.message.reply_text("Reply here")

def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)


def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    updater = Updater("[TOKEN]", use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("command", command))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    updater.start_polling()

    updater.idle()


if __name__ == '__main__':
    main()

推荐答案

您正在寻找的是所谓的内嵌按钮.查看官方文档和这个python-telegram-bot 示例.

What you are looking for are so called inline buttons. Check out the official docs and this python-telegram-bot example.

免责声明:我目前是 python-telegram-bot 的维护者.

Disclaimer: I'm currently the maintainer of python-telegram-bot.

这篇关于Telegram.py : 按钮创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 18:21