#print("Make request: {0}".format(last_update_id)) try: r = requests.post( config.API_URL + "getUpdates" , json={ "offset" : last_update_id + 1 , "timeout" : config.TIMEOUT } , timeout=config.TIMEOUT + 5 ) result = r.json() if result["ok"]: limit = 10 for update in result["result"]: limit-=1 if limit <= 0: break update_id = update["update_id"] if update_id > last_update_id: last_update_id = update_id methods.processMessage(update["message"]) server_retry = config.SERVER_RETRY_TIMEOUT else: # TODO error handling print(result) except (ValueError, requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout) as err: print("Connection Error {0}\nRetrying in {1} seconds".format(err, server_retry)) time.sleep(server_retry) server_retry *= 2 #print(result)
r = requests.post(config.API_URL + "getUpdates", json={ "offset": last_update_id + 1, "timeout": config.TIMEOUT }, timeout=config.TIMEOUT + 5) result = r.json() if result["ok"]: limit = 10 for update in result["result"]: limit -= 1 if limit <= 0: break update_id = update["update_id"] if update_id > last_update_id: last_update_id = update_id methods.processMessage(update["message"], args.console) server_retry = config.SERVER_RETRY_TIMEOUT else: # TODO error handling print(result) except (ValueError, requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout) as err: print("Connection Error {0}\nRetrying in {1} seconds".format( err, server_retry)) time.sleep(server_retry) server_retry *= 2 #print(result)
Sample response { "order": ["post_id1","post_id12"], "posts": { "post_id12": {... content ... }, "post_id1": {... content ... } } } ''' # If result obtained and pending posts exist if r.status_code == 200 and result["order"] != []: post_ids = result["order"] # Get only the last post content last_post_id = post_ids[0] # If last_post is unread, read and set this post as the last post if last_post_id != last_update_id: last_update_id = last_post_id # Process post content. ProcessMessage(message, root_id) methods.processMessage(result["posts"][last_update_id]['message'], last_update_id) # Sleep for specified time time.sleep(config.TIMEOUT) except KeyboardInterrupt: print("Terminating Server") break
import atexit last_update_id = 0 methods.startupMessage() atexit.register(methods.shutdownMessage) while True: methods.alarms() # print("Make request: {0}".format(last_update_id)) r = requests.post( config.API_URL + "getUpdates", json={"offset": last_update_id + 1, "timeout": config.TIMEOUT}, timeout=config.TIMEOUT + 5, ) result = r.json() if result["ok"]: for update in result["result"]: update_id = update["update_id"] if update_id > last_update_id: last_update_id = update_id methods.processMessage(update["message"]) else: # TODO error handling print(result) # print(result)