Answer the question
In order to leave comments, you need to log in
Why did Python commands stop working?
I'm trying to understand Python and telebot, the /start commands and the "hello/bye" text message worked and are working properly, the /test1, /test2 commands worked, but the other two did not work, I corrected them a little, they started working, but after restarting the program , everything stopped working except for the /start command and the "hello/bye" text message. Tell me what's wrong
import telebot
import sys
from requests import get
from config3 import TOKEN
bot = telebot.TeleBot(TOKEN);
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, 'Команда старт!')
@bot.message_handler(content_types=['text'])
def send_text(message):
if message.text.lower() == 'привет':
bot.send_message(message.chat.id, 'Привет, мой создатель')
elif message.text.lower() == 'пока':
bot.send_message(message.chat.id, 'Прощай, создатель')
@bot.message_handler(commands=['test'])
def test_q(message):
bot.send_message(message.chat.id, 'ТЕСТ1')
bot.send_photo(message.chat.id, get('https://ie.wampi.ru/2021/12/12/kitten0.jpg').content)
@bot.message_handler(commands=['test2'])
def test_w(message):
bot.send_message(message.chat.id, 'ТЕСТ2')
bot.send_photo(message.chat.id, photo=open('demo-media\pics\kitten1.jpg', 'rb'))
@bot.message_handler(commands=['test3'])
def test_e(message):
bot.send_message(message.chat.id, 'ТЕСТ3')
voice=open(r'demo-media\ogg\test3.ogg', 'rb')
bot.send_voise(message.chat.id, voise)
voise.close()
@bot.message_handler(commands=['test4'])
def test_r(message):
bot.send_message(message.chat.id, 'ТЕСТ4')
audio = open(r'D:\Python\bot3\demo-media\music\Mneskin_-_Beggin_52795146.mp3', 'rb')
bot.send_audio(message.chat.id, audio)
audio.close()
bot.polling()
Answer the question
In order to leave comments, you need to log in
Such large decorators should be defined somewhere at the very bottom.
What's going on: write /test
, the library is looking for which function to throw this message into. And sends it all to , because although it looks like a command, it is still text. Therefore, the top decorator is fired, which catches all text messages. def send_text(message):
/test
Try to specify at the end instead of bot.polling()
Or put the test_q() function after all the commands
The problem is that your script goes in order. First start, then the test_q() function, on which it stops, waiting for messages. And only then goes to the rest of the teams
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