Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map country code to different Twilio resources #1976

Merged
merged 16 commits into from
May 25, 2023
Merged
Prev Previous commit
Next Next commit
Order for default db sender
  • Loading branch information
mderynck committed May 23, 2023
commit 7a451acee820eb9a23cb4208b202469f9a110589
17 changes: 10 additions & 7 deletions engine/apps/twilioapp/twilio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ def default_twilio_number(self):
def _twilio_sender(self, sender_type, to):
_, _, country_code = self.parse_number(to)
TwilioSender = apps.get_model("twilioapp", "TwilioSender")
sender = (
TwilioSender.objects.instance_of(sender_type)
.filter(Q(country_code=country_code) | Q(country_code__isnull=True))
.order_by("-country_code")
.first()
senders = list(
TwilioSender.objects.instance_of(sender_type).filter(
Q(country_code=country_code) | Q(country_code__isnull=True)
)
)
client = sender.account.get_twilio_api_client() if sender else self.default_twilio_api_client
return client, sender
senders.sort(key=lambda x: (not x.country_code, x))

if senders:
return senders[0].account.get_twilio_api_client(), senders[0]

return self.default_twilio_api_client, None

def _sms_sender(self, to):
client, sender = self._twilio_sender(TwilioSmsSender, to)
Expand Down