publishers: zulip: Let the user specify their instance. (#95)

This commit is contained in:
Giacomo Leidi 2021-11-11 16:25:09 +01:00 committed by GitHub
parent 5335ed8cc3
commit f12992c5d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 5 deletions

View File

@ -7,6 +7,7 @@ username="xxx"
active=false active=false
[default.publisher.zulip] [default.publisher.zulip]
active=true active=true
instance="xxx"
chat_id="xxx" chat_id="xxx"
subject="xxx" subject="xxx"
bot_token="xxx" bot_token="xxx"
@ -31,6 +32,7 @@ token="xxx"
username="xxx" username="xxx"
[default.notifier.zulip] [default.notifier.zulip]
active=true active=true
instance="xxx"
chat_id="xxx" chat_id="xxx"
subject="xxx" subject="xxx"
bot_token="xxx" bot_token="xxx"

View File

@ -14,6 +14,7 @@ zulip_validators = [
Validator("notifier.zulip.bot_email", must_exist=True), Validator("notifier.zulip.bot_email", must_exist=True),
] ]
mastodon_validators = [ mastodon_validators = [
Validator("publisher.zulip.instance", must_exist=True),
Validator("notifier.mastodon.instance", must_exist=True), Validator("notifier.mastodon.instance", must_exist=True),
Validator("notifier.mastodon.token", must_exist=True), Validator("notifier.mastodon.token", must_exist=True),
Validator("notifier.mastodon.name", must_exist=True), Validator("notifier.mastodon.name", must_exist=True),

View File

@ -12,6 +12,7 @@ telegram_validators = [
Validator("publisher.telegram.username", must_exist=True), Validator("publisher.telegram.username", must_exist=True),
] ]
zulip_validators = [ zulip_validators = [
Validator("publisher.zulip.instance", must_exist=True),
Validator("publisher.zulip.chat_id", must_exist=True), Validator("publisher.zulip.chat_id", must_exist=True),
Validator("publisher.zulip.subject", must_exist=True), Validator("publisher.zulip.subject", must_exist=True),
Validator("publisher.zulip.msg_template_path", must_exist=True, default=None), Validator("publisher.zulip.msg_template_path", must_exist=True, default=None),

View File

@ -1,3 +1,5 @@
from urllib.parse import urljoin
import pkg_resources import pkg_resources
import requests import requests
from requests import Response from requests import Response
@ -54,8 +56,7 @@ class ZulipPlatform(AbstractPlatform):
""" """
_conf = ("publisher", "zulip") _conf = ("publisher", "zulip")
api_uri = "api/v1/"
api_uri = "https://zulip.twc-italia.org/api/v1/"
name = "zulip" name = "zulip"
def _send_private(self, message: str) -> Response: def _send_private(self, message: str) -> Response:
@ -63,7 +64,7 @@ class ZulipPlatform(AbstractPlatform):
Send private messages Send private messages
""" """
return requests.post( return requests.post(
url=self.api_uri + "messages", url=urljoin(self.conf.instance, self.api_uri) + "messages",
auth=HTTPBasicAuth(self.conf.bot_email, self.conf.bot_token), auth=HTTPBasicAuth(self.conf.bot_email, self.conf.bot_token),
data={"type": "private", "to": f"[{self.user_id}]", "content": message}, data={"type": "private", "to": f"[{self.user_id}]", "content": message},
) )
@ -73,7 +74,7 @@ class ZulipPlatform(AbstractPlatform):
Send stream messages Send stream messages
""" """
return requests.post( return requests.post(
url=self.api_uri + "messages", url=urljoin(self.conf.instance, self.api_uri) + "messages",
auth=HTTPBasicAuth(self.conf.bot_email, self.conf.bot_token), auth=HTTPBasicAuth(self.conf.bot_email, self.conf.bot_token),
data={ data={
"type": "stream", "type": "stream",
@ -88,7 +89,7 @@ class ZulipPlatform(AbstractPlatform):
res = requests.get( res = requests.get(
auth=HTTPBasicAuth(self.conf.bot_email, self.conf.bot_token), auth=HTTPBasicAuth(self.conf.bot_email, self.conf.bot_token),
url=self.api_uri + "users/me", url=urljoin(self.conf.instance, self.api_uri) + "users/me",
) )
data = self._validate_response(res) data = self._validate_response(res)

View File

@ -96,6 +96,9 @@ async def setup_db(event_model_generator, publication_model_generator):
settings["publisher"]["zulip"][ settings["publisher"]["zulip"][
"bot_email" "bot_email"
] = "giacomotest2-bot@zulip.twc-italia.org" ] = "giacomotest2-bot@zulip.twc-italia.org"
settings["publisher"]["zulip"][
"instance"
] = "https://zulip.twc-italia.org"
await update_publishers(["zulip"]) await update_publishers(["zulip"])
publisher = await Publisher.filter(name="zulip").first() publisher = await Publisher.filter(name="zulip").first()