How to send message in specific time TelegramBot

点点圈 提交于 2020-03-03 04:36:22

问题


Hi i want to send message from bot in specific time (without message from me), for example every Saturday morning at 8:00am. Here is my code:

import telebot
import config
from datetime import time, date, datetime

bot = telebot.TeleBot(config.bot_token)
chat_id=config.my_id    

@bot.message_handler(commands=['start', 'help'])
def print_hi(message):
    bot.send_message(message.chat.id, 'Hi!')


@bot.message_handler(func=lambda message: False) #cause there is no message
def saturday_message():
    now = datetime.now()
    if (now.date().weekday() == 5) and (now.time() == time(8,0)):
        bot.send_message(chat_id, 'Wake up!')

bot.polling(none_stop=True)

But ofc that's not working. Tried with

urlopen("https://api.telegram.org/bot" +bot_id+ "/sendMessage?chat_id=" +chat_id+ "&text="+msg)

but again no result. Have no idea what to do, help please with advice.


回答1:


You could manage the task with cron/at or similar.

Make a script, maybe called alarm_telegram.py.

#!/usr/bin/env python
import telebot
import config

bot = telebot.TeleBot(config.bot_token)
chat_id=config.my_id
bot.send_message(chat_id, 'Wake up!')

Then program in cron like this.

00 8 * * 6 /path/to/your/script/alarm_telegram.py

Happy Coding!!!



来源:https://stackoverflow.com/questions/48288124/how-to-send-message-in-specific-time-telegrambot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!