B
B
Brad_baf2020-06-01 20:43:09
Python
Brad_baf, 2020-06-01 20:43:09

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

2 answer(s)
S
soremix, 2020-06-02
@Brad_baf

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)

Should be:
def check(message):
    x = message.text
    if x == "say_smth":
        command = say_hi_comm(message)
    else:
        command = start_comm(message)

You process messages from the user either through the decorator, checking what text he entered, or through register_next_step_handler. But not both ways

H
Hukyl, 2020-06-01
@Hukyl

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)

If this code can also send a command twice, then the problem is not on the Telegram side
(I personally checked this code many times)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question