示例#1
0
        /// <summary>
        /// Makes a request to the LUIS service to parse <paramref name="text"/> for intents and entities.
        /// </summary>
        /// <param name="text">Natural language text string to parse.</param>
        /// <param name="context">Opaque context object to pass through to activated intent handler</param>
        /// <returns>True if an intent was routed and handled.</returns>
        public async Task <bool> Route(string text, object context)
        {
            if (_client == null)
            {
                throw new InvalidOperationException("No API endpoint has been specified for this IntentRouter");
            }

            var result = await _client.Predict(text);

            return(await Route(result, context));
        }
示例#2
0
        public IHttpActionResult POST()

        {
            try

            {
                //設定ChannelAccessToken(或抓取Web.Config)

                this.ChannelAccessToken = "qw6hghZir3I1lTq2PQCZ5Ap30I3eDEUju5UXCeS1E1vi8ja2QftluqLEf+TyAfmaS6CDLava+i1jzHEpy9rVVfxXz00kL615WL0mNasRf+Ge9pnxLv2sEmhg9Ml7AGT5MsqM7TktOmovad2CB8H4bgdB04t89/1O/w1cDnyilFU=";
                const string LuisAppId  = "c8a36302-0cc4-426d-b4ba-cac8234a0f09";
                const string LuisAppKey = "7d43e486a4fd49fe8e743470b8e5e8c6";
                const string Luisdomain = "westus";
                Microsoft.Cognitive.LUIS.LuisClient lc =
                    new Microsoft.Cognitive.LUIS.LuisClient(LuisAppId, LuisAppKey, true, Luisdomain);
                //取得Line Event

                var    item    = this.ReceivedMessage.events.FirstOrDefault();
                string Message = "";

                //回覆訊息
                switch (item.type)

                {
                case "join":

                    Message = $"有人把我加入{item.source.type}中了,大家好啊~";



                    //回覆用戶

                    isRock.LineBot.Utility.ReplyMessage(ReceivedMessage.events[0].replyToken, Message, ChannelAccessToken);

                    break;

                case "message":


                    if (item.message.text == "bye")

                    {
                        //回覆用戶

                        isRock.LineBot.Utility.ReplyMessage(item.replyToken, "bye-bye", ChannelAccessToken);

                        //離開

                        if (item.source.type.ToLower() == "room")
                        {
                            isRock.LineBot.Utility.LeaveRoom(item.source.roomId, ChannelAccessToken);
                        }

                        if (item.source.type.ToLower() == "group")
                        {
                            isRock.LineBot.Utility.LeaveGroup(item.source.roomId, ChannelAccessToken);
                        }
                    }
                    else if (item.message.text == "ID" || item.message.text == "id")
                    {
                        Message = "你的user id: " + Convert.ToString(item.source.userId);
                    }
                    else
                    {
                        var ret = lc.Predict(item.message.text).Result;
                        if (ret.TopScoringIntent.Name == "打招呼")
                        {
                            Message = "你好啊!!!";
                        }
                        else if (ret.TopScoringIntent.Name == "時間")
                        {
                            Message = "現在時間 : " + System.DateTime.Now;
                        }
                        else
                        {
                            Message = "??";
                        }
                    }
                    //回覆用戶
                    isRock.LineBot.Utility.ReplyMessage(item.replyToken, Message, ChannelAccessToken);

                    break;

                default:

                    break;
                }



                //response OK

                return(Ok());
            }

            catch (Exception ex)

            {
                //回覆訊息

                this.PushMessage("Ued3ad06f07d7d541a9cc742c7f2dcb5c", "發生錯誤:\n" + ex.Message);

                //response OK

                return(Ok());
            }
        }
示例#3
0
        public MainWindow()
        {
            InitializeComponent();

            Locale            = "en-US";
            english.IsChecked = true;

            // See https://www.luis.ai/applications
            // Make pi run the following command: python app.py 'CONNECTION_STRING'

            var enterKeyStream = Observable.FromEventPattern <KeyEventArgs>(command, "KeyDown")
                                 .Select(e => ((KeyEventArgs)e.EventArgs).Key)
                                 .Where(k => k == Key.Enter)
                                 .Select(k => (Object)null);

            Observable.FromEventPattern(submit, "Click")
            .Merge(enterKeyStream)
            .ObserveOnDispatcher()
            .Select(_ =>
            {
                var text           = command.Text;
                responseText.Text += "Recognized command: " + text;

                command.Text      = "";
                responseText.Text = "";
                return(text);
            })
            .Where(text => !String.IsNullOrWhiteSpace(text))
            .ObserveOnDispatcher()
            .SelectMany(text =>
            {
                responseText.Text += "Analyzing text... ";
                Debug.WriteLine("Predicting: " + text);
                return(_luisClient.Predict(text).ToObservable());
            })
            .ObserveOnDispatcher()
            .SelectMany(result =>
            {
                responseText.Text += "Done";
                responseText.Text += "\nIntent: " + result.TopScoringIntent.Name;
                Debug.WriteLine(result.TopScoringIntent.Name + ": " + result.TopScoringIntent.Score);

                bool isLight       = result.Entities.ContainsKey("light");
                responseText.Text += "\nEntity: " + (isLight ? "light" : "unknown");

                if (isLight)
                {
                    responseText.Text += "\nSending command... ";
                    return(SendIntentAsync(_iotClient, result.TopScoringIntent.Name, "light", parseFrequency(result)).ToObservable());
                }
                else
                {
                    responseText.Text += "\nNO ENTITIES FOUND. No action required. ";
                    return(Task.Delay(TimeSpan.FromMilliseconds(0)).ToObservable());
                }
            })
            .ObserveOnDispatcher()
            .Subscribe(result =>
            {
                responseText.Text += "Done";
            });
        }