Created
February 10, 2025 21:27
-
-
Save drewp/e20921f44fe35fa794266df226996f2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import asyncio | |
| import logging | |
| from pprint import pprint | |
| from typing import cast | |
| import aiomqtt | |
| from bigastbot import BigAstBot | |
| logging.basicConfig(level=logging.INFO, | |
| format='%(asctime)s %(levelname)s %(name)s %(message)s', | |
| datefmt='%Y-%m-%d %H:%M:%S') | |
| log = logging.getLogger() | |
| async def main(): | |
| email = '[email protected]' | |
| bot = BigAstBot(email=email) | |
| unread_msg_ids = set() | |
| reg = (bot.zulip_client.register(event_types=[ | |
| 'message', | |
| 'update_message_flags', | |
| ])) | |
| update_unreads_with_event(email, unread_msg_ids, reg) | |
| last = reg['last_event_id'] | |
| while True: | |
| update = bot.zulip_client.get_events(queue_id=reg['queue_id'], | |
| last_event_id=last) | |
| for ev in update['events']: | |
| last = ev['id'] | |
| update_unreads_with_event(email, unread_msg_ids, ev) | |
| await asyncio.sleep(1) | |
| def update_unreads_with_event(email, unread_msg_ids, ev): | |
| prev_count = len(unread_msg_ids) | |
| if 'unread_msgs' in ev: | |
| # looks like registration response | |
| unread_msg_ids.update( | |
| sum((cast(list[int], st['unread_message_ids']) | |
| for st in ev['unread_msgs']['streams']), [])) | |
| elif ev['type'] == 'message': | |
| if 'read' not in ev['flags']: | |
| unread_msg_ids.add(ev['message']['id']) | |
| elif ev['type'] == 'update_message_flags': | |
| if ev['flag'] == 'read': | |
| for msg_id in ev['messages']: | |
| unread_msg_ids.discard(msg_id) | |
| if prev_count != len(unread_msg_ids): | |
| send(email, len(unread_msg_ids)) | |
| def send(email, num_unread): | |
| print(email, 'unreads changed to ', num_unread) | |
| asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment