python 电报内联键盘中的换行符

Newline in telegram inline keyboard for python

我的按钮文本太长,我的 python telegram 机器人的内联键盘一行都放不下。 "\n" 不行。

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

这是我的代码:

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”。第一个只是文本。另一种选择是让每个人都得到一个更大的 phone :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)

结果: