diff --git a/ntfy/bot.py b/ntfy/bot.py index c5681ea..2414e99 100644 --- a/ntfy/bot.py +++ b/ntfy/bot.py @@ -160,7 +160,9 @@ class NtfyBot(Plugin): line = line.decode("utf-8").strip() self.log.trace("Received notification: %s", line) message = json.loads(line) - if message["event"] != "message": + if not "event" in message or message["event"] != "message": + if "error" in message: + await self.handle_subscription_error(message, topic, self.tasks[topic.id]) continue self.log.debug("Received message event: %s", line) # persist the received message id @@ -186,6 +188,27 @@ class NtfyBot(Plugin): self.log.exception( "Failed to send matrix message!", exc_info=exc) + async def handle_subscription_error(self, message: dict, topic: Topic, + task: asyncio.Task) -> None: + self.log.error(message["error"]) + code = message["code"] if "code" in message else None + match code: + case 42909: + if task.cancel(): + for sub in await self.db.get_subscriptions(topic.id): + html_content = "Authentication failed, please check the access token. Subscription has been cancelled." + text_content = await parse_html(html_content.strip()) + content = TextMessageEventContent( + msgtype=MessageType.TEXT, + format=Format.HTML, + formatted_body=html_content, + body=text_content, + ) + await self.client.send_message(sub.room_id, content) + else: + self.log.error("Failed to cancel subscription task") + + def build_message_content(self, server: str, message) -> str: topic = message["topic"] body = message["message"]