MCPcopy Create free account
hub / github.com/antirez/freakwan / process_api_response

Method process_api_response

telegram.py:142–204  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

140 # and resets the buffer. If the JSON reply is an incoming message, the
141 # user callback is invoked.
142 def process_api_response(self):
143 if self.rbuf_used > 0:
144 # Discard the HTTP request header by looking for the
145 # start of the json message.
146 start_idx = self.rbuf.find(b"{")
147 if start_idx != -1:
148 # It is possible that we read a non complete reply
149 # from the socket. In such case the JSON message
150 # will be broken and will produce a ValueError.
151 try:
152 mybuf = self.decode_surrogate_pairs(self.rbuf[start_idx:self.rbuf_used])
153 res = json.loads(mybuf)
154 except ValueError:
155 res = False
156 if res != False:
157 self.pending = False
158 if len(res['result']) == 0:
159 # Empty result set. Try again.
160 if self.debug: print("No more messages.")
161 elif not isinstance(res['result'],list):
162 # This is the reply to SendMessage or other
163 # non getUpdates related API calls? Discard
164 # it.
165 if self.debug: print("Got reply from sendMessage")
166 pass
167 else:
168 # Update the last message ID we get so we
169 # will get only next ones.
170 offset = res['result'][0]['update_id']
171 offset += 1
172 self.offset = offset
173 if self.debug: print("New offset:",offset)
174
175 # Process the received message.
176 entry = res['result'][0]
177 if "message" in entry:
178 msg = entry['message']
179 elif "channel_post" in entry:
180 msg = entry['channel_post']
181
182 # Fill the fields depending on the message
183 msg_type = None
184 chat_name = None
185 sender_name = None
186 chat_id = None
187 text = None
188
189 try: msg_type = msg['chat']['type']
190 except: pass
191 try: chat_name = msg['chat']['title']
192 except: pass
193 try: sender_name = msg['from']['username']
194 except: pass
195 try: chat_id = msg['chat']['id']
196 except: pass
197 try: text = msg['text']
198 except: pass
199

Callers 1

read_api_responseMethod · 0.95

Calls 1

Tested by

no test coverage detected