-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
156 lines (140 loc) · 4.29 KB
/
app.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from chalice import Chalice, Response
# from botocore.vendored import requests
import requests
import json
import os
app = Chalice(app_name='WallsDefaultAgentAllocation')
app.debug = True
QISCUS_BASE_URL = os.getenv("QISCUS_BASE_URL")
QISMO_BASE_URL = os.getenv("QISMO_BASE_URL")
QISMO_APP_ID = os.getenv("QISMO_APP_ID")
QISMO_AUTH_TOKEN = os.getenv("QISMO_AUTH_TOKEN")
QISMO_ADMIN_EMAIL = os.getenv("QISMO_ADMIN_EMAIL")
LINK_PAYMENT = "Link_Payment"
@app.route('/', methods=['POST'])
def index():
body = app.current_request.json_body
room = body['room_id']
agent = body['candidate_agent']['id']
allocation = allocate(room,agent)
print(f"allocation: {body}")
print("==============================")
email = body['email']
name = body['name']
payload = [
{
"key": "Nama",
"value": name
},
{
"key": "Alamat",
"value": "-"
},
{
"key": "No Hp",
"value": email
},
{
"key": "Order",
"value": "-"
},
{
"key": "Jumlah",
"value": "-"
},
{
"key": "Biaya",
"value": "-"
},
{
"key": LINK_PAYMENT,
"value": "https://invoice.bayardulu.com/"
},
{
"key": "No. Resi",
"value": "-"
}
]
info = additionalInformation(room, payload)
print(f"room: {room}")
print(f"agent: {agent}")
print(f"userInfo: {info.text}")
if info.status_code == 200:
print("success add info")
return allocation.json()
def allocate(room_id, agent_id):
url = f"{QISMO_BASE_URL}/api/v1/admin/service/assign_agent"
payload = {
'room_id':room_id,
'agent_id':int(agent_id)
}
headers = {
'Authorization': QISMO_AUTH_TOKEN,
}
response = requests.request("POST", url, data=payload, headers=headers)
return response
def additionalInformation(room_id, userInfo):
payload = {
"user_properties": userInfo
}
print(f"send additionalInformation {payload}")
url = f"{QISMO_BASE_URL}/api/v1/qiscus/room/{room_id}/user_info"
headers = {
'Authorization': QISMO_AUTH_TOKEN,
}
response = requests.post(url, headers=headers, json=payload)
return response
@app.route('/ticket', methods=['POST'])
def submitTicket():
body = app.current_request.json_body
add_info = body['additional_info']
room_id = body['room_id']
message = f"Berikut e-invoice anda \nOrder ID: {room_id}\n"
payment = ""
for i in add_info:
key = i['key']
value = i['value']
if LINK_PAYMENT in key:
payment = f"Silahkan melalukan pembayaran pada link berikut {value}"
else:
new = f"{key} : {value} \n"
message = message + new
message = f"{message} \n{payment}"
addTag(room_id)
sendMessage(room_id, message)
return Response(body={"message": "Job Accepted, please wait 1-2 to complete"},
status_code=200,
headers={'Content-Type': 'application/json'})
def sendMessage(room_id, message):
print(f"send {message} \nto {room_id}")
url = f"{QISMO_BASE_URL}/{QISMO_APP_ID}/bot"
payload = {
"sender_email": QISMO_ADMIN_EMAIL,
"message": message,
"type": "text",
"room_id": room_id
}
headers = {
'Authorization': QISMO_AUTH_TOKEN,
}
response = requests.request("POST", url, data=payload, headers=headers)
print(f"send message {response.text}")
return Response(body={response.text},
status_code=200,
headers={'Content-Type': 'application/json'})
def addTag(room_id):
print(f"add room tag {room_id}")
url = f"{QISMO_BASE_URL}/api/v1/room_tag/create"
payload = {
"tag": room_id,
"room_id": room_id
}
headers = {
'Authorization': QISMO_AUTH_TOKEN,
'Qiscus-App-Id': QISMO_APP_ID
}
response = requests.request("POST", url, data=payload, headers=headers)
print(f"add tag: {response.text}")
return Response(body={response.text},
status_code=200,
headers={'Content-Type': 'application/json'})