Skip to content

Instantly share code, notes, and snippets.

@keyuls
Last active October 24, 2017 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyuls/7ea646843bafc0202bb216dfba85fa64 to your computer and use it in GitHub Desktop.
Save keyuls/7ea646843bafc0202bb216dfba85fa64 to your computer and use it in GitHub Desktop.
Connecting Giphy with Chatfuel
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')
@waleedyounis87
Copy link

Not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment