Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newline in telegram inline keyboard for python

my button text is too long to fit in one line of my inline keyboard for the python telegram bot. "\n" wont do.

Code info: /key is the only command it understands. It reads the API token from the file token.txt in the code directory.

Here is my code:

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()

The second message is with the "\n" inserted. The first one is just text. Another option would be for everyone to get a bigger phone :D

like image 391
JulianWgs Avatar asked Oct 24 '17 18:10

JulianWgs


Video Answer


1 Answers

Not possible for InlineKeyboardButton, but possible for ReplyKeyboardMarkup (it is bigger and support resize option) if it works for you.

But preferred way is to show that really long text in message and for button - use really short version for inline buttons.

Example:

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)

Result: enter image description here

like image 161
wowkin2 Avatar answered Oct 15 '22 09:10

wowkin2