T
T
taxi_228_xach_2282022-03-31 14:33:48
Python
taxi_228_xach_228, 2022-03-31 14:33:48

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

A telegram bot written in Python, searches for information on Wikipedia on command, gives out too much information
I would like the bot to give out only up to 1000 characters or only 1 paragraph how to do this? used pip library pyTelegramBotAPI
eiJAH.jpg

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ramis, 2022-03-31
@taxi_228_xach_228

Two paragraphs

bot.send_message(message.chat.id, ''.join(r.split('\n')[:2]))

V
Viktor Golovanenko, 2022-03-31
@drygdryg

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 question

Ask a Question

731 491 924 answers to any question