【问题标题】:Running a python script real-time and listening for a request in Python 2.7在 Python 2.7 中实时运行 python 脚本并监听请求
【发布时间】:2016-06-23 12:35:24
【问题描述】:

我的 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---

【问题讨论】:

  • 我不太了解 Telepot,但您也许可以为 getUpdates 设置超时。然后只需在循环中使用该超时调用 getUpdates

标签: python-2.7 telegram python-telegram-bot


【解决方案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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2016-04-28
    • 2016-10-31
    • 2020-10-15
    • 1970-01-01
    • 2021-04-26
    相关资源
    最近更新 更多