-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
260 lines (219 loc) · 12 KB
/
app.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
import requests
import foo
import boto3
import mimetypes
from flask import Flask, request
from pymessenger.bot import Bot
from flask_pymongo import PyMongo
from pydub import AudioSegment
app = Flask(__name__)
ACCESS_TOKEN = foo.ACCESS_TOKEN
VERIFY_TOKEN = foo.VERIFY_TOKEN
bot = Bot(ACCESS_TOKEN)
# Database config
app.config["MONGO_URI"] = foo.MONGO_URI
mongo = PyMongo(app)
db_operations = mongo.db.user
db_collections = {}
db_collections['IamLoved'] = mongo.db.IamLoved
db_collections['IamOk'] = mongo.db.IamOk
db_collections['IamSafe'] = mongo.db.IamSafe
db_collections['LaborAndDelivery'] = mongo.db.LaborAndDelivery
db_collections['LetsDoThis'] = mongo.db.LetsDoThis
def verify_fb_token(token_sent):
# take token sent by facebook and verify it matches the verify token you sent
# if they match, allow the request, else return an error
if token_sent == VERIFY_TOKEN:
return request.args.get("hub.challenge")
return 'Invalid verification token'
def update_user(user):
for collection_name in db_collections:
verses = db_collections[collection_name].find()
for verse in verses:
_update = {
"$set": {
f"{collection_name}.{verse['_id']}.audio_url": "Foobar",
f"{collection_name}.{verse['_id']}.ReferenceLf": verse['ReferenceLf'],
}
}
db_operations.update_one(user, _update)
def listen_to_playlist(user):
collection_name = user['ref']
recordings = user[collection_name]
audio_url = ""
files = []
for ref in recordings:
audio_url = recordings[ref]['audio_url']
if audio_url != "Foobar":
r = requests.get(audio_url, allow_redirects=True)
file = audio_url.split("/")[-1].split("?")[0]
with open(f"./{file}",'wb') as f:
f.write(r.content)
files.append(AudioSegment.from_file(f"./{file}", "mp4"))
playlist = files[0]
for i in range(1, len(files)):
playlist = playlist + files[i]
playlist.export(f"{collection_name}.mp3", "mp3")
file_mime_type, _ = mimetypes.guess_type(f"{collection_name}.mp3")
# Uploading to S3 bucket
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket("verse-recordings")
bucket.upload_file(Filename=f"{collection_name}.mp3", Key=f"{user['_id']}/{collection_name}.mp3", ExtraArgs={'ContentType': file_mime_type})
# Public URL of the recording
audio_url = f"https://verse-recordings.s3.ap-south-1.amazonaws.com/{user['_id']}/{collection_name}.mp3"
# Sending the stitched version of all the recordings
bot.send_audio_url(recipient_id=user['_id'], audio_url=audio_url)
@app.route("/list")
def foo():
entries = list(db_operations.find())
verses = [entry['verse'] for entry in entries]
print(verses)
return "success"
# We will receive messages that Facebook sends to the bot at this endpoint
@app.route("/webhooks/facebook/webhook", methods=['GET', 'POST'])
def receive_message():
if request.method == 'GET':
"""Before allowing people to message your bot, Facebook has implemented a verify token
that confirms all requests that your bot receives came from Facebook."""
token_sent = request.args.get("hub.verify_token")
return verify_fb_token(token_sent)
# if the request was not get, it must be POST and we can just proceed with sending a message back to user
else:
# get whatever message a user sent the bot
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for message in messaging:
# Upserting the user
recipient_id = message['sender']['id']
user = db_operations.find_one({'_id': int(recipient_id)})
if user is None:
new_user = {'_id': int(recipient_id), 'prevBotMsg': "Foo"}
db_operations.insert_one(new_user)
user = db_operations.find_one({'_id': int(recipient_id)})
update_user(user)
try:
prevBotMsg = user['prevBotMsg']
except:
prevBotMsg = "Foo"
if message.get('postback'):
postback = message['postback']
if postback.get("referral"):
# Are there any Messenger ref values in the query parameter
if postback['referral'].get('ref'):
ref = postback['referral']['ref']
print(ref)
updateUser = {"$set": {"ref": ref}}
db_operations.update_one(user, updateUser)
# Getting the sender's name
r = requests.get('https://graph.facebook.com/{}?fields=first_name,last_name,profile_pic&access_token={}'.format(recipient_id, ACCESS_TOKEN)).json()
try:
first_name = r['first_name']
last_name = r['last_name']
msg = f"Welcome {first_name} {last_name}!"
updateUser = {"$set": {"name": f"{first_name} {last_name}"}}
db_operations.update_one(user, updateUser)
except KeyError:
msg = "Welcome!"
# Greeting the user
bot.send_text_message(recipient_id=recipient_id, message=msg)
# Sending an image corresponding to the ref value
imageURLs = {
"IamLoved": "https://i.ibb.co/5Ljc7vX/Whats-App-Image-2021-07-19-at-15-49-57.jpg",
"IamOk": "https://i.ibb.co/N2mjWC0/Whats-App-Image-2021-07-31-at-11-19-36-AM.jpg",
"LetsDoThis": "https://i.ibb.co/5KwPGs4/Whats-App-Image-2021-07-31-at-11-18-10-AM.jpg"
}
bot.send_image_url(recipient_id=recipient_id, image_url=imageURLs[ref])
bot.send_text_message(recipient_id=recipient_id, message="Which verse did you read today?")
updateUser = {"$set": {"prevBotMsg": "Which verse did you read today?"}}
db_operations.update_one(user, updateUser)
else:
postbackTitle = message['postback']['title']
if postbackTitle == "Record":
updateUser = {"$set": {"prevBotMsg": "Nice! Send your recording of this verse"}}
db_operations.update_one(user, updateUser)
bot.send_text_message(recipient_id=recipient_id, message="Nice! Send your recording of this verse")
elif postbackTitle == "Listen to Playlist":
listen_to_playlist(user)
elif message.get('message'):
userMessage = message['message']
# User enters a verse
if "Which verse did you read today" in prevBotMsg:
for collection_name in db_collections:
enteredVerse = userMessage['text']
verse_doc = db_collections[collection_name].find_one({
"$or":
[
{
'ReferenceSf':{ '$regex':f'^{enteredVerse}$', "$options":"i"}
},
{
'ReferenceLf': { '$regex':f'^{enteredVerse}$', "$options":"i"}
}
]
})
if verse_doc is not None:
verse_doc['ref'] = collection_name
break
print(verse_doc)
if verse_doc is not None:
verse = f"\"{verse_doc['verse']}\"\n{verse_doc['ReferenceLf']}, {verse_doc['version']}"
bot.send_text_message(recipient_id=recipient_id, message=verse)
buttons = [
{
"type":"postback",
"title":"Record",
"payload": "Record"
},
{
"type":"postback",
"title": "Hear an example",
"payload": "Example"
}
]
updateUser = {"$set": {"verse_id": verse_doc["_id"], "ref": verse_doc['ref']}}
db_operations.update_one(user, updateUser)
r = bot.send_button_message(recipient_id=recipient_id, text="Record this verse and your declaration", buttons=buttons)
print(r)
elif "Nice!" in prevBotMsg:
if userMessage.get("attachments"):
audios = userMessage['attachments']
for audio in audios:
if audio.get("payload"):
url = audio["payload"]["url"]
updateUser = {
"$set": {
f"{user['ref']}.{user['verse_id']}.audio_url": url
}
}
db_operations.update_one(user, updateUser)
modified_collection_name = {
"IamLoved": "I am Loved",
"IamOk": "I am Ok",
"IamSafe": "I am Safe",
"LaborAndDelivery": "Labor And Delivery",
"LetsDoThis": "Let's Do This"
}
msg = f"Added to {modified_collection_name[user['ref']]} playlist"
bot.send_text_message(recipient_id=recipient_id, message=msg)
buttons = [
{
"type":"postback",
"title":"Listen to Playlist",
"payload": "Listen to Playlist"
},
{
"type":"postback",
"title": "Create Schedule",
"payload": "Create Schedule"
},
{
"type":"postback",
"title": "Read All Verses",
"payload": "Read All Verses"
}
]
bot.send_button_message(recipient_id=recipient_id, text="Select an option", buttons=buttons)
return "Message Processed"
if __name__ == "__main__":
app.run(port=5005)