在没有firebase的情况下,如何在dialogflow实现中向其他服务发出http请求? [英] How to make a http request to other service in dialogflow fulfillment without firebase?

查看:68
本文介绍了在没有firebase的情况下,如何在dialogflow实现中向其他服务发出http请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个node.js实现代码,只需使用 http.request 函数将我的消息从Dialogflow发送到另一个服务(在此代码中是邮递员)。 。

I wrote a node.js fulfillment code to send my messages from Dialogflow to another services (in this code, it is postman) by simply using the http.request function.

"use strict";

const express = require("express");
const bodyParser = require("body-parser");
const https = require('https');
const app = express();

app.use(
  bodyParser.urlencoded({
    extended: true
  })
);

app.use(bodyParser.json())

app.post("/echo", function(req, res) {
    var speech =
    req.body.queryResult &&
    req.body.queryResult.parameters && 
    req.body.queryResult.parameters.echoText
      ? req.body.queryResult.parameters.echoText
      : "Seems like some problem. Speak again.";
    
    function mainFunction(callback){    

    var options = {
      hostname : "postman-echo.com",
      path : "/post",
      method : "POST",
      headers: {
        'Content-Type': 'application/json'
    }
    };

    var body = JSON.stringify({
        message: speech
    });
    
    
    var req = https.request(options, function(res) {
        var respBody = "";
        console.log("Response from server started");
        console.log(`Server status: ${res.statusCode}`);
    });
    
    //Necessary, otherwise a socket hangup error
    req.end(body);
    
    req.on('response', function(res){
        //console.log('STATUS: ' + res.statusCode);
        res.setEncoding('utf8');

        res.on('data',function(chunk){
            var temp = JSON.parse(chunk.toString());
            //console.log('Dit is de response');
            console.log(temp.data.message);
            
            var resp_message = [];
            if (temp.data.length > 1) {
              var i;
              for (i = 0; i < temp.data.length; i++) {
                var resp_message_single = temp.data[i].message;
                resp_message.push(resp_message_single);
              };
              callback(resp_message)
            }
            else{
              var resp_message_single = temp.data.message;
              resp_message.push(resp_message_single);
              callback(resp_message)
            };
        });
    });
  };

  mainFunction(function(resp_message){
    if (resp_message.length != 1) {
      var i;
      speech = "";
      var displayText = "";
      for (i = 0; i < resp_message.length; i++) {
        speech = speech+resp_message[i]+'<break time="500ms"/>';
        displayText = displayText+resp_message[i]+" ";
      };
      speech = '<speak>'+speech+'</speak>';
    }
    else{
      speech ='<speak>'+resp_message+'</speak>';
      displayText = resp_message[0];
    };
    return res.json({
      fulfillmentText: speech,
      fulfillmentMessages:[
        {
          text: {
            text: [
              displayText
            ]
          }
        }
      ]
    });
  });

});


const port = process.env.PORT || 8000;
app.listen(port,()=> console.log(`Listening on port ${port}...`));

现在我根据此链接,方法是使用Google Actions-on-google模块,但没有Firebase。但是,我似乎无法按我的方式在 assistant.intent 函数中使用 http.request 函数

Now I updated my code according to this link by using the actions-on-google module, but without Firebase. However, I can't seem to use the http.request function inside the assistant.intent function in the way I used it before.

"use strict";

const express = require("express");
const bodyParser = require("body-parser");
const https = require('https');

const {dialogflow} = require('actions-on-google');

const assistant = dialogflow();
const app = express();

app.use(
  bodyParser.urlencoded({
    extended: true
  })
);

app.use(bodyParser.json());

assistant.intent('Default Welcome Intent', (conv) => {
  conv.ask('Welkom bij test.');
});

assistant.intent('Echo', (conv) => {
var speech = conv.query    

  var options = {
    hostname : "postman-echo.com",
    path : "/post",
    method : "POST",
    headers: {
      'Content-Type': 'application/json'
  }
  };;

  var body = JSON.stringify({message: speech});
    
  var req = https.request(options, function(res) {
      var respBody = "";
  });

  req.end(body);
  
  req.on('response', function(res){
      res.setEncoding('utf8');

      res.on('data',function(chunk){
          var temp = JSON.parse(chunk.toString());
          var resp_message = [];
          if (temp.data.length != 1) {
            var i;
            for (i = 0; i < temp.data.length; i++) {
              var resp_message_single = temp.data[i].message;
              resp_message.push(resp_message_single);
            };
            callback(resp_message)
          }
          else{
            var resp_message_single = temp.data[0].message;
            resp_message.push(resp_message_single);
            callback(resp_message)
          };
      });
  });

  mainFunction(function(resp_message){
    if (resp_message.length != 1) {
      var i;
      speech = "";
      var displayText = "";
      for (i = 0; i < resp_message.length; i++) {
        speech = speech+resp_message[i]+'<break time="500ms"/>';
        displayText = displayText+resp_message[i]+" ";
      };
      speech = '<speak>'+speech+'</speak>';
    }
    else{
      speech ='<speak>'+resp_message+'</speak>';
      displayText = resp_message[0];
    };
    conv.ask(speech);
    });

});

app.post('/echo', assistant);

const port = process.env.PORT || 8000;
app.listen(port,()=> console.log(`Listening on port ${port}...`));

有人知道为什么会这样吗?

Does someone know why this happens? Is the actions-on-google module preventing that?

推荐答案

您的代码中有一些问题是使用on-on的? -google库。一个问题是没有定义回调 mainFunction()-但是,这些错误隐藏了一个更大的问题,即

You have a few issues in your code that uses the actions-on-google library. One issue is that neither callback nor mainFunction() are defined - however, these errors hide a bigger problem that you'll run into.

在使用Google动作库(以及为此使用dialogflow-fillfillment库注册Intent Handler)时,您应该如果您使用的是异步返回的函数,则返回Promise,这正是 http.request 的作用。

When using the actions-on-google library (and when registering Intent Handlers using the dialogflow-fulfillment library, for that matter), you are expected to return a Promise if you're using a function that returns asynchronously, which is exactly what http.request does.

最简单的方法是切换到 request-promise-native 之类的东西

The easiest way is to switch to something like the request-promise-native library.

我还没有测试它,但是Echo Intent处理程序的代码可能看起来像这样:

I haven't tested it, but the code for the Echo intent handler might look something like this:

assistant.intent('Echo', (conv) =>{
  const requestPromise = require('request-promise-native');
  const speech = conv.query;

  const options = {
    uri: "http://postman-echo.com/post",
    method: "POST",
    json: true,
    body: {
      message: speech
    }
  };

  return requestPromise( options )
    .then( body => {
      let speech = "I didn't hear anything.";
      let text   = "I didn't hear anything.";
      if (body.data && body.data.length > 0){
        speech = body.data[0].message;
        text   = body.data[0].message;
        for (let co=1; co<body.data.length; co++){
          speech += `<break time="500ms"/>${body.data[co].message}`;
          text   += ' '+body.data[co].message;
        }
      }
      speech = `<speak>${speech}</speak>`;
      return conv.ask( new SimpleResponse({
        speech: speech,
        text:   text
      }));
    });

});

这篇关于在没有firebase的情况下,如何在dialogflow实现中向其他服务发出http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆