如何在特定时间发送消息 TelegramBot [英] How to send message in specific time TelegramBot

查看:36
本文介绍了如何在特定时间发送消息 TelegramBot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定时间从机器人发送消息(没有我的消息),例如每个星期六早上 8:00.这是我的代码:

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)

但是ofc 是行不通的.试过

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.

推荐答案

您可以使用 cron/at 或类似工具管理任务.

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

制作一个脚本,可能叫做alarm_telegram.py.

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!')

然后像这样在 cron 中编程.

Then program in cron like this.

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

快乐编码!!!

这篇关于如何在特定时间发送消息 TelegramBot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆