A
A
Alexander2021-07-29 17:00:02
Python
Alexander, 2021-07-29 17:00:02

How to make a message listener and perform something in a loop for a tg bot at the same time?

I started learning Python, I want to make a simple bot that checks the rss feed and sends a link to the channel if there is a new post. But at the same time I want to make it possible to turn off news and add feeds. This requires the bot to listen to messages. Below is the code I have tried. Swapped the bot poll and the cycle, but one thing works. Tell me what to read, where to see. Bots with similar functions looked in the github, but there is something very complicated

import telebot, time
from config.config import BOT_TOKEN

bot = telebot.TeleBot(BOT_TOKEN)


@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
    bot.reply_to(message, "Howdy, how are you doing?")


@bot.message_handler(func=lambda message: True)
def echo_all(message):
    bot.reply_to(message, message.text)


bot.polling()

while True:
    print('Hello')
    time.sleep(1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-07-29
@anudas

I advise you to read about the basics of asynchronous programs.
But if on your fingers, consider that the program will not exit from bot.polling() until it completes. She will spin there, wait for the event, and pull the handlers.
Therefore, everything that you want to teach a bot to do, you need to do

  • OR within the framework (what by the way? pyTelegramBotAPI? He can't do it)
  • OR within a separate thread to run before calling bot.polling()
  • OR in general within a separate script, which is called by cron.

If within a thread, you will need to see if the telebot allows you to call methods from another thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question