Answer the question
In order to leave comments, you need to log in
How to reduce the size of the displayed text in the telegrambot?
import random
import telebot, wikipedia, re
from telebot import types, TeleBot
@bot.message_handler(commands=['wiki'])
def wiki(message):
wikipedia.set_lang('ru')
r = wikipedia.page(message.text.split(maxsplit=1)[1]).content
if len(r) > 4096:
for x in range(0, len(r), 4096):
bot.send_message(message.chat.id, '{}'.format(r[x:x + 4096]))
print(x)
else:
bot.send_message(message.chat.id, '{}'.format(r))
return r
Answer the question
In order to leave comments, you need to log in
Two paragraphs
bot.send_message(message.chat.id, ''.join(r.split('\n')[:2]))
Use the textwrap
standard library module . In this example, the text is reduced to 100 characters, taking into account word boundaries (words will not be cut off):
import textwrap
text = "В центре города большого, где травинки не растёт, жил поэт, волшебник слова, - вдохновенный рифмоплёт. Рифмовал он что попало, просто выбился из сил, и в деревню на поправку, где коровы щиплют травку, отдыхать отправлен был."
shortened_text = textwrap.shorten(text, width=100, placeholder="…") # "В центре города большого, где травинки не растёт, жил поэт, волшебник слова, - вдохновенный…"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question