-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
377 lines (304 loc) · 12.4 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import json
import requests
from flask import Flask,render_template,url_for,request,redirect,flash
from datetime import datetime
from Forms import BackyardForm
from flask_sqlalchemy import SQLAlchemy
import os
from sqlalchemy import desc
import urllib3.contrib.pyopenssl
import certifi
import urllib3
urllib3.contrib.pyopenssl.inject_into_urllib3()
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where())
baseddir=os.path.abspath(os.path.dirname(__file__))
UPLOAD_FOLDER = os.path.abspath('static/img')
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
app=Flask(__name__)
app.config['SECRET_KEY']='\xa2}{a9\xf8\x08-\x92 \xcb\x81E\xc7/\xd9\x9a\x9b\xa2^\xb4 !A'
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///'+os.path.join(baseddir,'backyard.db')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
db=SQLAlchemy(app)
#models class
class Backyard(db.Model):
id=db.Column(db.Integer,primary_key=True)
photo_url=db.Column(db.String(300),nullable=False)
date=db.Column(db.DateTime,default=datetime.utcnow,nullable=False)
description=db.Column(db.String(300),nullable=False)
location=db.Column(db.String(300),nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'),nullable=False)
@staticmethod
def newest(num):
return Backyard.query.order_by(desc(Backyard.date)).limit(num)
def __repr__(self):
return "<Bookmark '{}':'{}'>".format(self.description,self.photo_url)
class User(db.Model):
id=db.Column(db.Integer,primary_key=True)
user=db.Column(db.String(80),unique=True,nullable=False)
email=db.Column(db.String(80))
backyards = db.relationship('Backyard', backref='user', lazy='dynamic')
def __repr__(self):
return "<Bookmark %r"%self.user
#backyard instance to store information from facebook
backyard = Backyard()
def loged_in_user(user_id):
return User.query.filter_by(user=user_id).first()
# This needs to be filled with the Page Access Token that will be provided
# by the Facebook App that will be created.
GATE = 'EAAEVjnwxwgYBAKGvcxJ3DBjIj2ZCa1laA2MIBzJfKZCiFePd2hqpP8cX9w0ZBcZB9grlWlz4xfQc4LReDUmTIDEHZCSndmvP9adyZC0kLgB26uKREEu3wZBuBwLZB2XXgEYfZAuZC8D4QFzFtPO0aonZC0u1UocEZCYoF4o6ZBS9pzDjCGwZDZD'
def button_template(sender):
return {
"recipient":{
"id":sender
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":"Hy am Bob, Ill be guiding you in simplifying your backyard sales.Select one of the options below ",
"buttons":[
{
"type":"postback",
"title":"Sell an Item",
"payload":"SALE_PAYLOAD",
},
{
"type":"postback",
"title":"View/Buy an Item",
"payload":"BUY_PAYLOAD"
}
]
}
}
}
}
def buy_list_template(sender):
try:
by = Backyard.newest(5)
print "hellllllo"
print by
photo = []
description = []
user = []
for data in by:
photo.append(data.photo_url)
description.append(data.description)
user.append(data.user.user)
print photo
print description
print user
if len(photo)>=2:
return {
"recipient": {
"id": sender
}, "message": {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [
{
"title": str(description[0]),
"image_url": str(photo[0]),
"subtitle": "Message the owner",
"default_action": {
"type": "web_url",
"url": str(photo[0]),
},
"buttons": [
{
"title": "Message the owner",
"type": "web_url",
"url": ' https://backyardsales.herokuapp.com/add?user={} &sender={}'.format(str(user[0]),sender),
}
]
},
{
"title": str(description[1]),
"image_url":str(photo[1]) ,
"subtitle": "message the owner",
"default_action": {
"type": "web_url",
"url": str(photo[1]),
},
"buttons": [
{
"title": "Message the owner",
"type": "web_url",
"url": ' https://backyardsales.herokuapp.com/add?user={} &sender={}'.format(str(user[1]),sender),
}
]
}
]
}
}
}
}
except IndexError:
print "no items on sale currently. Try again later"
send_message(GATE, sender,"no items on sale currently. please try again later")
def location_quick_reply(sender):
return {
"recipient": {
"id": sender
},
"message": {
"text": "Share the location you want to sell this item:",
"quick_replies": [
{
"content_type": "location",
}
]
}
}
@app.route("/login",methods=["POST","GET"])
def login():
return render_template('login.html')
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route("/add",methods=["POST","GET"])
def add():
form=BackyardForm()
recipient=request.args.get('user')
sender = request.args.get('sender')
print sender
if form.validate_on_submit():
comment = form.comment.data
clientToOwner(GATE,recipient,comment,sender)
print 'save comment to db'
flash("Your message has been sent")
return render_template('add.html',form=form)
@app.route('/', methods=['GET'])
def handle_verification():
print "Handling Verification."
if request.args.get('hub.verify_token', '') == 'my_secret_key':
print "Verification successful!"
return request.args.get('hub.challenge', '')
else:
return "Verification failed!"
@app.route('/', methods=['POST'])
def handle_messages():
print "Handling Messages"
payload = request.get_data()
print payload
for sender, message in messaging_events(payload):
print "Incoming from %s: %s" % (sender, message)
send_message(GATE, sender, message)
return "ok"
def messaging_events(payload):
"""Generate tuples of (sender_id, message_text) from the
provided payload.
"""
data = json.loads(payload)
messaging_events = data["entry"][0]["messaging"]
for event in messaging_events:
sender = event['sender']['id']
if "message" in event and "text" in event["message"]:
if str(event['message']['text']).startswith('Description'):
print 'Capture users Description of item here'
description=event['message']['text']
backyard.description=description
print backyard.photo_url
print backyard.description
print description
payload = location_quick_reply(sender)
r = requests.post('https://graph.facebook.com/v2.6/me/messages/?access_token=' + GATE, json=payload)
else:
payload = button_template(sender)
print payload
r = requests.post('https://graph.facebook.com/v2.6/me/messages/?access_token=' + GATE, json=payload)
print r
elif 'postback' in event and 'payload' in event['postback']:
sender = event['sender']['id']
if event['postback']["payload"]=='User_defined':
print 'hy'
payload = button_template(sender) # {'recipient': {'id': sender}, 'message': {'text': "Hello World"}}
print payload
r = requests.post('https://graph.facebook.com/v2.6/me/messages/?access_token=' + GATE, json=payload)
print r
elif event['postback']["payload"]=='BUY_PAYLOAD':
print "captured"
payload = buy_list_template(sender) # {'recipient': {'id': sender}, 'message': {'text': "Hello World"}}
print payload
r = requests.post('https://graph.facebook.com/v2.6/me/messages/?access_token=' + GATE, json=payload)
print r
elif event['postback']["payload"]=='SALE_PAYLOAD':
print "captured"
message="Step 1: Send us a photo of item you want to sell"
yield event["sender"]["id"], message.encode('unicode_escape')
elif 'message' in event and 'payload' in event['message']['attachments'][0]:
print 'well done'
if 'title' in event['message']['attachments'][0] and 'elements' not in event['message']['attachments'][0]['payload']:
location = event['message']['attachments'][0]['title']
backyard.location=location
print backyard.photo_url
print backyard.description
print location
#add user id to db
user_id=event["sender"]["id"]
if not loged_in_user(user_id):
user = User(user=user_id, email='[email protected]')
db.session.add(user)
db.session.commit()
by = Backyard(user=loged_in_user(user_id),photo_url=backyard.photo_url, date=datetime.utcnow(), description=str(backyard.description),location=str(backyard.location))
print by.photo_url
print by.description
print by.location
db.session.add(by)
db.session.commit()
print "Thank you...Uploaded to db"
message = "Your Item has been Created. Seat back and wait to be messaged by one of facebooks 1 billion clients. Thanks :)"
yield event["sender"]["id"], message.encode('unicode_escape')
if 'url' in event['message']['attachments'][0]['payload']:
print 'yay image capt'
image_path = event['message']['attachments'][0]['payload']['url']
backyard.photo_url=image_path
print backyard.photo_url
print image_path
item_description = "Step 2: Describe this item,start this with the word 'Description'."
send_message(GATE, event['sender']['id'], item_description.encode('unicode_escape'))
def send_message(token, recipient, text):
"""Send the message text to recipient with id recipient.
"""
r = requests.post("https://graph.facebook.com/v2.6/me/messages",
params={"access_token": token},
data=json.dumps({
"recipient": {"id": recipient},
"message": {"text": text.encode('unicode_escape')}
}),
headers={'Content-type': 'application/json'})
if r.status_code != requests.codes.ok:
print r.text
@app.errorhandler(404)
def errorHandler(e):
return render_template('404.html'),404
#function to send message to user automatically
def clientToOwner(token, recipient,text,sender):
payload={
"recipient": {
"id": recipient
},
"message": {
"attachment": {
"type": "template",
"payload": {
"template_type": "button",
"text": text,
"buttons": [
{
"title": "Reply",
"type": "web_url",
"url": ' https://backyardsales.herokuapp.com/add?user={} &sender={}'.format(sender,recipient),
}
]
}
}
}
}
r = requests.post('https://graph.facebook.com/v2.6/me/messages/?access_token=' + GATE, json=payload)
if __name__ == '__main__':
app.run(debug=True)