F
F
From Prog2021-06-09 12:31:53
Python
From Prog, 2021-06-09 12:31:53

How to remove a function call with sending a message by a bot in Telegram after stopping?

The code:

@my_bot.message_handler(content_types = ['text'])
def msg(message):

  def send_bitcon_rate():
    source = requests.get(url_bitcoin, headers = headers)
    html = BeautifulSoup(source.text, 'lxml')
    kbd = types.ReplyKeyboardMarkup(row_width = 1)
    value_stop = types.KeyboardButton('stop')
    kbd.add(value_stop)
    my_bot.send_message(message.chat.id, 'Курс Bitcoin: {0}'.format(html.find('span', {'class': 'pid-1057391-last', 'id': 'last_last'}).get_text()), reply_markup = kbd)

  while True:
    if message.text == 'Bitcoin':
      send_bitcon_rate()
    elif message.text == 'stop':
      pass

my_bot.polling(none_stop = True, interval = 0)

I write: Bitcoin
Bot: Bitcoin rate: 34.340.0
Bot: Bitcoin rate: 34.367.3
I write: stop
Bot writes in 20 seconds again Bitcoin rate: 34.336.3

How can I make sure that after stopping the bot does not call this function?
Like if my last stop, how does it get to if message.text == 'Bitcoin':?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Krostelev, 2021-06-09
@FromProg

And what did you expect from while True ?
Your "stop" message hasn't even started processing. You go into an endless loop after the first message.
In a good way, for any mailings, you need one more script. Which will only send messages on a timer. there will be no bot.pooling.
And in your script, you need to add interaction with the database. There you will record the status of each user. Remove while from the message handler, now there will only be a record in the database.
In the new script that will deal with the mailing, you can insert while with some kind of waiting time. The script will go through the database each time, select the user IDs who need to send the bitcoin rate and send it out.

O
Onigire, 2021-06-09
@Onigire

stopped  = False  
...
@bot.message_handler(commands=['stop'])
def stop(message):
    global stopped
    bot.send_message(message.chat.id, 'Рассылка прекращена')
    stopped = True

...

while True:
    if not stopped:
        if message.text.lower() == 'bitcoin':
            send_bitcon_rate()

In theory, this is how it should work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question