简体   繁体   中英

Connect Dialogflow with Google BigQuery via Cloudfunctions with Python

I'm new on Python and cloud functions but I saw that it's possible to connect Dialogflow and GoogleBigQuery via Cloudfunctions but I do not understand how to make this, can someone explain me how to do it or if the way I'm trying to it's at least close?

import flask
from flask import Flask
from flask import request
from flask import make_response

app = Flask(__name__)
@app.route('/prueba_1', methods=['POST'])
def prueba_1():
    import json
    import pandas as pd
    ss = pd.read_gbq("SELECT something FROM bigquery_table LIMIT 1","arbor-209819")
    ll = {
        "speech" : ss.to_json(),
        "displayText": ss.to_json(),
        "source": "apiai-weather-webhook-sample"
    }
    res = json.dumps(ll, indent=4)
    r = make_response(res)
    r.headers['Content-Type'] = 'application/json'
    return r

if __name__ == '__main__':
    app.run(debug=True)

Thank you so much.

You are close, your function needs to accept the request. Also, python for cloud functions removes a lot of the overhead out of the code (you will still need it for local testing though). You can follow this quickstart to get you started.

Here is an example of how it could be written:

from flask import jsonify
import pandas as pd

def prueba_1(request):

    # Your code

    ss = pd.read_gbq('SELECT * FROM my_dataset.my_table')

    # More code

    return jsonify(my_dictionary)

jsonify takes a dict object and returns an application/json Response. Remember to add pandas and pandas-gbq in your requirements.

Finally, if you are using cloud functions to set up a custom webhook for Dialogflow, remember to check the request and response formats.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM