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):
# TODO define a better logic (or a customizable strategy) to get the location
return (data.get("physicalAddress", {}) or {}).get("locality") or data.get(
"onlineAddress"
)
if "physicalAddress" in data and data["physicalAddress"]:
addr = data["physicalAddress"]
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):

View File

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

View File

@ -1,2 +1,6 @@
{{ name }}
{{ description }}
*{{ name }}*
🕒 {{ 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
import arrow
import pytest
from mobilizon_reshare.event.event import MobilizonEvent
@ -11,7 +12,7 @@ from mobilizon_reshare.publishers.exceptions import PublisherError, InvalidRespo
@pytest.fixture
def test_event():
now = datetime.now()
now = arrow.now()
return MobilizonEvent(
**{
"name": "TestName",