本文整理汇总了Python中temba.contacts.models.URN.normalize_number方法的典型用法代码示例。如果您正苦于以下问题:Python URN.normalize_number方法的具体用法?Python URN.normalize_number怎么用?Python URN.normalize_number使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类temba.contacts.models.URN
的用法示例。
在下文中一共展示了URN.normalize_number方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize_number [as 别名]
def clean(self):
# first check that our phone number looks sane
number, valid = URN.normalize_number(self.cleaned_data["number"], self.cleaned_data["country"])
if not valid:
raise forms.ValidationError(_("Please enter a valid phone number"))
self.cleaned_data["number"] = number
try:
resp = requests.post(
self.cleaned_data["base_url"] + "/v1/users/login",
auth=(self.cleaned_data["username"], self.cleaned_data["password"]),
)
if resp.status_code != 200:
raise Exception("Received non-200 response: %d", resp.status_code)
self.cleaned_data["auth_token"] = resp.json()["users"][0]["token"]
except Exception:
raise forms.ValidationError(
_("Unable to check WhatsApp enterprise account, please check username and password")
)
return self.cleaned_data
示例2: post
# 需要导入模块: from temba.contacts.models import URN [as 别名]
# 或者: from temba.contacts.models.URN import normalize_number [as 别名]
def post(self, request, *args, **kwargs):
from twilio.request_validator import RequestValidator
from temba.flows.models import FlowSession
signature = request.META.get("HTTP_X_TWILIO_SIGNATURE", "")
url = "https://" + request.get_host() + "%s" % request.get_full_path()
channel_uuid = kwargs.get("uuid")
call_sid = self.get_param("CallSid")
direction = self.get_param("Direction")
status = self.get_param("CallStatus")
to_number = self.get_param("To")
to_country = self.get_param("ToCountry")
from_number = self.get_param("From")
# Twilio sometimes sends un-normalized numbers
if to_number and not to_number.startswith("+") and to_country: # pragma: no cover
to_number, valid = URN.normalize_number(to_number, to_country)
# see if it's a twilio call being initiated
if to_number and call_sid and direction == "inbound" and status == "ringing":
# find a channel that knows how to answer twilio calls
channel = self.get_ringing_channel(uuid=channel_uuid)
if not channel:
response = VoiceResponse()
response.say("Sorry, there is no channel configured to take this call. Goodbye.")
response.hangup()
return HttpResponse(str(response))
org = channel.org
if self.get_channel_type() == "T" and not org.is_connected_to_twilio():
return HttpResponse("No Twilio account is connected", status=400)
client = self.get_client(channel=channel)
validator = RequestValidator(client.auth[1])
signature = request.META.get("HTTP_X_TWILIO_SIGNATURE", "")
url = "https://%s%s" % (request.get_host(), request.get_full_path())
if validator.validate(url, request.POST, signature):
from temba.ivr.models import IVRCall
# find a contact for the one initiating us
urn = URN.from_tel(from_number)
contact, urn_obj = Contact.get_or_create(channel.org, urn, channel)
flow = Trigger.find_flow_for_inbound_call(contact)
if flow:
call = IVRCall.create_incoming(channel, contact, urn_obj, channel.created_by, call_sid)
session = FlowSession.create(contact, connection=call)
call.update_status(
request.POST.get("CallStatus", None), request.POST.get("CallDuration", None), "T"
)
call.save()
FlowRun.create(flow, contact, session=session, connection=call)
response = Flow.handle_call(call)
return HttpResponse(str(response))
else:
# we don't have an inbound trigger to deal with this call.
response = channel.generate_ivr_response()
# say nothing and hangup, this is a little rude, but if we reject the call, then
# they'll get a non-working number error. We send 'busy' when our server is down
# so we don't want to use that here either.
response.say("")
response.hangup()
# if they have a missed call trigger, fire that off
Trigger.catch_triggers(contact, Trigger.TYPE_MISSED_CALL, channel)
# either way, we need to hangup now
return HttpResponse(str(response))
# check for call progress events, these include post-call hangup notifications
if request.POST.get("CallbackSource", None) == "call-progress-events":
if call_sid:
from temba.ivr.models import IVRCall
call = IVRCall.objects.filter(external_id=call_sid).first()
if call:
call.update_status(
request.POST.get("CallStatus", None), request.POST.get("CallDuration", None), "TW"
)
call.save()
return HttpResponse("Call status updated")
return HttpResponse("No call found")
return HttpResponse("Not Handled, unknown action", status=400) # pragma: no cover