2

我的 python 脚本作为 cronjob 安装,它每分钟运行一次,并根据发出的请求将报告发送给用户(基本上我的电报机器人与授权用户交互并发送报告)。一切正常,只是需要延迟 1 分钟。我希望我的脚本能够实时监听请求并立即发送报告,不会有任何延迟。我不太确定该怎么做。

import telepot

# Get the latest update
def fetch():
    response = bot.getUpdates()

# Authorize the user 
def authorize():
    ---Code to Authorize---


# Send the stats
def stat():
    ---Code to send the stats/report---
4

1 回答 1

1

尝试 Telepot 源代码树中的第一个示例:https ://github.com/nickoala/telepot/blob/master/examples/skeleton.py

import sys
import time
import telepot

"""
$ python2.7 skeleton.py <token>

A skeleton for your telepot programs.
"""

def handle(msg):
    flavor = telepot.flavor(msg)

    summary = telepot.glance(msg, flavor=flavor)
    print flavor, summary


TOKEN = sys.argv[1]  # get token from command-line

bot = telepot.Bot(TOKEN)
bot.message_loop(handle)
print 'Listening ...'

# Keep the program running.
while 1:
    time.sleep(10)
于 2016-06-25T22:06:15.867 回答