(self)
| 36 | # Sould be executed asynchronously, like with: |
| 37 | # asyncio.create_task(bot.run()) |
| 38 | async def run(self): |
| 39 | while self.active: |
| 40 | if self.reconnect: |
| 41 | if self.debug: print("[telegram] Reconnecting socket.") |
| 42 | # Reconnection (or first connection) |
| 43 | try: |
| 44 | addr = socket.getaddrinfo("api.telegram.org", 443, socket.AF_INET) |
| 45 | addr = addr[0][-1] |
| 46 | self.socket = socket.socket(socket.AF_INET) |
| 47 | self.socket.connect(addr) |
| 48 | self.socket.setblocking(False) |
| 49 | self.ssl = ssl.wrap_socket(self.socket) |
| 50 | self.reconnect = False |
| 51 | self.pending = False |
| 52 | except: |
| 53 | self.reconnect = True |
| 54 | |
| 55 | self.send_api_requests() |
| 56 | self.read_api_response() |
| 57 | |
| 58 | # Watchdog: if the connection is idle for a too long |
| 59 | # time, force a reconnection. |
| 60 | if self.pending and time.ticks_diff(time.ticks_ms(),self.pending_since) > self.watchdog_timeout_ms: |
| 61 | self.reconnect = True |
| 62 | print("[telegram] *** SOCKET WATCHDOG EXPIRED ***") |
| 63 | |
| 64 | # If there are outgoing messages pending, wait less |
| 65 | # to do I/O again. |
| 66 | sleep_time = 0.1 if len(self.outgoing) > 0 else 1.0 |
| 67 | await asyncio.sleep(sleep_time) |
| 68 | |
| 69 | # Send HTTP requests to the server. If there are no special requests |
| 70 | # to handle (like sendMessage) we just ask for updates with getUpdates. |
nothing calls this directly
no test coverage detected