gpt4 book ai didi

python-2.7 - python - 特定日期的电报机器人sendMessage

转载 作者:行者123 更新时间:2023-12-02 03:01:08 25 4
gpt4 key购买 nike

我是 python 的新手,我的进步就像蜗牛一样:(我想制作一个在特定日期和时间发送消息的电报机器人。我用了apschedulertelepot为此的图书馆。这是我的代码:

import telepot
import sys
import time
from time import sleep
from datetime import datetime
from apscheduler.scheduler import Scheduler
import logging


bot = telepot.Bot("***")
logging.basicConfig()
sched = Scheduler()
sched.start()
exec_date = datetime(2017, 9, 12 ,1,51,0)


def handle(msg):
content_type,chat_type,chat_id = telepot.glance(msg)
print(content_type,chat_type,chat_id)

if content_type == 'text' :
bot.sendMessage(chat_id,msg['text'])


def sendSimpleText():
# content_type,chat_type,chat_id = telepot.glance(msg)
# print(content_type,chat_type,chat_id)
#
# if content_type == 'text' :
chat_id = telepot.
bot.sendMessage(chat_id,'faez')


def main():
job = sched.add_date_job(sendSimpleText, exec_date)
while True:
sleep(1)
sys.stdout.write('.'); sys.stdout.flush()
# bot.message_loop(handle)
# # job = sched.add_date_job(sendSimpleText, '2017-09-11 21:35:00', ['testFuckBot'])
# while True:
# time.sleep(10)

if __name__ == '__main__':
main()

我的问题是我在 add_date_job 中将什么作为参数传递给 sendSimpleText?在这一行中:

job = sched.add_date_job(sendSimpleText, exec_date)

我知道 msg 是用户输入的消息,所以对于 add_date_job 我什么都没有?

最佳答案

您使用的是旧版 (2.1.2) 的 APScheduler。新版本有新语法。

add_date_job 函数不再可用。这是一个适合您的解决方案:

import telepot
import sys
import time
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from telepot.loop import MessageLoop
import logging

bot = telepot.Bot("***YOUR_BOT_TOKEN***")
logging.basicConfig()
sched = BackgroundScheduler()
exec_date = datetime(2017, 9, 12 ,3,5,0)

def handle(msg):
content_type,chat_type,chat_id = telepot.glance(msg)
print(content_type,chat_type,chat_id)

if content_type == 'text' :
bot.sendMessage(chat_id,msg['text'])


def sendSimpleText(chat_id):
bot.sendMessage(chat_id,'faez')


def main():
MessageLoop(bot, handle).run_as_thread()
job = sched.add_job(sendSimpleText, run_date=exec_date, args=['**YOUR_TELEGRAM_ID**'])

while True:
time.sleep(1)
sys.stdout.write('.'); sys.stdout.flush()

if __name__ == '__main__':
sched.start()
main()

关于python-2.7 - python - 特定日期的电报机器人sendMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46164955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com