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
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),
)