0

我是使用 dialogflow 的新手,对 .NET 还很陌生。我一直在努力创建我的履行 webhook。我已经让它与 node.js 内联编辑器一起工作,但想在 .NET 中创建我自己的 WebhookController,这样我就可以更轻松地进行外部 API 调用/db 调用。这是我到目前为止所拥有的:

我有一个非常基本的类似whats-app 的 UI,用户可以在其中输入一些附加到聊天窗口的文本,然后 ping javascript 以获取聊天机器人的响应:

function userSubmit() {

var userInput = document.getElementById('user-input').value;

$.ajax({
    type: "GET",
    url: "/Home/CheckIntentAsync",
    data: {
        userInput: userInput
    },
    async: true,
    contentType: "application/json",
    success: function (data) {
        var reply = data;
        var botNode = document.createElement("div");
        botNode.classList.add('chat');
        botNode.classList.add('bot-chat');
        botNode.innerHTML = reply; // <--- appends chat window with the reply from Dialogflow
        chatWindow.appendChild(botNode);
        chatWindow.scrollTop = chatWindow.scrollHeight;
        console.log(data);
    },
    error: function () {
        var reply = "I didn't quite catch that, can you rephrase? :/";
        var botNode = document.createElement("div");
        botNode.classList.add('chat');
        botNode.classList.add('bot-chat');
        botNode.innerHTML = reply;
        chatWindow.appendChild(botNode);
    }
});

ajax 调用 ping 连接到 Dialogflow 的 HomeController 类:

public class HomeController : Controller
{

    private string sessionID = "XXX"; // my session ID
    private string projectID = "XXX"; // my projectID

    public ActionResult Index()
    {
        SetEnvironmentVariable();
        return View();
    }

   private void SetEnvironmentVariable()
    {
        try
        {
           Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "MY PATH TO SERVICE ACCOUNT PRIVATE KEY IS HERE");
        }
        catch (ArgumentNullException)
        {
            throw;
        }
        catch (ArgumentException)
        {
            throw;
        }
    }

    [HttpGet]
    public async Task<JsonResult> CheckIntentAsync(string userInput)
    {
        var sessionClient = await SessionsClient.CreateAsync();
        var sessionName = new SessionName(projectID, sessionID);

        QueryInput queryInput = new QueryInput();
        var queryText = new TextInput();
        queryText.Text = userInput;
        queryText.LanguageCode = "en";
        queryInput.Text = queryText;

        // Make the request
        DetectIntentResponse response = await sessionClient.DetectIntentAsync(sessionName, queryInput);

        var reply = response.QueryResult;

        return Json(reply, JsonRequestBehavior.AllowGet);
    }
}

到目前为止,上述所有内容都与 Dialogflow 中的内联编辑器一起发挥了作用。我现在正在 .NET 中创建我的 webhook 实现,但无法让它工作。我的 API 类如下所示:

 public class WebhookController : ApiController
    {
        private static readonly JsonParser jsonParser =
            new JsonParser(JsonParser.Settings.Default.WithIgnoreUnknownFields(true));

        [HttpPost]
        public async Task<HttpResponseMessage> Post()
        {
            WebhookRequest request;
            using (var stream = await Request.Content.ReadAsStreamAsync())
            {
                using (var reader = new StreamReader(stream))
                {
                    request = jsonParser.Parse<WebhookRequest>(reader);
                }
            }
// Simply sets the fulfillment text to equal the name of the intent detected by Dialogflow
            WebhookResponse webhookResponse = new WebhookResponse
            {
                FulfillmentText = request.QueryResult.Intent.DisplayName
            };

            HttpResponseMessage httpResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(webhookResponse.ToString())
                {
                    Headers = { ContentType = new MediaTypeHeaderValue("text/json") }
                }
            };
            return httpResponse;
        }
}

当我运行它时,我在对话框流控制台的诊断信息中收到一条“DEADLINE_EXCEEDED”消息,但是 webhook 做的很少,我不明白这是为什么?

  "webhookStatus": {
    "code": 4,
    "message": "Webhook call failed. Error: DEADLINE_EXCEEDED."
  }

我不知道我是否应该在 webhook 以及我的 HomeController 中执行某种身份验证?

一些帮助将不胜感激!!!

非常感谢!

4

1 回答 1

0

当我从未在履行内联编辑器中映射(到处理程序)的后续意图启用 webhook 调用时,我遇到了同样的错误。

于 2020-03-01T21:29:24.037 回答