0

I am new to working with dialogflow and fairly new to .NET. I have been struggling for a while now to create my fulfillment webhook. I have got it to work with the node.js inline-editor but want to create my own WebhookController in .NET so I can make external API calls/db calls more easily. Here is what I have so far:

I have a really basic whats-app-like UI where a user can input some text which is appended to a chat window and then the javascript is pinged for the chatbot's response:

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);
    }
});

The ajax call pings my HomeController class which connects to 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);
    }
}

So far all of the above works a charm with the inline-editor in Dialogflow. I now am creating my webhook fulfilment in .NET and cannot get it to work. My API class looks like this:

 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;
        }
}

When I run this, I get in the dialogflow console's diagnostic info a 'DEADLINE_EXCEEDED' message however the webhook is doing so little I don't understand why this is?

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

I don't know if I'm supposed to perform some sort of authentication in the webhook as well as in my HomeController?

Some help would be greatly greatly appreciated!!!

Many thanks!

plr108
  • 1,201
  • 11
  • 16
  • This might shed a little light? Depends on what you're doing I would think: [Webhook Call Failed](https://stackoverflow.com/questions/59695057/webhook-call-failed-error-deadline-exceeded-request-timed-out) – Ingenioushax Feb 03 '20 at 17:15

1 Answers1

0

I was getting this same error when i enabled the webhook call from a follow up intent that wasn't mapped (to handler) in fulfillment inline editor.

Mark
  • 1