如何安排电报机器人发送消息?

问题描述 投票:0回答:1

我正在尝试创建一个Telegram机器人,该机器人在特定时间下午5:30发送消息。但是,a尝试的方法不正确。

我想触发与时间有关的send_message,而无需用户发送任何/ command。

import telebot
import datetime

TOKEN = 'MyToken'
bot = telebot.TeleBot(TOKEN)


@bot.message_handler(commands=['start'])
def send_welcome(message):
    message_id=message.chat.id
    bot.reply_to(message,"Welcome")


bot.polling()

直到现在,我一直在尝试添加类似的内容,当然,它不是python而是一种伪代码,仅用于说明:

if currenttime=17:30
 send_message(idchat, "mymessage")

谢谢你。

python telegram-bot python-telegram-bot
1个回答
0
投票

如果我理解正确,则需要在发送消息之前检查系统时间,可以使用以下代码 [source]

from datetime import datetime
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
© www.soinside.com 2019 - 2024. All rights reserved.