Last active
October 24, 2017 15:41
-
-
Save keyuls/7ea646843bafc0202bb216dfba85fa64 to your computer and use it in GitHub Desktop.
Connecting Giphy with Chatfuel
This file contains 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
from flask import Flask,request | |
import os | |
from flask import make_response | |
import json | |
app = Flask(__name__) | |
@app.route('/getgif') | |
def send_gif(): | |
query = request.args.get('userinput') | |
result= connectGiphy(userinput) | |
result= makeImageResponse(result) | |
result = json.dumps(result, indent=4) | |
result= make_response(result) | |
return result | |
def connectGiphy(userinput): | |
baseurl= "http://api.giphy.com/v1/gifs/" | |
api_key= "dc6zaTOxFJmzC" | |
baseurl= baseurl+"search?q="+userinput+"&api_key="+api_key | |
response=makeConnection(baseurl) | |
output=retriveGiphyData(response) | |
return output | |
#Connecting wih Giphy | |
def makeConnection(baseurl): | |
s=requests.Session() | |
result =s.get(baseurl) | |
result=result.json() | |
return result | |
# selecting random image from list of images | |
def retriveGiphyData(response): | |
data = response["data"] | |
num =randint(0,len(data)-1) | |
data= data[num] | |
images = data["images"] | |
image= images["fixed_height"] | |
output = { | |
"url":image["url"] | |
} | |
return output | |
# creating json format that accepts by Chatfuel | |
def makeImageResponse(result): | |
message=[] | |
data={ | |
"attachment": { | |
"type": "image", | |
"payload": { | |
"url": result["url"] | |
} | |
} | |
} | |
message.append(data) | |
return message | |
if __name__ == '__main__': | |
port = int(os.getenv('PORT', 5000)) | |
app.run(debug=False, port=port, host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working