D
D
Dmitry2020-05-29 17:15:02
Python
Dmitry, 2020-05-29 17:15:02

Why is the bot not working?

Hello, I wrote a code for the translate command, the bot receives the word along with the command and gives the translation of the word. I did it through the messages.text.split construction, but the bot outputs that "I didn't understand the word". Where is the mistake?

#Обработчик команды /translate
@bot.message_handler(commands=["translate"])
def translate(message):
    try:
        word = message.text.split(maxsplit=1)[1]
        worden = translator.translate(word, dest='en')
        finword = worden.text
        bot.send_message(message.chat.id, finword)
    except:
        bot.send_message(message.chat.id, "Не понял слова")

Used googletrans libraries. At the top of the code declared Translator.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2020-05-29
@Tomio

Handle the exception correctly, it's not even clear to you what the error is.
Replace at least except with:

except Exception as err:
    print(str(err))

and look at the error. Or remove the try...except block (leave only what is in the try block) so that the error stack trace is visible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question