Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 46 additions & 29 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,77 @@
#Python libraries that we need to import for our bot
# Python libraries that we need to import for our bot
import os
import random

from flask import Flask, request
from pymessenger.bot import Bot
import os

app = Flask(__name__)
ACCESS_TOKEN = 'ACCESS_TOKEN' #ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
VERIFY_TOKEN = 'VERIFY_TOKEN' #VERIFY_TOKEN = os.environ['VERIFY_TOKEN']
bot = Bot (ACCESS_TOKEN)
ACCESS_TOKEN = os.environ.get('ACCESS_TOKEN', 'ACCESS_TOKEN')
VERIFY_TOKEN = os.environ.get('VERIFY_TOKEN', 'VERIFY_TOKEN')
bot = Bot(ACCESS_TOKEN)


#We will receive messages that Facebook sends our bot at this endpoint
# We will receive messages that Facebook sends our bot at this endpoint
@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."""
# 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
# 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:
if message.get('message'):
#Facebook Messenger ID for user so we know where to send response back to
recipient_id = message['sender']['id']
if message['message'].get('text'):
response_sent_text = get_message()
send_message(recipient_id, response_sent_text)
#if user sends us a GIF, photo,video, or any other non-text item
if message['message'].get('attachments'):
response_sent_nontext = get_message()
send_message(recipient_id, response_sent_nontext)
output = request.get_json()
for event in output['entry']:
messaging = event['messaging']
for message in messaging:
if message.get('message'):
# Facebook Messenger ID for user so we know where
# to send response back to
recipient_id = message['sender']['id']
if message['message'].get('text'):
response_sent_text = get_message()
send_message(recipient_id, response_sent_text)
# if user sends us a GIF, photo,video, or any other
# non-text item
if message['message'].get('attachments'):
response_sent_nontext = get_message()
send_message(recipient_id, response_sent_nontext)
return "Message Processed"


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
"""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'


#chooses a random message to send to the user
def get_message():
sample_responses = ["You are stunning!", "We're proud of you.", "Keep on being you!", "We're greatful to know you :)"]
"""Chooses a random message to send to the user"""
sample_responses = (
"You are stunning!",
"We're proud of you.",
"Keep on being you!",
"We're grateful to know you :)"
)
# return selected item to the user
return random.choice(sample_responses)

#uses PyMessenger to send response to user

def send_message(recipient_id, response):
#sends user the text message provided via input response parameter
"""Use PyMessenger to send user the text message provided via input
response parameter
"""
bot.send_text_message(recipient_id, response)
return "success"


if __name__ == "__main__":
app.run()