Telegram format (#64)

* improved template

* improved location format
This commit is contained in:
Simone Robutti 2021-09-27 09:20:01 +02:00 committed by GitHub
parent 6654d13ed0
commit 3258e1b1c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 17 deletions

View File

@ -19,10 +19,13 @@ class MobilizonRequestFailed(Exception):
def parse_location(data): def parse_location(data):
# TODO define a better logic (or a customizable strategy) to get the location if "physicalAddress" in data and data["physicalAddress"]:
return (data.get("physicalAddress", {}) or {}).get("locality") or data.get( addr = data["physicalAddress"]
"onlineAddress" return f"{addr['description']}, {addr['locality']}, {addr['region']}"
) elif "onlineAddress" in data and data["onlineAddress"]:
return data["onlineAddress"]
else:
return None
def parse_picture(data): def parse_picture(data):

View File

@ -23,12 +23,14 @@ class TelegramPublisher(AbstractPublisher):
) )
def _escape_message(self, message: str) -> str: def _escape_message(self, message: str) -> str:
return ( message = (
message.replace("-", "\\-") message.replace("-", "\\-")
.replace(".", "\\.") .replace(".", "\\.")
.replace("(", "\\(") .replace("(", "\\(")
.replace(")", "\\)") .replace(")", "\\)")
.replace("#", "")
) )
return message
def _send(self, message: str) -> Response: def _send(self, message: str) -> Response:
return requests.post( return requests.post(
@ -54,8 +56,7 @@ class TelegramPublisher(AbstractPublisher):
err.append("username") err.append("username")
if err: if err:
self._log_error( self._log_error(
", ".join(err) + " is/are missing", ", ".join(err) + " is/are missing", raise_error=InvalidCredentials,
raise_error=InvalidCredentials,
) )
res = requests.get(f"https://api.telegram.org/bot{token}/getMe") res = requests.get(f"https://api.telegram.org/bot{token}/getMe")
@ -63,8 +64,7 @@ class TelegramPublisher(AbstractPublisher):
if not username == data.get("result", {}).get("username"): if not username == data.get("result", {}).get("username"):
self._log_error( self._log_error(
"Found a different bot than the expected one", "Found a different bot than the expected one", raise_error=InvalidBot,
raise_error=InvalidBot,
) )
def validate_event(self) -> None: def validate_event(self) -> None:
@ -77,8 +77,7 @@ class TelegramPublisher(AbstractPublisher):
res.raise_for_status() res.raise_for_status()
except requests.exceptions.HTTPError as e: except requests.exceptions.HTTPError as e:
self._log_error( self._log_error(
f"Server returned invalid data: {str(e)}", f"Server returned invalid data: {str(e)}", raise_error=InvalidResponse,
raise_error=InvalidResponse,
) )
try: try:
@ -91,8 +90,7 @@ class TelegramPublisher(AbstractPublisher):
if not data.get("ok"): if not data.get("ok"):
self._log_error( self._log_error(
f"Invalid request (response: {data})", f"Invalid request (response: {data})", raise_error=InvalidResponse,
raise_error=InvalidResponse,
) )
return data return data

View File

@ -1,2 +1,6 @@
{{ name }} *{{ name }}*
{{ description }}
🕒 {{ begin_datetime.format('DD MMMM, HH:mm') }} - {{ end_datetime.format('DD MMMM, HH:mm') }}
{% if location %}📍 {{ location }}{% endif %}
{{ description }}

View File

@ -1,6 +1,7 @@
from datetime import datetime, timedelta from datetime import timedelta
from uuid import UUID from uuid import UUID
import arrow
import pytest import pytest
from mobilizon_reshare.event.event import MobilizonEvent from mobilizon_reshare.event.event import MobilizonEvent
@ -11,7 +12,7 @@ from mobilizon_reshare.publishers.exceptions import PublisherError, InvalidRespo
@pytest.fixture @pytest.fixture
def test_event(): def test_event():
now = datetime.now() now = arrow.now()
return MobilizonEvent( return MobilizonEvent(
**{ **{
"name": "TestName", "name": "TestName",