forked from amazon-connect/amazon-connect-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
125 lines (99 loc) · 4.53 KB
/
lambda_function.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
'''
Basic helper function that covers common text/math operations for use with
contact flows. This function is assuming incoming events from a contact flow.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import json
import random
def lambda_handler(event, context):
response = {}
response.update({'result':'success'})
operation_requested = event['Details']['Parameters']['operation']
operation_parameters = event['Details']['Parameters']
if operation_requested == 'freeform_math':
response.update({'answer':str(freeform_math(operation_parameters))})
elif operation_requested == 'random_number':
response.update({'answer':str(random_number(operation_parameters))})
elif operation_requested == 'increment_1':
response.update({'answer':str(increment_1(operation_parameters))})
elif operation_requested == 'increment_n':
response.update({'answer':str(increment_n(operation_parameters))})
elif operation_requested == 'random_choice':
response.update({'answer':str(random_choice(operation_parameters))})
elif operation_requested == 'replace_text':
response.update({'answer':str(replace_text(operation_parameters))})
elif operation_requested == 'split_text':
response.update(split_text(operation_parameters))
elif operation_requested == 'strip_text':
response.update({'answer': str(strip_text(operation_parameters))})
elif operation_requested == 'upper_text':
response.update({'answer': str(upper_text(operation_parameters))})
elif operation_requested == 'lower_text':
response.update({'answer': str(lower_text(operation_parameters))})
else:
response.update({'result':'fail'})
response.update({'reason':'no valid function passed'})
return response
def freeform_math(operation_parameters):
result = eval(operation_parameters['expression'])
return result
def random_number(operation_parameters):
result = round(random.uniform(int(operation_parameters['start']),int(operation_parameters['end'])))
return result
def increment_1(operation_parameters):
result = int(operation_parameters['base'])+1
return result
def increment_n(operation_parameters):
result = int(operation_parameters['base'])+int(operation_parameters['increment'])
return result
def random_choice(operation_parameters):
choice_list = operation_parameters['list'].split(',')
result = random.choice(choice_list)
return result
def replace_text(operation_parameters):
do_replace = operation_parameters['text_string']
result = do_replace.replace(operation_parameters['replace_this'], operation_parameters['with_this'])
return result
def split_text(operation_parameters):
split_values = {}
split_counter = 0
text_to_split = operation_parameters['text_string']
split_at = operation_parameters['split_at']
try:
split_max = int(operation_parameters['split_max'])
except:
split_max = -1
split_result = text_to_split.split(split_at,split_max)
for string in split_result:
split_counter = split_counter + 1
split_values.update({
'segment'+str(split_counter):string
})
split_values.update({'total_segments':str(split_counter)})
result = split_values
return result
def strip_text(operation_parameters):
text_to_strip = operation_parameters['text_string']
what_to_strip = operation_parameters['strip_this']
strip_mode = operation_parameters['mode']
if strip_mode == 'trim':
result = text_to_strip.strip(what_to_strip)
if strip_mode == 'right':
result = text_to_strip.rstrip(what_to_strip)
if strip_mode == 'left':
result = text_to_strip.lstrip(what_to_strip)
return result
def upper_text(operation_parameters):
result = operation_parameters['text_string'].upper()
return result
def lower_text(operation_parameters):
result = operation_parameters['text_string'].lower()
return result