-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtelesign.py
More file actions
59 lines (52 loc) · 2.35 KB
/
Copy pathtelesign.py
File metadata and controls
59 lines (52 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import base64
import urllib.parse
from ..models import ParsedResponse
class TelesignProvider:
manifest = {
"id": "telesign",
"auth": {
"mode": "apiKey",
"key_vault_secret_name": "telesign-api-key",
"identity_key_vault_secret_name": "telesign-customer-id",
},
"response_mapping": {
"200": "Continue", "203": "Continue", "290": "Continue", "291": "Continue", "292": "Continue",
"100": "Continue", "101": "Continue", "102": "Continue", "103": "Continue",
"default": "Fail",
},
}
def build_request(self, channel, endpoint, dispatch, credential, env):
raw = f"{credential['identity']}:{credential['secret']}".encode()
authorization = "Basic " + base64.b64encode(raw).decode()
external_id = dispatch.correlation_id or dispatch.message_id
if channel == "voice":
path = "/v1/voice"
form = {
"phone_number": dispatch.destination,
"message": dispatch.message or "",
"message_type": "OTP",
"voice": env.get("TELESIGN_VOICE") or "f-en-US",
"external_id": external_id,
}
else:
path = "/v1/messaging"
form = {
"phone_number": dispatch.destination,
"message": dispatch.message or "",
"sender_id": env.get("EPP_PROVIDER_ACCOUNT_NAME") or "",
"message_type": "OTP",
"external_id": external_id,
"is_primary": "true",
}
headers = {"Authorization": authorization, "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"}
return {"url": f"{endpoint}{path}", "method": "POST", "headers": headers, "body": urllib.parse.urlencode(form)}
def parse_response(self, http_status, ok, json_body):
status = json_body.get("status") or {} if isinstance(json_body, dict) else {}
code = status.get("code")
return ParsedResponse(
success=ok,
provider_http_status=http_status,
provider_message_id=json_body.get("reference_id") if isinstance(json_body, dict) else None,
provider_status_code=str(code) if code is not None else None,
provider_status_description=status.get("description"),
)