Last active
September 21, 2017 21:33
-
-
Save keyuls/d944455507a1c6d09789abf9633908bc to your computer and use it in GitHub Desktop.
connecting to api.ai
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
from flask import Flask, request | |
from flask import make_response | |
import requests | |
import os | |
import json | |
app = Flask(__name__) | |
@app.route('/connect') | |
def cf_connect(): | |
query = request.args.get('query') | |
api_url = 'https://api.api.ai/v1/query?v=20150910&query=' | |
head = {'Authorization': 'Bearer YOUR_ACCESS_TOKEN'} | |
s = requests.Session() | |
result = s.get(api_url + query + '&lang=en'+ '&sessionId=1234567890', headers=head) | |
result = result.json() | |
result = result.get('result') | |
fulfil = result.get('fulfillment') | |
data= fulfil.get('data') | |
if data is None: | |
speech= fulfil.get('speech') | |
fb={"text": speech} | |
else: | |
fb = data.get('facebook') | |
element=[] | |
element.append(fb) | |
res = json.dumps(element, indent=4) | |
r = make_response(res) | |
#r.headers['Content-Type'] = 'application/json' | |
return r | |
if __name__ == '__main__': | |
port = int(os.getenv('PORT', 5000)) | |
app.run(debug=False, port=port, host='0.0.0.0') |
Thank you for pointing out this. I updated code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My version of this wasn't working anymore. Apparently API.AI changed the sessionID parameter in JSON fields to "required". I added a 10 digit session id token (line 15) to my fork and it works again now.