def send_au_sms(to, body, app_name=None): counter = r.get(TELSTRA_SMS_MONTHLY_COUNTER) or 0 counter = int(counter) if not counter < TELSTRA_MONTHLY_FREE_LIMIT: log.info('[SMS] Telstra SMS reach 1000 free limitation.') return False, 'Telstra SMS reach 1000 free limitation.' to = validate_au_mobile(to) if not to: return False, 'INVALID_PHONE_NUMBER' body = str(body) if not body: return False, 'EMPTY_CONTENT' # Configure OAuth2 access token for authorization: auth configuration = Telstra_Messaging.Configuration() configuration.access_token = get_token() from_number = get_from_number() # api_instance = Telstra_Messaging.ProvisioningApi(Telstra_Messaging.ApiClient(configuration)) # provision_number_request = Telstra_Messaging.ProvisionNumberRequest() # ProvisionNumberRequest | A JSON payload containing the required attributes api_instance = Telstra_Messaging.MessagingApi( Telstra_Messaging.ApiClient(configuration)) send_sms_request = Telstra_Messaging.SendSMSRequest(to, body, from_number) try: # {'country': [{u'AUS': 1}], # 'message_type': 'SMS', # 'messages': [{'delivery_status': 'MessageWaiting', # 'message_id': 'd872ad3b000801660000000000462650037a0801-1261413725868', # 'message_status_url': 'https://tapi.telstra.com/v2/messages/sms/d872ad3b000801660000000000462650037a0801-1261413725868/status', # 'to': '+61413725868'}], # 'number_segments': 1} api_response = api_instance.send_sms(send_sms_request) success = api_response.messages[0].delivery_status == 'MessageWaiting' if connection.schema_name.startswith(Tenant.SCHEMA_NAME_PREFIX): sms = Sms(app_name=app_name, send_to=to, content=body, success=success, template_code=api_response.messages[0].delivery_status, remark=api_response.messages[0].message_status_url, biz_id=api_response.messages[0].delivery_status) sms.save() if success: counter = r.get(TELSTRA_SMS_MONTHLY_COUNTER) or 0 counter = int(counter) counter += 1 r.set(TELSTRA_SMS_MONTHLY_COUNTER, counter) if counter == TELSTRA_MONTHLY_FREE_LIMIT - 1: send_to_admin('[Warning] Telstra sms meet monthly limitation.') r.set(TELSTRA_SMS_MONTHLY_COUNTER, counter + 1) return True, 'MessageWaiting' except ApiException as e: log.error("Exception when calling MessagingApi->send_sms: %s\n" % e) return False, str(e)
def sendSMS(self, recipient, message): 'Sends a SMS to the given recipient with the given message.' self.__checkAuthToken() api_instance_Messaging = Telstra_Messaging.MessagingApi( Telstra_Messaging.ApiClient(self.configuration)) payload = Telstra_Messaging.SendSMSRequest(recipient, message) try: api_response = api_instance_Messaging.send_sms(payload) pprint(api_response) except ApiException as e: print("Exception when calling MessagingApi->send_sms: %s\n" % e)
def send_au_sms(mobile_number, message, from_app=None): counter = redis_counter.get_telstra_monthly_counter() counter = int(counter) if not counter < TELSTRA_MONTHLY_FREE_LIMIT: logger.info('[SMS] Telstra SMS reach 1000 free limitation.') return False, 'Telstra SMS reach 1000 free limitation.' mobile_number = validate_au_mobile(mobile_number) if not mobile_number: return False, 'INVALID_PHONE_NUMBER' message = str(message) if not message: return False, 'EMPTY_CONTENT' # Configure OAuth2 access token for authorization: auth configuration = Telstra_Messaging.Configuration() configuration.access_token = get_token() from_number = _get_from_number() # api_instance = Telstra_Messaging.ProvisioningApi(Telstra_Messaging.ApiClient(configuration)) # provision_number_request = Telstra_Messaging.ProvisionNumberRequest() # ProvisionNumberRequest | A JSON payload containing the required attributes api_instance = Telstra_Messaging.MessagingApi( Telstra_Messaging.ApiClient(configuration)) send_sms_request = Telstra_Messaging.SendSMSRequest( mobile_number, message, from_number) try: # {'country': [{u'AUS': 1}], # 'message_type': 'SMS', # 'messages': [{'delivery_status': 'MessageWaiting', # 'message_id': 'd872ad3b000801660000000000462650037a0801-1261413725868', # 'message_status_url': 'https://tapi.telstra.com/v2/messages/sms/d872ad3b000801660000000000462650037a0801-1261413725868/status', # 'to': '+61413725868'}], # 'number_segments': 1} api_response = api_instance.send_sms(send_sms_request) success = api_response.messages[0].delivery_status == 'MessageWaiting' if success: counter = redis_counter.get_telstra_monthly_counter() counter = int(counter) counter += 1 redis_counter.set_telstra_monthly_counter(counter) if counter == TELSTRA_MONTHLY_FREE_LIMIT - 1: send_to_admin('[Warning] Telstra sms meet monthly limitation.') redis_counter.set_telstra_monthly_counter(counter + 1) return True, 'MessageWaiting' except ApiException as e: logger.error("Exception when calling MessagingApi->send_sms: %s\n" % e) return False, str(e)
def send_sms(self, msg_to, msg_body): api_instance = Telstra_Messaging.MessagingApi( Telstra_Messaging.ApiClient(self.configuration)) send_sms_request = Telstra_Messaging.SendSMSRequest(to=msg_to, body=msg_body) try: # Send SMS api_response = api_instance.send_sms(send_sms_request) # pprint(api_response) return True except ApiException as e: print("Exception when calling MessagingApi->send_sms: %s\n" % e)
def send_sms(self, to, content, reference, sender=None): telstra_api = telstra.MessagingApi(self._client) notify_url = f"{self.notify_host}/notifications/sms/telstra/{reference}" if self.notify_host else None payload = telstra.SendSMSRequest(to=to, body=content, _from=sender if sender else None, notify_url=notify_url) # Avoid circular imports by importing this file later. from app.models import ( NOTIFICATION_SENDING, NOTIFICATION_PERMANENT_FAILURE, ) try: resp = telstra_api.send_sms(payload) message = resp.messages[0] self.logger.info( f"Telstra send SMS request for {reference} succeeded: {message.message_id}" ) return message.message_id, NOTIFICATION_SENDING except telstra.rest.ApiException as e: try: messages = json.loads(e.body)["messages"] if e.status == 400 and len(messages) > 0 and messages[0][ "code"] == "TO-MSISDN-NOT-VALID" and messages[0][ "deliveryStatus"] == "DeliveryImpossible": self.logger.info( f"Telstra send SMS request for {reference} failed with API exception, status: {e.status}, reason: {e.reason}, message code: {messages[0]['code']}, message delivery status: {messages[0]['deliveryStatus']}" ) return None, NOTIFICATION_PERMANENT_FAILURE except ValueError: pass self.logger.error( f"Telstra send SMS request for {reference} failed with API exception" ) raise e except Exception as e: self.logger.error( f"Telstra send SMS request for {reference} failed") raise e
def send_sms(self, to, content, reference, sender=None): telstra_api = telstra.MessagingApi(self._client) notify_host = self._callback_notify_url_host notify_url = f"{notify_host}/notifications/sms/telstra/{reference}" if notify_host else None payload = telstra.SendSMSRequest(to=to, body=content, _from=sender if sender else None, notify_url=notify_url) try: resp = telstra_api.send_sms(payload) message = resp.messages[0] self.logger.info( f"Telstra send SMS request for {reference} succeeded: {message.message_id}" ) except Exception as e: self.logger.error( f"Telstra send SMS request for {reference} failed") raise e
def send_sms(self, to, content, reference, sender=None): conf = self.auth() api_instance = Telstra_Messaging.MessagingApi(Telstra_Messaging.ApiClient(conf)) payload = Telstra_Messaging.SendSMSRequest( to=to, body=content, notify_url="{}/notifications/sms/telstra/{}".format(self._callback_notify_url_host, reference) ) start_time = monotonic() try: resp = api_instance.send_sms(payload) message = resp.messages[0] self.logger.info("Telstra send SMS request for {} succeeded: {}".format(reference, message.message_id)) except Exception as e: self.logger.error("Telstra send SMS request for {} failed".format(reference)) raise e finally: elapsed_time = monotonic() - start_time self.logger.info("Telstra send SMS request for {} finished in {}".format(reference, elapsed_time))