Ejemplo n.º 1
0
        /// <summary>
        /// Register an intent handler with this <see cref="IntentRouter"/>.
        /// </summary>
        /// <param name="intentName">Name of the intent as defined in the LUIS application.</param>
        /// <param name="confidenceThreshold">Confidence score required to accitvate the handler.</param>
        /// <param name="handler">Function to call when routing the intent to this handler.</param>
        public void RegisterHandler(string intentName, double confidenceThreshold, IntentHandlerFunc handler)
        {
            if (string.IsNullOrWhiteSpace(intentName))
            {
                throw new ArgumentException(nameof(intentName));
            }
            if ((confidenceThreshold < 0) || (confidenceThreshold > 1))
            {
                throw new ArgumentOutOfRangeException(nameof(confidenceThreshold));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            if (_handlers.ContainsKey(intentName))
            {
                throw new InvalidOperationException($"Router already has a handler for intent '{intentName}'");
            }

            _handlers[intentName] = new HandlerDetails()
            {
                Threshold = confidenceThreshold,
                Exec      = handler
            };
        }
Ejemplo n.º 2
0
        public static void LoadRegistered()
        {
            _loadedHandlers.Clear();

            var settings = ConfigManager.LoadConfig <HandlerRegistry>("handlers");

            var handlerDetails = new List <HandlerDetails>();

            var assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();

            foreach (var handler in settings.Handlers)
            {
                var  name        = handler.Name;
                Type correctType = null;
                try
                {
                    correctType = assemblyTypes.First(t => {
                        return(t.Name == handler.Type &&
                               typeof(ITweetHandler).IsAssignableFrom(t) &&
                               typeof(ITweetHandler).Name != t.Name);
                    });
                }
                catch (InvalidOperationException ex)
                {
                    throw new InvalidOperationException($"Could not find tweet handler type for '{handler.Type}'", ex);
                }

                var data = handler.Data;

                HandlerDetails details = new HandlerDetails(name, correctType, data);
                handlerDetails.Add(details);
            }

            foreach (var details in handlerDetails)
            {
                ITweetHandler createdHandler = Create(details.Type, details.Data);
                _loadedHandlers.Add(details.Name, createdHandler);
            }
        }