(self)
| 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. |
| 71 | def send_api_requests(self): |
| 72 | if self.pending: return # Request already in progress. |
| 73 | request = None |
| 74 | |
| 75 | # Re-issue a pending write that failed for OS error |
| 76 | # after a reconnection. |
| 77 | if self.missed_write != None: |
| 78 | request = self.missed_write |
| 79 | self.missed_write = None |
| 80 | |
| 81 | # Issue sendMessage requests if we have pending |
| 82 | # messages to deliver. |
| 83 | elif len(self.outgoing) > 0: |
| 84 | oldest = self.outgoing.pop() |
| 85 | request = self.build_post_request("sendMessage",oldest) |
| 86 | |
| 87 | # Issue a new getUpdates request if there is not |
| 88 | # some request still pending. |
| 89 | else: |
| 90 | # Limit the fetch to a single message since we are using |
| 91 | # a fixed 4k buffer. Very large incoming messages will break |
| 92 | # the reading loop: that's a trade off. |
| 93 | request = "GET /bot"+self.token+"/getUpdates?offset="+str(self.offset)+"&timeout=0&allowed_udpates=message&limit=1 HTTP/1.1\r\nHost:api.telegram.org\r\n\r\n" |
| 94 | |
| 95 | # Write the request to the SSL socket. |
| 96 | # |
| 97 | # Here we assume that the output buffer has enough |
| 98 | # space available, since this is sent either at startup |
| 99 | # or when we already received a reply. In both the |
| 100 | # situations the socket buffer should be empty and |
| 101 | # this request should work without sending just part |
| 102 | # of the request. |
| 103 | if request != None: |
| 104 | if self.debug: print("[telegram] Writing payload:",request) |
| 105 | try: |
| 106 | self.ssl.write(request) |
| 107 | self.pending = True |
| 108 | self.pending_since = time.ticks_ms() |
| 109 | except: |
| 110 | self.reconnect = True |
| 111 | self.missed_write = request |
| 112 | |
| 113 | # Try to read the reply from the Telegram server. Process it |
| 114 | # and if needed ivoke the callback registered by the user for |
no test coverage detected