MCPcopy Index your code
hub / github.com/tinode/chat / client_message_loop

Function client_message_loop

chatbot/python/chatbot.py:252–290  ·  view source on GitHub ↗
(stream)

Source from the content-addressed store, hash-verified

250 return stream
251
252def client_message_loop(stream):
253 try:
254 # Read server responses
255 for msg in stream:
256 log(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3], "in:", to_json(msg))
257
258 if msg.HasField("ctrl"):
259 # Run code on command completion
260 exec_future(msg.ctrl.id, msg.ctrl.code, msg.ctrl.text, msg.ctrl.params)
261
262 elif msg.HasField("data"):
263 # log("message from:", msg.data.from_user_id)
264
265 # Protection against the bot talking to self from another session.
266 if msg.data.from_user_id != botUID:
267 # Respond to message.
268 # Mark received message as read
269 client_post(note_read(msg.data.topic, msg.data.seq_id))
270 # Insert a small delay to prevent accidental DoS self-attack.
271 time.sleep(0.1)
272 # Respond with a witty quote
273 client_post(publish(msg.data.topic, next_quote()))
274
275 elif msg.HasField("pres"):
276 # log("presence:", msg.pres.topic, msg.pres.what)
277 # Wait for peers to appear online and subscribe to their topics
278 if msg.pres.topic == 'me':
279 if (msg.pres.what == pb.ServerPres.ON or msg.pres.what == pb.ServerPres.MSG) \
280 and subscriptions.get(msg.pres.src) == None:
281 client_post(subscribe(msg.pres.src))
282 elif msg.pres.what == pb.ServerPres.OFF and subscriptions.get(msg.pres.src) != None:
283 client_post(leave(msg.pres.src))
284
285 else:
286 # Ignore everything else
287 pass
288
289 except grpc._channel._Rendezvous as err:
290 log("Disconnected:", err)
291
292def read_auth_cookie(cookie_file_name):
293 """Read authentication token from a file"""

Callers 1

runFunction · 0.85

Calls 10

logFunction · 0.85
exec_futureFunction · 0.85
client_postFunction · 0.85
note_readFunction · 0.85
publishFunction · 0.85
next_quoteFunction · 0.85
subscribeFunction · 0.85
leaveFunction · 0.85
getMethod · 0.80
to_jsonFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…