Answer the question
In order to leave comments, you need to log in
I am making a bot in python and trying to send a message at a certain time, but an error appears, what should I do?
Full code:
import time
import schedule
import telebot
bot = telebot.TeleBot('ключ')
@bot.message_handler(commands=['start'])
def start_command(message):
bot.send_message(message.chat.id, 'тут будет чота гавгать')
schedule.every().day.at("17:47").do(start_command)
while True:
schedule.run_pending()
time.sleep(1)
bot.polling()
Answer the question
In order to leave comments, you need to log in
Try writing "def start_message()" instead of "def start_message(message)".
You are not passing the message argument to your function
You inserted the wrong thing into the schedule.every().day.at("17:47").do() function . Should have put in bot.polling()
import time
import schedule
import telebot
bot = telebot.TeleBot('ключ')
@bot.message_handler(commands=['start'])
def start_command(message):
bot.send_message(message.chat.id, 'тут будет чота гавгать')
schedule.every().day.at("17:47").do(bot.polling()) #Здесь запускаем бота в нужный момент
while True:
schedule.run_pending()
time.sleep(1)
Sometimes it's useful to look at the documentation
. As I understand it, you just need to send a message to someone, so if I were you, I would put it in a separate function and run it, and don't touch the handler for these tasks
def my_daily_task(tg_id):
bot.send_message(tg_id, 'тут будет чота гавгать')
schedule.every().day.at("17:47").do(start_command, tg_id=<Id пользователя кому отправляем>)
It should be understood that bot.polling() will start the bot's work cycle, and will not return control until the bot is disconnected.
This means that making friends with while True: schedule.run_pending() is very problematic - they both want to run an eternal loop in which they will do their own thing, but there can only be one such loop ... within one thread.
I would do this:
1. In the main thread, prepare the second thread and set the desired response time via schedule.every(). Then we launch the bot via bot.polling().
2. The second thread executes the schedule.run_pending()+sleep() loop. Use a boolean variable so the loop knows when to end.
3. Upon exiting bot.polling(), we signal completion for the second thread and wait for it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question