Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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.
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()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question