/// <summary>
        /// Handle a DialogFlow conversation request.
        /// An instance of this class handles just one conversation,
        /// which is tracked external to this class, using the DialogFlow sessionID.
        /// </summary>
        /// <param name="req">Conversation request</param>
        /// <returns>A JSON response which is passed back to DialogFlow</returns>
        public async Task <string> HandleAsync(ConvRequest req)
        {
            // Use reflection to find the handler method for the requested DialogFlow intent
            var handler = FindHandler(req.IntentName);

            if (handler == null)
            {
                return(DialogflowApp.Tell($"Sorry, no handler found for intent: {req.IntentName}"));
            }

            try
            {
                using (_tracer.StartSpan(req.IntentName))
                {
                    // Call the sync handler, if there is one. If not, call the async handler.
                    // Otherwise, it's an error.
                    return(handler.Handle(req) ??
                           await handler.HandleAsync(req) ??
                           DialogflowApp.Tell("Error. Handler did not return a valid response."));
                }
            }
            catch (Exception e) when(req.IntentName != "exception.throw")
            {
                _exceptionLogger.Log(e);
                var msg = (e as GoogleApiException)?.Error.Message ?? e.Message;

                return(DialogflowApp.Tell($"Sorry, there's a problem: {msg}"));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handles received HTTP request. For details of the expected request,
        /// please see Dialogflow fulfillment doc: https://dialogflow.com/docs/fulfillment
        /// </summary>
        /// <param name="httpRequest">HTTP request</param>
        /// <returns>A response to the request which usually includes a spoken fulfillment</returns>
        public async Task <string> HandleRequest(HttpRequest httpRequest)
        {
            using (_tracer.StartSpan(nameof(DialogflowApp)))
            {
                var request = await ConvRequest.ParseAsync(httpRequest);

                _logger.LogInformation($"Intent: '{request.IntentName}',  QueryText: '{request.QueryText}'");

                var conversation = GetOrCreateConversation(request);

                using (_tracer.StartSpan("Conversation"))
                {
                    return(await conversation.HandleAsync(request));
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Given a conversation request with a session id, either get the existing
        /// conversation or create a new one.
        /// </summary>
        /// <param name="convRequest">Conversation request</param>
        /// <returns>Conversation</returns>
        private Conversation GetOrCreateConversation(ConvRequest convRequest)
        {
            Conversation conversation;

            lock (conversations)
            {
                var sessionId = convRequest.SessionId;
                if (!conversations.TryGetValue(sessionId, out conversation))
                {
                    _logger.LogInformation($"Creating new conversation with sessionId: {sessionId}");
                    conversation = new Conversation(_exceptionLogger, _tracer);
                    conversations.Add(sessionId, conversation);
                }
            }

            return(conversation);
        }