Skip to content

Commit 8d24760

Browse files
yil532nicainparthea
authored
docs(samples): Add prebuilt telecom agent webhook code in python (#434)
* feat: add prebuilt telecom webhook python sample code and test * feat:add test * fix:lint errors * fix:add new line at the end of file * fix: add more comprehensive tests * fix: pylint * feat:change helper function and address comments * fix: pylint Co-authored-by: nicain <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 12b1fdc commit 8d24760

File tree

2 files changed

+494
-0
lines changed

2 files changed

+494
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
# Copyright 2022, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
""" handle_webhook will return the correct fullfilment response depending on the tag that is sent in the request"""
15+
# [START dialogflow_cx_v3_webhook_prebuilt_telecom]
16+
import copy
17+
import logging
18+
19+
20+
def cxPrebuiltAgentsTelecom(request):
21+
logging.info('Cloud Function:' + 'Invoked cloud function from Dialogflow')
22+
request_dict = request.get_json()
23+
24+
# Get the parameters in current page
25+
parameter_info_list = request_dict["pageInfo"]["formInfo"]["parameterInfo"]
26+
parameter_dict = {}
27+
for parameter_info in parameter_info_list:
28+
key = parameter_info["displayName"]
29+
parameter_dict[key] = parameter_info["value"]
30+
31+
# Get the tag
32+
tag = request_dict["fulfillmentInfo"]["tag"]
33+
34+
# BEGIN detectCustomerAnomaly
35+
if tag == 'detectCustomerAnomaly':
36+
logging.info(tag + ' was triggered.')
37+
phone_number = parameter_dict["phone_number"]
38+
bill_state = parameter_dict["bill_state"]
39+
parameters = copy.deepcopy(parameter_dict)
40+
bill_amount = None
41+
product_line = None
42+
anomaly_detect = "false"
43+
purchase = "The Godfather"
44+
purchase_amount = 9.99
45+
total_bill_amount = 64.33
46+
bill_without_purchase = 54.34
47+
updated_parameters = {}
48+
49+
month_name, first_of_month, last_month_name = get_date_details(bill_state)
50+
logging.info(month_name, first_of_month, last_month_name)
51+
52+
# Getting the month name based on the bill state - current or previous
53+
# For example, if the current month is December, we get the values as
54+
# December, December 1st, November
55+
56+
# Only 999999 will have anomaly detection
57+
if str(phone_number) == '999999':
58+
anomaly_detect = "true"
59+
product_line = "phone"
60+
purchase = "device protection"
61+
updated_parameters["product_line"] = product_line
62+
updated_parameters["bill_month"] = month_name
63+
updated_parameters["last_month"] = last_month_name
64+
65+
# If bill hike amount is given - we just add it to the total bill
66+
if 'bill_amount' in parameters:
67+
bill_amount = parameters['bill_amount']
68+
purchase_amount = bill_amount['amount']
69+
total_bill_amount = 54.34 + purchase_amount
70+
71+
# Adding the updated session parameters to the new parameters json
72+
updated_parameters["anomaly_detect"] = anomaly_detect
73+
updated_parameters["purchase"] = purchase
74+
updated_parameters["purchase_amount"] = purchase_amount
75+
updated_parameters["bill_without_purchase"] = bill_without_purchase
76+
updated_parameters["total_bill"] = total_bill_amount
77+
updated_parameters["first_month"] = first_of_month
78+
79+
res = {
80+
"sessionInfo": {
81+
"parameters": updated_parameters
82+
}
83+
}
84+
85+
# BEGIN validatePhoneLine
86+
elif tag == 'validatePhoneLine':
87+
logging.info(tag + ' was triggered.')
88+
phone = parameter_dict["phone_number"]
89+
phone_line_verified = 'false'
90+
line_index = None
91+
domestic_coverage = 'false'
92+
covered_lines = ['5555555555', '5105105100', '1231231234', '9999999999']
93+
94+
# Loop over the covered lines array
95+
for index, line in enumerate(covered_lines):
96+
# For each phone line in the array, check if the last 4 digits are
97+
# included in the string. when true, update the line_index variable
98+
if phone == line:
99+
line_index = index
100+
logging.info('This is the index ' + str(line_index))
101+
102+
# Only 9999999999 will fail
103+
if line_index == 3:
104+
phone_line_verified = 'false'
105+
else:
106+
phone_line_verified = 'true'
107+
108+
# Only 1231231234 will have domestic coverage
109+
if line_index == 2:
110+
domestic_coverage = 'true'
111+
else:
112+
domestic_coverage = 'false'
113+
114+
res = {
115+
"sessionInfo": {
116+
"parameters": {
117+
"phone_line_verified": phone_line_verified,
118+
"domestic_coverage": domestic_coverage
119+
}
120+
}
121+
}
122+
123+
# BEGIN cruisePlanCoverage
124+
elif tag == 'cruisePlanCoverage':
125+
logging.info(tag + ' was triggered.')
126+
port = parameter_dict["destination"]
127+
port_is_covered = None
128+
# Sample list of covered cruise ports.
129+
covered_ports = [
130+
'mexico',
131+
'canada',
132+
'anguilla',
133+
]
134+
135+
if port.lower() in covered_ports:
136+
port_is_covered = 'true'
137+
else:
138+
port_is_covered = 'false'
139+
140+
res = {
141+
"sessionInfo": {
142+
"parameters": {
143+
"port_is_covered": port_is_covered,
144+
}
145+
}
146+
}
147+
148+
# BEGIN internationalCoverage
149+
elif tag == 'internationalCoverage':
150+
logging.info(tag + ' was triggered.')
151+
destination = parameter_dict["destination"]
152+
coverage = None
153+
# Sample list of covered international monthly destinations.
154+
covered_by_monthly = [
155+
'anguilla',
156+
'australia',
157+
'brazil',
158+
'canada',
159+
'chile',
160+
'england',
161+
'france',
162+
'india',
163+
'japan',
164+
'mexico',
165+
'russia',
166+
'singapore',
167+
]
168+
# Sample list of covered international daily destinations.
169+
covered_by_daily = [
170+
'anguilla', 'australia', 'brazil', 'canada', 'chile', 'england',
171+
'france', 'india', 'japan', 'mexico', 'singapore'
172+
]
173+
if destination.lower() in covered_by_monthly and destination.lower() in covered_by_daily:
174+
coverage = 'both'
175+
elif destination.lower() in covered_by_monthly and destination.lower() not in covered_by_daily:
176+
coverage = 'monthly_only'
177+
elif destination.lower() not in covered_by_monthly and destination.lower() not in covered_by_daily:
178+
coverage = 'neither'
179+
else:
180+
# This should never happen, because covered_by_daily is a subset of
181+
# covered_by_monthly
182+
coverage = 'daily_only'
183+
184+
res = {
185+
"sessionInfo": {
186+
"parameters": {
187+
"coverage": coverage,
188+
}
189+
}
190+
}
191+
192+
# BEGIN cheapestPlan
193+
elif tag == 'cheapestPlan':
194+
logging.info(tag + ' was triggered.')
195+
trip_duration = parameter_dict["trip_duration"]
196+
monthly_cost = None
197+
daily_cost = None
198+
suggested_plan = None
199+
200+
# Can only suggest cheapest if both are valid for location.
201+
202+
# When trip is longer than 30 days, calculate per-month cost (example $
203+
# amounts). Suggest monthly plan.
204+
if trip_duration > 30:
205+
monthly_cost = (int(trip_duration / 30)) * 70
206+
daily_cost = trip_duration * 10
207+
suggested_plan = 'monthly'
208+
209+
# When trip is <= 30 days, but greater than 6 days, calculate monthly
210+
# plan cost and daily plan cost. Suggest monthly b/c it is the cheaper
211+
# one.
212+
elif trip_duration <= 30 and trip_duration > 6:
213+
monthly_cost = 70
214+
daily_cost = trip_duration * 10
215+
suggested_plan = 'monthly'
216+
217+
# When trip is <= 6 days, calculate daily plan cost. Suggest daily
218+
# plan.
219+
elif trip_duration <= 6 and trip_duration > 0:
220+
monthly_cost = 70
221+
daily_cost = trip_duration * 10
222+
suggested_plan = 'daily'
223+
224+
else:
225+
# This should never happen b/c trip_duration would have to be
226+
# negative
227+
suggested_plan = 'null'
228+
229+
res = {
230+
"sessionInfo": {
231+
"parameters": {
232+
"monthly_cost": monthly_cost,
233+
"daily_cost": daily_cost,
234+
"suggested_plan": suggested_plan,
235+
}
236+
}
237+
}
238+
239+
# Default Case
240+
else:
241+
res = None
242+
logging.info(f'{"default case called"}')
243+
244+
# Returns json
245+
return res
246+
247+
248+
# Get the current month, first day of current month and last month values
249+
# based on today's date
250+
def get_date_details(bill_state):
251+
from datetime import date
252+
253+
monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
254+
today = date.today()
255+
# index starts with 0
256+
first_month_name = monthNames[today.month - 1]
257+
firstDay = today.replace(day=1)
258+
first_day_str = str(firstDay)
259+
260+
last_month_name = monthNames[today.month - 2]
261+
last_month_first_day_str = str(today.replace(day=1, month=(today.month - 1)))
262+
second_last_month_name = monthNames[today.month - 3]
263+
if bill_state == 'current':
264+
return [first_month_name, first_day_str, last_month_name]
265+
else:
266+
return [last_month_name, last_month_first_day_str, second_last_month_name]
267+
# [END dialogflow_cx_v3_webhook_prebuilt_telecom]

0 commit comments

Comments
 (0)