def send_inbound_sms_to_service(self, inbound_sms_id, service_id): inbound_api = get_service_inbound_api_for_service(service_id=service_id) if not inbound_api: current_app.logger.error( f'could not send inbound sms to service "{service_id}" because it does not have a callback API configured' ) return inbound_sms = dao_get_inbound_sms_by_id(service_id=service_id, inbound_id=inbound_sms_id) sms_sender = dao_get_sms_sender_by_service_id_and_number( service_id=service_id, number=inbound_sms.notify_number) payload = { "id": str(inbound_sms.id), # TODO: should we be validating and formatting the phone number here? "source_number": inbound_sms.user_number, "destination_number": inbound_sms.notify_number, "message": inbound_sms.content, "date_received": inbound_sms.provider_date.strftime(DATETIME_FORMAT), "sms_sender_id": str(sms_sender.id) if sms_sender else None } _send_to_service_callback_api(self, payload, inbound_api.url, inbound_api.bearer_token, logging_tags={ 'inbound_sms_id': str(inbound_sms_id), 'service_id': str(service_id) })
def test_get_service_inbound_api_for_service(sample_service): service_inbound_api = create_service_inbound_api(service=sample_service) result = get_service_inbound_api_for_service(sample_service.id) assert result.id == service_inbound_api.id assert result.url == service_inbound_api.url assert result.bearer_token == service_inbound_api.bearer_token assert result.created_at == service_inbound_api.created_at assert result.updated_at == service_inbound_api.updated_at assert result.updated_by_id == service_inbound_api.updated_by_id
def send_inbound_sms_to_service(self, inbound_sms_id, service_id): inbound_api = get_service_inbound_api_for_service(service_id=service_id) if not inbound_api: # No API data has been set for this service return inbound_sms = dao_get_inbound_sms_by_id(service_id=service_id, inbound_id=inbound_sms_id) data = { "id": str(inbound_sms.id), # TODO: should we be validating and formatting the phone number here? "source_number": inbound_sms.user_number, "destination_number": inbound_sms.notify_number, "message": inbound_sms.content, "date_received": inbound_sms.provider_date.strftime(DATETIME_FORMAT) } try: response = request(method="POST", url=inbound_api.url, data=json.dumps(data), headers={ 'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(inbound_api.bearer_token) }, timeout=60) current_app.logger.debug( f"send_inbound_sms_to_service sending {inbound_sms_id} to {inbound_api.url}, " + f"response {response.status_code}") response.raise_for_status() except RequestException as e: current_app.logger.warning( f"send_inbound_sms_to_service failed for service_id: {service_id} for inbound_sms_id: {inbound_sms_id} " + f"and url: {inbound_api.url}. exception: {e}") if not isinstance(e, HTTPError) or e.response.status_code >= 500: try: self.retry(queue=QueueNames.RETRY) except self.MaxRetriesExceededError: current_app.logger.error( "Retry: send_inbound_sms_to_service has retried the max number of" + f"times for service: {service_id} and inbound_sms {inbound_sms_id}" ) else: current_app.logger.warning( f"send_inbound_sms_to_service is not being retried for service_id: {service_id} for " + f"inbound_sms id: {inbound_sms_id} and url: {inbound_api.url}. exception: {e}" )
def send_inbound_sms_to_service(self, inbound_sms_id, service_id): inbound_api = get_service_inbound_api_for_service(service_id=service_id) if not inbound_api: # No API data has been set for this service return inbound_sms = dao_get_inbound_sms_by_id(service_id=service_id, inbound_id=inbound_sms_id) data = { "id": str(inbound_sms.id), "source_number": inbound_sms.user_number, "destination_number": inbound_sms.notify_number, "message": inbound_sms.content, "date_received": inbound_sms.provider_date.strftime(DATETIME_FORMAT) } try: response = request( method="POST", url=inbound_api.url, data=json.dumps(data), headers={ 'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format(inbound_api.bearer_token) }, timeout=60 ) current_app.logger.debug('send_inbound_sms_to_service sending {} to {}, response {}'.format( inbound_sms_id, inbound_api.url, response.status_code )) response.raise_for_status() except RequestException as e: current_app.logger.warning( "send_inbound_sms_to_service request failed for service_id: {} and url: {}. exc: {}".format( service_id, inbound_api.url, e ) ) if not isinstance(e, HTTPError) or e.response.status_code >= 500: try: self.retry(queue=QueueNames.RETRY) except self.MaxRetriesExceededError: current_app.logger.exception('Retry: send_inbound_sms_to_service has retried the max number of times')