Answer the question
In order to leave comments, you need to log in
How to make sure that the bot does not duplicate the execution of commands?
I write on the pyTelegramBotAPI library. There is a bot that has two commands. If at the beginning you write the name of the team once, then everything will work as it should, but if you write the name of the team two or more times in the starting field until the moment when the bot starts to analyze the sent message, then the command will be executed twice. Here is the question of how to make the bot perceive not 2 sent messages, but only one.
I will try to describe how it looks in the chat:
User: /start
Bot: prompts you to select one of the commands
User: once the
user has registered this command: the second time he has registered this command
Bot: performs the function once
Bot: performs the function a second time
User: /start
Bot : prompts you to select one of the commands
Bot: for the second time it offers to choose one of the commands, despite the fact that the user has registered / start only once
If possible, a code example. And thanks for taking the time to solve my problem!
Answer the question
In order to leave comments, you need to log in
The decorator is redundant here:
@bot.message_handler(content_types=['text'])
def check(message):
x = message.text
if x == "say_smth":
command = say_hi_comm(message)
else:
command = start_comm(message)
def check(message):
x = message.text
if x == "say_smth":
command = say_hi_comm(message)
else:
command = start_comm(message)
Honestly, I don’t understand how you managed to get the bot to enter twice
. Here is the code:
import telebot
TOKEN = "ВАШ_ТОКЕН"
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands = ['start'])
def start_comm(message):
bot.send_message(message.chat.id, "Выбери команду (/start, /say_hi)")
@bot.message_handler(commands = ['say_hi'])
def say_hi_comm(message):
bot.send_message(message.chat.id, f"Привет, {message.from_user.first_name}!")
bot.polling(none_stop = True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question