def send_sample_sms(self, data): request_token = data.get("request_token") if not self.get_cached_webhook_secret(request_token): return {"success": False, "unexpected_error": self.unexpected_error} outgoing_sms_form = TelerivetOutgoingSMSForm( {"api_key": data.get("api_key"), "project_id": data.get("project_id"), "phone_id": data.get("phone_id")} ) test_sms_form = TelerivetPhoneNumberForm({"test_phone_number": data.get("test_phone_number")}) # Be sure to call .is_valid() on both outgoing_sms_form_valid = outgoing_sms_form.is_valid() test_sms_form_valid = test_sms_form.is_valid() if not outgoing_sms_form_valid or not test_sms_form_valid: return { "success": False, "api_key_error": self.get_error_message(outgoing_sms_form, "api_key"), "project_id_error": self.get_error_message(outgoing_sms_form, "project_id"), "phone_id_error": self.get_error_message(outgoing_sms_form, "phone_id"), "test_phone_number_error": self.get_error_message(test_sms_form, "test_phone_number"), } tmp_backend = SQLTelerivetBackend() tmp_backend.set_extra_fields( api_key=outgoing_sms_form.cleaned_data.get("api_key"), project_id=outgoing_sms_form.cleaned_data.get("project_id"), phone_id=outgoing_sms_form.cleaned_data.get("phone_id"), ) sms = SMS( phone_number=clean_phone_number(test_sms_form.cleaned_data.get("test_phone_number")), text="This is a test SMS from CommCareHQ.", ) tmp_backend.send(sms) return {"success": True}
def send_sample_sms(request, domain): request_token = request.POST.get('request_token') if not TelerivetSetupView.get_cached_webhook_secret(request_token): return { 'success': False, 'unexpected_error': TelerivetSetupView.unexpected_error, } outgoing_sms_form = TelerivetOutgoingSMSForm({ 'api_key': request.POST.get('api_key'), 'project_id': request.POST.get('project_id'), 'phone_id': request.POST.get('phone_id'), }) test_sms_form = TelerivetPhoneNumberForm({ 'test_phone_number': request.POST.get('test_phone_number'), }) # Be sure to call .is_valid() on both outgoing_sms_form_valid = outgoing_sms_form.is_valid() test_sms_form_valid = test_sms_form.is_valid() if not outgoing_sms_form_valid or not test_sms_form_valid: return json_response({ 'success': False, 'api_key_error': TelerivetSetupView.get_error_message(outgoing_sms_form, 'api_key'), 'project_id_error': TelerivetSetupView.get_error_message(outgoing_sms_form, 'project_id'), 'phone_id_error': TelerivetSetupView.get_error_message(outgoing_sms_form, 'phone_id'), 'test_phone_number_error': TelerivetSetupView.get_error_message(test_sms_form, 'test_phone_number'), }) tmp_backend = SQLTelerivetBackend() tmp_backend.set_extra_fields( api_key=outgoing_sms_form.cleaned_data.get('api_key'), project_id=outgoing_sms_form.cleaned_data.get('project_id'), phone_id=outgoing_sms_form.cleaned_data.get('phone_id'), ) sms = SMS(phone_number=clean_phone_number( test_sms_form.cleaned_data.get('test_phone_number')), text="This is a test SMS from CommCareHQ.") tmp_backend.send(sms) return json_response({ 'success': True, })
def send_sample_sms(request, domain): request_token = request.POST.get('request_token') if not TelerivetSetupView.get_cached_webhook_secret(request_token): return { 'success': False, 'unexpected_error': TelerivetSetupView.unexpected_error, } outgoing_sms_form = TelerivetOutgoingSMSForm({ 'api_key': request.POST.get('api_key'), 'project_id': request.POST.get('project_id'), 'phone_id': request.POST.get('phone_id'), }) test_sms_form = TelerivetPhoneNumberForm({ 'test_phone_number': request.POST.get('test_phone_number'), }) # Be sure to call .is_valid() on both outgoing_sms_form_valid = outgoing_sms_form.is_valid() test_sms_form_valid = test_sms_form.is_valid() if not outgoing_sms_form_valid or not test_sms_form_valid: return json_response({ 'success': False, 'api_key_error': TelerivetSetupView.get_error_message(outgoing_sms_form, 'api_key'), 'project_id_error': TelerivetSetupView.get_error_message(outgoing_sms_form, 'project_id'), 'phone_id_error': TelerivetSetupView.get_error_message(outgoing_sms_form, 'phone_id'), 'test_phone_number_error': TelerivetSetupView.get_error_message(test_sms_form, 'test_phone_number'), }) tmp_backend = SQLTelerivetBackend() tmp_backend.set_extra_fields( api_key=outgoing_sms_form.cleaned_data.get('api_key'), project_id=outgoing_sms_form.cleaned_data.get('project_id'), phone_id=outgoing_sms_form.cleaned_data.get('phone_id'), ) sms = SMS( phone_number=clean_phone_number(test_sms_form.cleaned_data.get('test_phone_number')), text="This is a test SMS from CommCareHQ." ) tmp_backend.send(sms) return json_response({ 'success': True, })
def page_context(self): # The webhook secret is a piece of data that is sent to hq on each # Telerivet inbound request. It's used to tie an inbound request to # a Telerivet backend. webhook_secret = uuid.uuid4().hex # The request token is only used for the purposes of using this UI to # setup a Telerivet backend. We need a way to post the webhook_secret # to create the backend, but we want hq to be the origin of the secret # generation. So instead, the request_token resolves to the webhook_secret # via a redis lookup which expires in 1 week. request_token = uuid.uuid4().hex TelerivetSetupView.set_cached_webhook_secret(request_token, webhook_secret) domain_has_default_gateway = SQLMobileBackend.get_domain_default_backend( SQLMobileBackend.SMS, self.domain, id_only=True) is not None webhook_url = absolute_reverse('telerivet_in') return { 'outgoing_sms_form': TelerivetOutgoingSMSForm(), 'test_sms_form': TelerivetPhoneNumberForm(), 'finalize_gateway_form': FinalizeGatewaySetupForm( initial={ 'name': 'TELERIVET', 'set_as_default': (FinalizeGatewaySetupForm.NO if domain_has_default_gateway else FinalizeGatewaySetupForm.YES), }), 'webhook_url': webhook_url, 'include_https_notice': webhook_url.startswith('https'), 'webhook_secret': webhook_secret, 'request_token': request_token, 'gateway_list_url': reverse(DomainSmsGatewayListView.urlname, args=[self.domain]), }