Added ntfy router #2

Squashed commit of the following:

commit 3ba0c9b4c6
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 05:21:27 2023 -0600

    Another bugfix

commit 8a76bddbf1
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 05:12:40 2023 -0600

    Added ntfy router to ansible scripts

commit 7b51846e1b
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 05:12:14 2023 -0600

    Fixed inconsistent variable names bug

commit a96a14e261
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 04:53:09 2023 -0600

    Added ntfy router to docker compose

commit d2f30faa5e
Author: Ashish D'Souza <[email protected]>
Date:   Sun Nov 19 04:52:07 2023 -0600

    Added ntfy routing
This commit is contained in:
ashish committed 2023-11-19 06:17:01 -06:00
1 parent 1640a7a590
commit a14ae8581a
9 files changed
+219

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
import json
import asyncio
from typing import AsyncGenerator
import aiohttp
class NtfySubscriber:
def __init__(self, topics: list[str]):
self._topics = topics
def _generate_ntfy_url(self):
return 'https://ntfy.homelab.net/' + ','.join(self._topics) + '/json'
async def subscribe(self) -> AsyncGenerator[tuple[str, str, str], None]:
async with aiohttp.ClientSession(raise_for_status=True, timeout=aiohttp.ClientTimeout()) as session:
async with session.get(self._generate_ntfy_url(), verify_ssl=False) as response:
async for line in response.content:
msg = json.loads(line.decode())
if msg['event'] == 'message':
yield msg['topic'], msg['title'], msg['message']