D
D
dandy...2021-09-07 21:29:37
Python
dandy..., 2021-09-07 21:29:37

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()


Error: 6137aee24df2f990520171.png

ps
I understand the error, I don't understand how to solve it. I don’t understand how to paste the argument, and most importantly, I don’t know where)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
W
wasp1ck, 2021-09-07
@wasp1ck

Try writing "def start_message()" instead of "def start_message(message)".
You are not passing the message argument to your function

R
Renat Ataev, 2021-09-08
@fanepka

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)

If I understand you correctly, then the bot should start at 17:47. If so, then the code I wrote above will work for you

S
Stefan, 2021-09-08
@MEDIOFF

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 пользователя кому отправляем>)

V
Vindicar, 2021-09-08
@Vindicar

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 question

Ask a Question

731 491 924 answers to any question