This repository was archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
181 lines (162 loc) · 7.04 KB
/
app.py
File metadata and controls
181 lines (162 loc) · 7.04 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
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
import sys
from flask import Flask, request
from libs.pymessenger_modified import Bot
from libs.pymessenger_modified import Element, Button
from libs.utils import PostbackSwitcher
import logging as log
from os import environ as env
from dotenv import load_dotenv
app = Flask(__name__)
load_dotenv()
secrets = ['PAGE_ACCESS_TOKEN','VERIFY_TOKEN','APP_SECRET']
if not all([secret in env for secret in secrets]):
log.error('Be sure to set all environment vars')
sys.exit(1)
PAGE_ACCESS_TOKEN = env.get('PAGE_ACCESS_TOKEN')
VERIFY_TOKEN = env.get('VERIFY_TOKEN')
APP_SECRET = env.get('APP_SECRET')
API_VERSION = env.get('API_VERSION')
Users = {}
postbackSwitcher = PostbackSwitcher()
bot = Bot(PAGE_ACCESS_TOKEN, api_version=API_VERSION, app_secret=APP_SECRET)
@app.route('/', 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()
# log.warn("OUTPUT: "+str(output))
log.warn("USERS: "+str(Users))
actions(output)
return "Message Processed"
# TODO: Complete logic for when user record is not in memory
def actions(output):
global Users
for event in output['entry']:
messaging = event['messaging']
# log.warn("MESSAGING: "+str(messaging))
for message in messaging:
#Facebook Messenger ID for user so we know where to send response back to
recipient_id = message['sender']['id']
if message.get('postback'):
error = postbackSwitcher.payload_string_to_response(payload_string=message['postback']['payload'],
recipient_id = recipient_id,
bot=bot,
Users=Users,
user=None
)
if error is not None: log.error("ERROR: "+str(error))
# elif message.get('message'):
# user = get_user(recipient_id)
# if user is None: pass
# if message['message'].get('text'):
# response = get_generic()
# try:
# res = bot.send_action(recipient_id, "mark_seen")
# # log.warn('RESPONSE: '+str(res))
# if res.get('error'):
# log.error('ERROR: '+str(res['error']))
# continue
# except Exception as e:
# log.error('ERROR: '+str(e))
# continue
# try:
# res = bot.send_action(recipient_id, "typing_on")
# # log.warn('RESPONSE: '+str(res))
# if res.get('error'):
# log.error('ERROR: '+str(res['error']))
# continue
# except Exception as e:
# log.error('ERROR: '+str(e))
# continue
# try:
# res = bot.send_generic_message(recipient_id, response)
# # log.warn('RESPONSE: '+str(res))
# if res.get('error'):
# log.error('ERROR: '+str(res['error']))
# continue
# except Exception as e:
# log.error('ERROR: '+str(e))
# continue
# #if user sends us a GIF, photo,video, or any other non-text item
# elif message['message'].get('attachments'):
# text, buttons = get_buttons()
# bot.send_action(recipient_id, "mark_seen")
# bot.send_action(recipient_id, "typing_on")
# bot.send_button_message(recipient_id, text, buttons)
# continue
return
def get_generic():
elements = []
default_action = Button(
type="web_url",
url="http://www.example.com/",
webview_height_ratio="compact"
)
# log.warn("Buttons 1: "+str(buttons))
elements.append(Element(
title="Welcome!",
image_url="https://images.pexels.com/photos/157675/fashion-men-s-individuality-black-and-white-157675.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
subtitle="We have the right stuff for everyone.",
default_action=default_action,
buttons=[Button(type="web_url",
url="http://www.example.com/",
title="View Website"
),
Button(type="postback",
title="Add to order",
payload="Got some payload!"
)
]
)
)
default_action = Button(
type="web_url",
url="http://www.example.com/",
webview_height_ratio="compact"
)
elements.append(Element(
title="Welcome!",
image_url="https://images.pexels.com/photos/291762/pexels-photo-291762.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500",
subtitle="We have the right stuff for you.",
default_action=default_action,
buttons=[Button(type="web_url",
url="http://www.example.com/",
title="Like us"
),
Button(type="postback",
title="Add to order",
payload="Got some payload!"
)
]
)
)
return elements
def get_buttons():
text = 'Select'
buttons = []
button = Button(title='Yes', type='postback', payload="Selected yes")
buttons.append(button)
button = Button(title='No', type='postback', payload='Selected no')
buttons.append(button)
return text, buttons
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 send_message(recipient_id, response):
#sends user the text message provided via input response parameter
bot.send_text_message(recipient_id, response)
return "success"
def send_generic(recipient_id, response):
bot.send_generic_message(recipient_id, response)
return 'success'
if __name__ == '__main__':
app.run(host="localhost",port=8080,load_dotenv=True)