简体   繁体   English

使用Python通过Cloudfunctions将Dialogflow与Google BigQuery连接

[英]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? 我是Python和云函数的新手,但我看到可以通过Cloudfunctions连接Dialogflow和GoogleBigQuery,但我不知道该如何做,有人可以向我解释一下该如何做,或者我是否尝试这样做至少接近?

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). 同样,用于云功能的python消除了代码的很多开销(尽管您仍然需要它来进行本地测试)。 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. jsonify接受一个dict对象并返回一个application / json响应。 Remember to add pandas and pandas-gbq in your requirements. 请记住在您的需求中添加pandas和pandas-gbq。

Finally, if you are using cloud functions to set up a custom webhook for Dialogflow, remember to check the request and response formats. 最后,如果您使用云功能为Dialogflow设置自定义 Webhook,请记住检查请求响应格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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