diff bots/bigastbot.py @ 1:2a288d2cb88c

add unread_to_mqtt bridge
author drewp@bigasterisk.com
date Tue, 11 Feb 2025 19:20:47 -0800
parents 96f842f12121
children
line wrap: on
line diff
--- a/bots/bigastbot.py	Tue Jan 28 23:30:02 2025 -0800
+++ b/bots/bigastbot.py	Tue Feb 11 19:20:47 2025 -0800
@@ -1,3 +1,6 @@
+import asyncio
+from typing import AsyncGenerator, cast
+
 import zulip
 from kubernetes import client, config
 
@@ -35,3 +38,19 @@
                    topic=topic,
                    content=content)
         return self.zulip_client.send_message(msg)
+
+    async def get_registration_and_events(
+            self, **register_kw) -> AsyncGenerator[dict, None]:
+        """yields the registration response, then the events as they come"""
+        reg = self.zulip_client.register(**register_kw)
+        yield reg
+
+        last = reg['last_event_id']
+        while True:
+            update = self.zulip_client.get_events(queue_id=reg['queue_id'],
+                                                  last_event_id=last)
+            for ev in cast(list[dict], update['events']):
+                yield ev
+                last = max(last, ev['id'])
+
+            await asyncio.sleep(1)