gpt4 book ai didi

python-3.x - python的电报内联键盘中的换行符

转载 作者:行者123 更新时间:2023-12-03 14:44:11 30 4
gpt4 key购买 nike

我的按钮文本太长,无法放入 python 电报机器人的内联键盘的一行中。 "\n"不行。

代码信息:/key 是它唯一能理解的命令。它从代码目录中的文件 token.txt 中读取 API token 。

这是我的代码:

from pprint import pprint
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
from telegram.ext import InlineQueryHandler
from telegram.ext import CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram import InlineQueryResultArticle, InputTextMessageContent
import logging
import time, threading, pickle

file = open("token.txt", "r")
TOKEN = file.read()

updater = Updater(TOKEN)

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

dispatcher = updater.dispatcher

def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")

start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)

def key_menu(bot, update):

text = "Some really long text I\n want on two rows :D"
callback = "nothing"

keyboard = []
keyboard.append([InlineKeyboardButton(text, callback_data = callback)])

reply_markup = InlineKeyboardMarkup(keyboard)

update.message.reply_text('Some text', reply_markup=reply_markup)

key_menu_handler = CommandHandler('key', key_menu, pass_args=False)
dispatcher.add_handler(key_menu_handler)


updater.start_polling()



第二条消息是插入了“\n”。第一个只是文本。另一种选择是让每个人都获得更大的手机:D

最佳答案

不适用于 InlineKeyboardButton ,但可能适用于 ReplyKeyboardMarkup (它更大并支持调整大小选项)如果它适合你。
但首选的方法是在消息和按钮中显示非常长的文本 - 对内联按钮使用非常短的版本。
示例:

buttons = [
['Some really long text I \n'
'want on two rows :D'],
['Some really long text I \n'
'want on two rows :D']
]
keyboard = ReplyKeyboardMarkup(buttons, resize_keyboard=True)

update.message.reply_text('Some message', reply_markup=keyboard)
结果:
enter image description here

关于python-3.x - python的电报内联键盘中的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46917909/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com