2
0
Fork 0

fix(messaging): outbound ClickSend backend

This commit is contained in:
Luca 2024-05-10 16:22:12 +02:00
parent 908c3d21cc
commit ccf9728200
1 changed files with 18 additions and 26 deletions

View File

@ -1,4 +1,5 @@
import json import json
from ast import literal_eval
from datetime import datetime, timezone from datetime import datetime, timezone
from clicksend_client import ApiClient from clicksend_client import ApiClient
@ -69,41 +70,32 @@ class Sender(BaseSender):
messages = messages[:MAX_BATCH_SIZE] messages = messages[:MAX_BATCH_SIZE]
try: try:
response = ( response = SMSApi(client).sms_send_post(
SMSApi(client)
.sms_send_post(
SmsMessageCollection( SmsMessageCollection(
messages=[ messages=[
SmsMessage( SmsMessage(
**{ _from=settings.SMS_SETTINGS["clicksend_sender_id"],
"from": settings.SMS_SETTINGS[ body=message.text,
"clicksend_sender_id" to=message.recipient,
], source="shiftregister",
"body": message.text, custom_string=message.key,
"to": message.recipient,
"source": "shiftregister",
"custom_string": message.key,
}
) )
for message in messages for message in messages
] ]
) )
) )
.data
)
except ApiException as e: 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": if message["status"] == "SUCCESS":
yield Message( yield Message(
message["custom_string"], message["custom_string"],
recipient=message["to"], recipient=message["to"],
sender=message["from"], sender=message["from"],
text=message["body"], text=message["body"],
type=MessageType.OUTBOUND,
created_at=datetime.fromtimestamp(message["date"], timezone.utc),
) )