From ccf97282000cd09d22139facbdfcfeee3a897849 Mon Sep 17 00:00:00 2001 From: Luca Date: Fri, 10 May 2024 16:22:12 +0200 Subject: [PATCH] fix(messaging): outbound ClickSend backend --- shiftregister/messaging/backends/clicksend.py | 44 ++++++++----------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/shiftregister/messaging/backends/clicksend.py b/shiftregister/messaging/backends/clicksend.py index 7a33e64..5fdb7a7 100644 --- a/shiftregister/messaging/backends/clicksend.py +++ b/shiftregister/messaging/backends/clicksend.py @@ -1,4 +1,5 @@ import json +from ast import literal_eval from datetime import datetime, timezone from clicksend_client import ApiClient @@ -69,41 +70,32 @@ class Sender(BaseSender): messages = messages[:MAX_BATCH_SIZE] try: - response = ( - SMSApi(client) - .sms_send_post( - SmsMessageCollection( - messages=[ - SmsMessage( - **{ - "from": settings.SMS_SETTINGS[ - "clicksend_sender_id" - ], - "body": message.text, - "to": message.recipient, - "source": "shiftregister", - "custom_string": message.key, - } - ) - for message in messages - ] - ) + response = SMSApi(client).sms_send_post( + SmsMessageCollection( + messages=[ + SmsMessage( + _from=settings.SMS_SETTINGS["clicksend_sender_id"], + body=message.text, + to=message.recipient, + source="shiftregister", + custom_string=message.key, + ) + for message in messages + ] ) - .data ) except ApiException as e: - if e.body: - response = e.body - else: - raise OutboundMessageError(f"{e.status} {e.reason}") + raise OutboundMessageError(f"{e.status} {e.reason}") - response = json.loads(response) + data = literal_eval(response)["data"] - for message in response.get("messages", []): + for message in data.get("messages", []): if message["status"] == "SUCCESS": yield Message( message["custom_string"], recipient=message["to"], sender=message["from"], text=message["body"], + type=MessageType.OUTBOUND, + created_at=datetime.fromtimestamp(message["date"], timezone.utc), )