-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlambda_code.py
89 lines (75 loc) · 2.52 KB
/
lambda_code.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
# Step 1
def chuck_norris_joke():
return "Chuck Norris does not do push ups. He moves the earth up and down."
def dad_joke():
return "What do call a man with no body and no nose? Nobody knows"
def random_fact():
return "We are 13.7 billion light years from edge of the observable universe"
def lambda_handler(event, context):
response = {
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText"
}
}
}
print(event)
if event["currentIntent"]["name"] == "FactsIntent":
response["dialogAction"]["message"]["content"] = random_fact()
else:
if event["currentIntent"]["slots"]["joketype"] == "chuck norris":
response["dialogAction"]["message"]["content"] = chuck_norris_joke()
else:
response["dialogAction"]["message"]["content"] = dad_joke()
return response
# Step 2
import json
import requests
def chuck_norris_joke():
try:
URL = 'https://api.chucknorris.io/jokes/random'
r = requests.get(url = URL)
data = r.json()
print(data)
return data['value']
except:
return "Chuck Norris does not do push ups. He moves the earth up and down."
def dad_joke():
try:
URL = 'https://icanhazdadjoke.com/'
r = requests.get(url = URL, headers = {'Accept':'application/json'})
data = r.json()
print(data)
return data['joke']
except:
return "What do call a man with no body and no nose? Nobody knows"
def random_fact():
try:
URL = 'https://uselessfacts.jsph.pl/random.json?language=en'
r = requests.get(url = URL)
data = r.json()
print(data)
return data['text']
except:
return "We are 13.7 billion light years from edge of the observable universe"
def lambda_handler(event, context):
response = {
"dialogAction": {
"type": "Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText"
}
}
}
print(event)
if event["currentIntent"]["name"] == "FactsIntent":
response["dialogAction"]["message"]["content"] = random_fact()
else:
if event["currentIntent"]["slots"]["joketype"] == "chuck norris":
response["dialogAction"]["message"]["content"] = chuck_norris_joke()
else:
response["dialogAction"]["message"]["content"] = dad_joke()
return response