在 .NET C# 中为 Dialogflow 编写实现 Webhook



我是使用dialogflow的新手,对.NET相当陌生。我一直在努力创建我的实现网络钩子。我已经让它与节点.js内联编辑器一起使用,但想在 .NET 中创建我自己的 WebhookController,这样我就可以更轻松地进行外部 API 调用/数据库调用。这是我到目前为止所拥有的:

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

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 我的 HomeController 类,该类连接到 Dialogflow:

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 中执行某种身份验证?

一些帮助将不胜感激!!

非常感谢!

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

相关内容

  • 没有找到相关文章

最新更新