Gracefully handle opt-outs from Twilio
Recently we had our first user opt-out by texting STOP
to our Twilio number, and now Twilio returns a 400
error whenever we try texting that number
Traceback (most recent call last):
File "/var/task/lambda_function/dispatchers/phone.py", line 108, in text
self._client.messages.create(
File "/var/task/twilio/rest/api/v2010/account/message/__init__.py", line 103, in create
payload = self._version.create(method='POST', uri=self._uri, data=data, )
File "/var/task/twilio/base/version.py", line 205, in create
raise self.exception(method, uri, response, 'Unable to create record')
twilio.base.exceptions.TwilioRestException: HTTP 400 error: Unable to create record: Attempt to send to unsubscribed recipient
We hadn't realized this was a feature, and our normal way of opting users out is by deleting their Contact
entries, either by their actions on /alerts/
or automatically after they leave the collaboration.
To handle this more gracefully within EGAD, we should catch this particular error and log a warning rather than an error. We can do this by accessing the code
attribute of the exception, which will be 21610
in this case (Twilio docs).