From 775fb89cf62c3bf6107079bd4dbcdd6a80088a41 Mon Sep 17 00:00:00 2001 From: Giacomo Leidi Date: Sun, 16 Jul 2023 13:27:00 +0200 Subject: [PATCH] telegram: Add support for topics. (#184) --- mobilizon_reshare/.secrets.toml | 4 +++- mobilizon_reshare/config/notifiers.py | 1 + mobilizon_reshare/config/publishers.py | 1 + mobilizon_reshare/publishers/platforms/telegram.py | 7 ++++++- mobilizon_reshare/settings.toml | 5 +++-- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/mobilizon_reshare/.secrets.toml b/mobilizon_reshare/.secrets.toml index 9e86094..815caa0 100644 --- a/mobilizon_reshare/.secrets.toml +++ b/mobilizon_reshare/.secrets.toml @@ -1,6 +1,7 @@ [default.publisher.telegram] active=true chat_id="xxx" +message_thread_id="xxx" token="xxx" username="xxx" [default.publisher.zulip] @@ -31,6 +32,7 @@ page_access_token="xxx" [default.notifier.telegram] active=true chat_id="xxx" +message_thread_id="xxx" token="xxx" username="xxx" [default.notifier.zulip] @@ -51,4 +53,4 @@ active=false [default.notifier.facebook] active=false -page_access_token="xxx" \ No newline at end of file +page_access_token="xxx" diff --git a/mobilizon_reshare/config/notifiers.py b/mobilizon_reshare/config/notifiers.py index 4322e5d..8f122b7 100644 --- a/mobilizon_reshare/config/notifiers.py +++ b/mobilizon_reshare/config/notifiers.py @@ -4,6 +4,7 @@ from dynaconf import Validator telegram_validators = [ Validator("notifier.telegram.chat_id", must_exist=True), + Validator("notifier.telegram.message_thread_id", default=None), Validator("notifier.telegram.token", must_exist=True), Validator("notifier.telegram.username", must_exist=True), ] diff --git a/mobilizon_reshare/config/publishers.py b/mobilizon_reshare/config/publishers.py index edf3366..4984f4a 100644 --- a/mobilizon_reshare/config/publishers.py +++ b/mobilizon_reshare/config/publishers.py @@ -3,6 +3,7 @@ from dynaconf import Validator telegram_validators = [ Validator("publisher.telegram.chat_id", must_exist=True), + Validator("publisher.telegram.message_thread_id", default=None), Validator("publisher.telegram.msg_template_path", must_exist=True, default=None), Validator("publisher.telegram.recap_template_path", must_exist=True, default=None), Validator( diff --git a/mobilizon_reshare/publishers/platforms/telegram.py b/mobilizon_reshare/publishers/platforms/telegram.py index 3ae5b5f..84715b7 100644 --- a/mobilizon_reshare/publishers/platforms/telegram.py +++ b/mobilizon_reshare/publishers/platforms/telegram.py @@ -99,9 +99,14 @@ class TelegramPlatform(AbstractPlatform): ) def _send(self, message: str, event: Optional[MobilizonEvent] = None) -> Response: + json_message = {"chat_id": self.conf.chat_id, "text": message, "parse_mode": "html"} + + if self.conf.message_thread_id: + json_message["message_thread_id"] = self.conf.message_thread_id + return requests.post( url=f"https://api.telegram.org/bot{self.conf.token}/sendMessage", - json={"chat_id": self.conf.chat_id, "text": message, "parse_mode": "html"}, + json=json_message, ) def _validate_response(self, res): diff --git a/mobilizon_reshare/settings.toml b/mobilizon_reshare/settings.toml index 89f9645..98d47c6 100644 --- a/mobilizon_reshare/settings.toml +++ b/mobilizon_reshare/settings.toml @@ -1,6 +1,7 @@ [default] local_state_dir = "/var/mobilizon_reshare" -db_url = "sqlite:///var/mobilizon_reshare/events.db" +log_dir = "@format {this.local_state_dir}" +db_url = "@format sqlite://{this.local_state_dir}/events.db" locale= "en-us" [default.source.mobilizon] @@ -31,7 +32,7 @@ stream = "ext://sys.stderr" level = "DEBUG" class = "logging.handlers.RotatingFileHandler" formatter = "standard" -filename = "/var/log/mobilizon_reshare/mobilizon_reshare.log" +filename = "@format {this.log_dir}/mobilizon_reshare.log" maxBytes = 52428800 backupCount = 500 encoding = "utf8"