fix(messaging): outbound ClickSend backend

pull/1/head
Luca 8 months ago
parent 908c3d21cc
commit ccf9728200

@ -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) SmsMessageCollection(
.sms_send_post( messages=[
SmsMessageCollection( SmsMessage(
messages=[ _from=settings.SMS_SETTINGS["clicksend_sender_id"],
SmsMessage( body=message.text,
**{ to=message.recipient,
"from": settings.SMS_SETTINGS[ source="shiftregister",
"clicksend_sender_id" custom_string=message.key,
], )
"body": message.text, for message in messages
"to": message.recipient, ]
"source": "shiftregister",
"custom_string": message.key,
}
)
for message in messages
]
)
) )
.data
) )
except ApiException as e: except ApiException as e:
if e.body: raise OutboundMessageError(f"{e.status} {e.reason}")
response = e.body
else:
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),
) )

Loading…
Cancel
Save