This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def send_gif_message(recipient_id, message): | |
gif_url = search_gif(message) | |
data = json.dumps({ | |
"recipient": {"id": recipient_id}, | |
"message": { | |
"attachment": { | |
"type": "image", | |
"payload": { | |
"url": gif_url |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def search_gif(text): | |
#get a GIF that is similar to text sent | |
payload = {'s': text, 'api_key': '<GIPHY_API_KEY>'} | |
r = requests.get('http://api.giphy.com/v1/gifs/translate', params=payload) | |
r = r.json() | |
url = r['data']['images']['original']['url'] | |
return url |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def send_text_message(recipient_id, message): | |
data = json.dumps({ | |
"recipient": {"id": recipient_id}, | |
"message": {"text": message} | |
}) | |
params = { | |
"access_token": <PAGE_ACCESS_TOKEN> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route('/', methods=['POST']) | |
def handle_messages(): | |
data = request.get_json() | |
entry = data['entry'][0] | |
if entry.get("messaging"): | |
messaging_event = entry['messaging'][0] | |
sender_id = messaging_event['sender']['id'] | |
message_text = messaging_event['message']['text'] | |
send_text_message(sender_id, message_text) | |
return 'ok', 200 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
from flask import Flask, request | |
VERIFY_TOKEN = "giphy" | |
app = Flask(__name__) | |
@app.route('/', methods=['GET']) | |
def verify(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if user enters "hey" => | |
reply user "hi" | |
if user enters "hello" => | |
reply user "hi" | |
if user enters "hi" => | |
reply user "hi" | |
if user entered "where are you?" => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if user entered "hey" => | |
reply user with "hi" | |
if user entered "where are you?" => | |
reply user with "I am somewhere in the cloud" | |
if user entered "what is your name?" => | |
reply user with "I am Bot" | |
` |