从Dialogflow中的实现发出HTTP POST请求 [英] Making an HTTP POST request from fulfillment in Dialogflow

查看:106
本文介绍了从Dialogflow中的实现发出HTTP POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写用于生成PDF的处理程序。该API接受带有JSON数据的POST请求,并返回到生成的PDF的链接。该意图触发此代码,但答案未添加到代理中。请求是否可能不会转发到目的地?该API似乎未收到任何请求。知道如何解决吗?

I am writing a handler for an intent to generate a PDF. This API accepts a POST request with the JSON data and returns a link to the generated PDF. The intent triggers this code but the answer is not added to the agent. Is it possible that the request is not forwarded to the destination? The API seems to not get any requests. Any idea how to solve this?

function fillDocument(agent) {
    const name = agent.parameters.name;
    const address = agent.parameters.newaddress;
    const doctype = agent.parameters.doctype;

    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    var xhr = new XMLHttpRequest();
    var url = "https://us1.pdfgeneratorapi.com/api/v3/templates/36628/output?format=pdf&output=url";
    xhr.open("POST", url, true);
    xhr.setRequestHeader("X-Auth-Key", "...");
    xhr.setRequestHeader("X-Auth-Secret", "...");
    xhr.setRequestHeader("X-Auth-Workspace", "...");
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.setRequestHeader("Accept", "application/json");
    xhr.setRequestHeader("Cache-Control", "no-cache");
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            var json = JSON.parse(xhr.responseText);
            agent.add(json.response);
        }
    };
    var data = JSON.stringify({...});
    xhr.send(data);
}

编辑:我继续在GCP中设置结算帐户,现在通话可以,但是是异步的。如果我通过执行以下操作将其更改为syn:

I proceeded to set up a billing account in GCP, now the call works, but it is async. If I change it to syn by doing this:

xhr.open("POST", url, false);

我收到以下错误:

EROFS: read-only file system, open '.node-xmlhttprequest-sync-2'

我需要使其异步,因为我的机器人应发送的响应取决于API的响应。有关如何解决此问题的任何想法?

I need it to be async as the response my bot is supposed to send depends on the response from the API. Any ideas on how to go around this?

推荐答案

如果进行异步调用,则处理程序函数需要返回Promise。否则,处理程序调度程序将不知道存在异步调用,并且将在函数返回后立即结束。

If you are doing async calls, your handler function needs to return a Promise. Otherwise the handler dispatcher doesn't know there is an async call and will end immediately after the function returns.

对网络调用使用promise的最简单方法是使用a软件包,例如 request-promise-native 。使用此代码,您的代码可能类似于:

The easiest way to use promises with network calls is to use a package such as request-promise-native. Using this, your code might look something like:

var options = {
  uri: url,
  method: 'POST',
  json: true,
  headers: { ... }
};
return rp(options)
  .then( body => {
    var val = body.someParameter;
    var msg = `The value is ${val}`;
    agent.add( msg );
  });

如果您确实想继续使用xhr,则需要将其包装在Promise中。可能是这样的

If you really wanted to keep using xhr, you need to wrap it in a Promise. Possibly something like

return new Promise( (resolve,reject) => {

  var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
  // ... other XMLHttpRequest setup here
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      var json = JSON.parse(xhr.responseText);
      agent.add(json.response);
      resolve();
    }
  };

});

这篇关于从Dialogflow中的实现发出HTTP POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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