예제 #1
0
        WitActions ValidateActions(WitActions actions)
        {
            if (!actions.ContainsKey("send"))
            {
                Console.WriteLine("The 'send' action is missing.");
            }

            return(actions);
        }
예제 #2
0
        /// <summary>
        /// Initializes new instance of Wit class
        /// </summary>
        /// <param name="accessToken">Client access token. You can grab it from your Wit console, under Settings\Access Token.</param>
        /// <param name="actions">(optional if you only use message()) the dictionary with your actions. Actions has action names as keys and action implementations as values.</param>
        public Wit(string accessToken, WitActions actions = null)
        {
            client = PrepareRestClient(accessToken);

            sessions = new Dictionary <string, int>();

            if (actions != null)
            {
                this.actions = ValidateActions(actions);
            }
        }
예제 #3
0
        public WitService()
        {
            _actions = new WitActions();
            _actions["send"] = Send;
            _actions["time"] = TellTheTime;

            _witClient = new Wit("2PDIOI3QNKWLN5HQMSQLZJT4DYUSZ2RT", _actions)
            {
                WIT_API_VERSION = "20170307"
            };
        }
예제 #4
0
        static void Main(string[] args)
        {
            var actions = new WitActions();

            actions["send"] = Send;


            Wit wit = new Wit(accessToken: "<SERVER_ACCESS_TOKEN>", actions: actions);

            wit.Converse("session-id-01", "Hi!", new WitContext());
        }
예제 #5
0
        public void Brain()
        {
            var actions = new WitActions();

            actions["send"] = Send;

            Wit client = new Wit(accessToken: accessToken, actions: actions);

            var    response = client.Message("hello dupa");
            string dupa     = String.Join("", response.Entities.Values);


            dynamic reply = response.Entities.Values;



            Console.WriteLine("Yay, got Wit.ai response: " + response.Entities.ToString());
        }
예제 #6
0
        BotResponse _RunActions(string sessionId, int currentRequest,
                                string message, BotResponse response, int maxSteps = 5, bool verbose = true)
        {
            if (maxSteps <= 0)
            {
                throw new WitException("Max steps reached, stopping.");
            }

            var json = Converse(sessionId, message, response.Context, verbose);

            if (json.Type == null)
            {
                throw new WitException("Couldn\'t find type in Wit response");
            }

            if (currentRequest != sessions[sessionId])
            {
                return(response);
            }

            // backwards-compatibility with API version 20160516
            if (json.Type == "merge")
            {
                json.Type   = "action";
                json.Action = "merge";
            }

            if (json.Type == "error")
            {
                throw new Exception("Oops, I don\'t know what to do.");
            }

            if (json.Type == "stop")
            {
                return(response);
            }

            var request = new ConverseRequest();

            request.SessionId = sessionId;
            request.Context   = response.Context;
            request.Message   = message;
            request.Entities  = json.Entities;

            switch (json.Type)
            {
            case "msg":
                ThrowIfActionMissing("send");

                var converseResponse = new ConverseResponse();
                converseResponse.Msg          = json.Msg;
                converseResponse.QuickReplies = json.QuickReplies;

                // SDK is able to handle multiple bot responses at the same time
                response.Messages.Add(converseResponse.Msg);

                actions["send"](request, converseResponse);
                // actions["send"](request);
                break;

            case "action":
                var action = json.Action;
                ThrowIfActionMissing(action);
                response.Context = actions[action](request, null);
                // context = this.actions[action](request);
                if (response.Context == null)
                {
                    Console.WriteLine("missing context - did you forget to return it?");
                    response.Context = new WitContext();
                }

                break;

            default:
                throw new WitException($"unknown type:  {json.Type}");
            }

            if (currentRequest != sessions[sessionId])
            {
                return(response);
            }

            return(_RunActions(sessionId, currentRequest, null, response, maxSteps - 1, verbose));
        }