fear_matrix
fear_matrix

Reputation: 4960

Running a python script real-time and listening for a request in Python 2.7

My python script is installed as a cronjob which runs every minute and sends the report to the user as per the request made (basically my telegram bot interacts with the authorize users and sends the report). Everything works well except it takes 1 minute delay. I want my script to keep listening for a request real-time and sends the report instantly without any delay. I am quite not sure how to do it.

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

Upvotes: 2

Views: 1466

Answers (1)

temoto
temoto

Reputation: 5577

Try the first example in telepot source tree: 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)

Upvotes: 1

Related Questions