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

标签 python-2.7 apscheduler telepot

我是 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/

相关文章:

python - 基于列表查找字符串的有效方法

python-类型错误: func must be callable

python - django + apscheduler - 如何配置 APScheduler 以使用 settings.py 中的 django db 连接数据?

python - Telepot - 读取已发送消息的文本

python - 将数据从套接字绘制到 TKinter 时,pickle 出现 EOFError

python - 导入/包含配置 Python

python - 如何在 Telegram Bot 中管理更多用户?

flask - 连接已建立时发生连接错误

Python Pandas Pivot - 为什么失败

python - 安排 SQLAlchemy 清除表中的所有行