-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
162 lines (148 loc) · 5.86 KB
/
server.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
155
156
157
158
159
160
161
162
CLIENT_ID = '3a7fad8d-0bb9-4451-87be-e82fa0855ce2'
CLIENT_SECRET = '9b05153f-d0ed-4ba0-a1d6-cd5d4bf9cf30'
import smartcar
from flask import Flask, request, jsonify, abort
import requests
import json
app = Flask(__name__)
client = smartcar.AuthClient(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri='https://542e5de9.ngrok.io/callback',
scope=['read_vehicle_info', 'read_location', 'control_security', 'control_security:lock', 'control_security:unlock'],
test_mode=False,
)
@app.route('/callback', methods=['GET'])
def callback():
code = request.args.get('code')
id = request.args.get('state')
access = client.exchange_code(code)
print(access)
response = smartcar.get_vehicle_ids(access['access_token'])
vehicle_id = response['vehicles'][0]
global vehicle
vehicle = smartcar.Vehicle(vehicle_id, access['access_token'])
# return jsonify(access)
response = {
"text": "You have successfully signed in! What would you like to do?",
"quick_replies":[{
"content_type":"text",
"title":"Lock",
"payload":"Lock",
},
{
"content_type":"text",
"title":"Unlock",
"payload":"Unlock",
},
{
"content_type":"text",
"title":"Find",
"payload":"Find",
}
]
}
callSendAPI(id, response)
return 'Please close this popup window'
@app.route('/webhook', methods=['POST'])
def webhook_handler():
body = request.json
if body['object'] == 'page':
for entry in body['entry']:
webhook_event = entry['messaging'][0]
print(webhook_event)
try:
user_message = webhook_event['message']['text']
except:
response = {
'text':'Sorry, I didn\'t understand that'
}
else:
print(user_message)
if user_message.lower() == 'sign in' or user_message.lower() == 'signin':
response = {
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"Press the button below!",
"buttons":[
{
"type":"web_url",
"url": client.get_auth_url(state=webhook_event['sender']['id']),
"title":"Sign in",
"webview_height_ratio": "full"
}
]
}
}
}
elif user_message.lower() == 'find':
try:
location = vehicle.location()
latitude = location['data']['latitude']
longitude = location['data']['longitude']
print(latitude, longitude)
response = {
'text':'Your car is here!\nhttps://maps.google.com/?q=%s,%s' % (latitude, longitude)
}
except:
response = {
'text': 'Uh oh! You need to "sign in" first'
}
elif user_message.lower() == 'lock':
try:
vehicle.lock()
response = {
'text': 'Your car is now locked!'
}
except:
response = {
'text': 'Uh oh! You need to "sign in" first'
}
elif user_message.lower() == 'unlock':
try:
vehicle.unlock()
response = {
'text': 'We unlocked your car!'
}
except:
response = {
'text': 'Uh oh! You need to "sign in" first'
}
elif user_message.lower() == 'sign out' or user_message.lower() == 'signout':
try:
vehicle.disconnect()
response = {
'text': 'You signed out!'
}
except:
response = {
'text': 'You\'re not signed in yet!'
}
else:
response = {
'text':'Forgot where you parked? Shoot our bot a message and it will respond with the location of your car!'
}
callSendAPI(webhook_event['sender']['id'], response)
return 'EVENT_RECEIVED'
else:
abort(404)
@app.route('/webhook', methods=['GET'])
def verify():
return request.args.get('hub.challenge')
def callSendAPI(sender_psid, response):
request_body = {
'recipient': {
'id': sender_psid
},
'message': response
}
params = {
"access_token":'EAAPBvndkCP4BADvlG56O44ODcIRgIWukSZAQmspjCWm760Osbs64nNokvwnkhUsMAhU9ZAyg2SutWXyIOaV3UwufsicB1D5NiuKzDgZB0udE20DejJUyMKwoZA3zrfd3Nes7HTTLPE3tfLBZAxwypCNZB1vIAapG0cKbv9rA4gTenfFlC5ZAIKq'
}
r = requests.post('https://graph.facebook.com/v2.6/me/messages', params=params, json=request_body)
print(r.status_code)
print(r.text)
if __name__ == '__main__':
app.run(port=1337)