与BigQuery进行Dialogflow集成-将整数参数传递给BQ

时间:2019-03-02 10:47:36

标签: google-bigquery dialogflow

我正在创建一个聊天机器人,该机器人从BigQuery检索数据,但是Dialogflow中的数据类型存在问题:

Dialogflow参数为:评分(@ sys.number-int),国家(字符串),部门(字符串)

当我执行以下代码时,bigquery似乎正在以字符串形式接收Rating(Country和Department是在BigQuery中正常工作的字符串),所以这就是为什么我尝试了CAST但没有运气的原因。

有人可以帮我从Dialogflow将INT64变量传递给BigQuery吗?

 function buyAgainPredictor(agent) {
    const OPTIONS = {
                query: 'WITH pred_table AS (SELECT CAST(`request.body.queryResult.outputContexts[0].parameters.Rating´ AS INT64) as Rating, "request.body.queryResult.outputContexts[0].parameters.Department" as Department,  "request.body.queryResult.outputContexts[0].parameters.Country" as Country)' +
                'SELECT cast(predicted_label as INT64) as predicted_label ' +
                'FROM ML.PREDICT(MODEL Customer_feedback.recommend_model,  TABLE pred_table)',
                timeoutMs: 10000,
                useLegacySql: false,
                queryParameters: {}

DialogFlow parameter definition

1 个答案:

答案 0 :(得分:0)

您没有替换字符串中的变量。在query中尝试:

`WITH pred_table AS (
  SELECT ${request.body.queryResult.outputContexts[0].parameters.Rating} as Rating,
     "${request.body.queryResult.outputContexts[0].parameters.Department}" as Department,
     "${request.body.queryResult.outputContexts[0].parameters.Country}" as Country)

SELECT cast(predicted_label as INT64) as predicted_label 
     FROM ML.PREDICT(MODEL Customer_feedback.recommend_model, TABLE pred_table)`

通常,您不想在SQL查询(SQL injection)中使用用户输入,请查看parameterized queries上的文档。考虑到这一点,您可以尝试以下方法:

const OPTIONS = {
    query: `WITH pred_table AS (
                SELECT @rating as Rating,
                       @department as Department,  
                       @country as Country)

            SELECT cast(predicted_label as INT64) as predicted_label
                 FROM ML.PREDICT(MODEL Customer_feedback.recommend_model, TABLE pred_table)`,

    timeoutMs: 10000,
    useLegacySql: false,
    queryParameters: {
        rating: parameters.Rating,
        department: parameters.Department,
        country: parameters.Country
    }
}