-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLF2.py
More file actions
108 lines (94 loc) · 3.6 KB
/
Copy pathLF2.py
File metadata and controls
108 lines (94 loc) · 3.6 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
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
import json
import boto3
# from elasticsearch import Elasticsearch, RequestsHttpConnection
from opensearchpy import OpenSearch, RequestsHttpConnection
from requests_aws4auth import AWS4Auth
TABLE_NAME = 'yelp-restaurants'
SAMPLE_N = '5'
SEARCH_URL = 'https://search-restaurants-i6vpb7aoab5uqzmzg57xczr6fy.us-west-2.es.amazonaws.com'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, 'us-west-2', 'es',session_token=credentials.token)
sqs = boto3.resource('sqs',region_name='us-west-2')
es = OpenSearch(SEARCH_URL, http_auth=awsauth, connection_class=RequestsHttpConnection)
def sendsms(number, message):
send_sms = boto3.client('sns',region_name='us-west-2')
smsattrs = {
'AWS.SNS.SMS.SenderID': {
'DataType': 'String',
'StringValue': 'TestSender'
},
'AWS.SNS.SMS.SMSType': {
'DataType': 'String',
'StringValue': 'Transactional' # change to Transactional from Promotional for dev
}
}
response = send_sms.publish(
PhoneNumber=number,
Message=message,
MessageAttributes=smsattrs
)
print(number)
print(response)
print("The message is: ", message)
def search(cuisine):
data = es.search(index="restaurants", body={"query": {"match": {'cuisine':cuisine}}})
print("search complete", data['hits']['hits'])
return data['hits']['hits']
def get_restaurant_data(ids):
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
table = dynamodb.Table('yelp-restaurants')
ans = 'Hi! Here are your suggestions,\n '
i = 1
for id in ids:
if i<6:
response = table.get_item(
Key={
'restaurantID': id
}
)
response_item = response.get("Item")
restaurant_name = response_item['name']
restaurant_address = response_item['address']
restaurant_city = response_item['city:']
restaurant_zipcode = response_item['zip_code']
restaurant_rating = str(response_item['rating'])
ans += "{}. {}, located at {}\n".format(i, restaurant_name, restaurant_address)
# return ans
i += 1
else:
break
print("db pass")
return ans # string type
def lambda_handler(event=None, context=None):
print('Hello')
queue = sqs.get_queue_by_name(QueueName='restaurant_chatbot')
print(queue)
print(event)
# messages = queue.receive_messages(AttributeNames=['All'])
# messages = queue.receive_messages(MessageAttributeNames=['All'])
# print(messages)
messages = event['Records'][0]['body']
print('Hello1')
try:
message = messages
message1 = json.loads(message)
location = message1.get('location').get('StringValue')
cuisine = message1.get('cuisine').get('StringValue')
dining_date = message1.get('date').get('StringValue')
dining_time = message1.get('diningTime').get('StringValue')
num_people = message1.get('numberOfPeople').get('StringValue')
phone = message1.get('phoneNumber').get('StringValue')
print(location, cuisine, dining_date, dining_time, num_people, phone)
ids = search(cuisine)
ids = list(map(lambda x: x['_id'], ids))
rest_details = get_restaurant_data(ids)
# phone = '6316205900'
sendsms("+1"+phone, rest_details)
message.delete()
except Exception as e:
print(e)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda LF2!')
}
lambda_handler()