Search code examples
pythontelegramchatbotpython-telegram-bot

How to output message every 10 items in a list to the chatbot with python-telegram-bot


I am writing a Telegram bot with python-telegram-bot package. I want to output a list which store search result to the chatbot with python-telegram-bot. Sometimes the result is more than 4000+ characters and not allow to output. So I want to send the result separately and make it send 10 items in a list per message if the items inside the list is more than 10. So far I can now output the format I want for less than 4000+ characters through the chatbot but I can't figure out how to make as 10 items per message? I checked about slice but it seems only work on list but python-telegram-bot only export text. Since the school_date is dictionary, I also tried to put the for loop between "text = "Searching result:\n"" & "for i, school_data in enumerate(search_result): " and also after this for loop, it kept repeating the last one or repeating individual lines; used split() (wanted to trim from the modified text) but it would mess up the output format.

Here's the ideal output format of items I've been working:

university_name: blablabla
department: blablabla
staff_name: blablabla
ranking: blablabla
specialisation: blablabla
website: blablabla
-----
university_name: blablabla
department: blablabla
staf_name: blablabla
ranking: blablabla
specialisation: blablabla
website: blablabla
------
.
.
.

Here's the code that I can print no matter how many items there is in the list:

        search_result = []

        for items in data["staffs"]:
            specialisation = items["staff_specialisation"]
            if keyword in specialisation:
                search_result.append({"university_name": items["university"], "department": items["department"], "staff_name": items["staff_name"], "ranking": items["ranking"], "specialisation": items["staff_specialisation"], "website": items["website"]})

        text = "Searching result:\n"
        for i, school_data in enumerate(search_result):
            for key in school_data:
                text += "\n" + "{}:{}".format(key, school_data[key])
            text += "\n------"

        update.message.reply_text(text)

Many thanks!


Solution

  • I would put the reply_text() inside an if item%10==9: while you're iterating. With %10 (modulo 10) you will do something every 10 loops. Why 9? Because enumerates starts at 0 and also the if will be at the end of every loop).

    But if the item list isn't a multiple of 10 it will leave you some items without displaying. For example, if your item list has 28 items you will show the first 10 items, the second 10 items and that's it, you will miss the last 8 items. To fix that it's a better idea to use if item%10==9 or item==len(itemlist)-1:, so the if will also check if you're at the last item of your searching result and perform the final reply_text()

    text = "Searching result:\n"
    for i, school_data in enumerate(search_result):
        for key in school_data:
            text += "\n" + "{}:{}".format(key, school_data[key])
        text += "\n------"
        if i%10==9 or i==len(search_result)-1:
            update.message.reply_text(text)
            text = "Searching result:\n"
    

    In this particular case, the if clause is in the first for loop, the one that iterates over search_result. The sames goes for the i counter that we use in the if.