如何在特定时间发送消息 TelegramBot

编程入门 行业动态 更新时间:2024-10-28 15:29:53
本文介绍了如何在特定时间发送消息 TelegramBot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想在特定时间从机器人发送消息(没有我的消息),例如每个星期六早上 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/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屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 07:20:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送消息   时间   如何在   TelegramBot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!