private static async Task <LUIS> GetIntentFromLUIS(string Query) { Query = Uri.EscapeDataString(Query); LUIS Data = new LUIS(); using (HttpClient client = new HttpClient()) { //string RequestURI = "https://api.projectoxford.ai/luis/v1/application?id=28745440-fd03-4658-b190-9c154fe89d89&subscription-key=7efb093087dd48918b903885b944740c&q=" + Query; string RequestURI = "https://api.projectoxford.ai/luis/v1/application?id=1adab70c-f7a6-4d5c-9809-c27672653896&subscription-key=7489b95cf3fb4797939ea70ce94a4b11&q=" + Query; HttpResponseMessage msg = await client.GetAsync(RequestURI); if (msg.IsSuccessStatusCode) { var JsonDataResponse = await msg.Content.ReadAsStringAsync(); Data = JsonConvert.DeserializeObject <LUIS>(JsonDataResponse); } } return(Data); }
/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> /// //ThreadTest test = new ThreadTest(); //delegate void del_thread(string str); //public static void new_thread(string name) //{ // Thread cur_thread = Thread.CurrentThread; // Debug.WriteLine("Current {0} Thread = {1}", name, cur_thread.ManagedThreadId); //} public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { //Thread workerThread = Thread.CurrentThread; //Worker workerObject = new Worker(); //Thread workerThread = new Thread(workerObject.DoWork); // welcome message 출력 if (activity.Type == ActivityTypes.ConversationUpdate && activity.MembersAdded.Any(m => m.Id == activity.Recipient.Id)) { WeatherInfo weatherInfo = await GetWeatherInfo(); //weatherInfo.list[0].weather[0].description Debug.WriteLine("weatherInfo : " + weatherInfo.list[0].weather[0].description); Debug.WriteLine("weatherInfo : " + string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.min, 1))); Debug.WriteLine("weatherInfo : " + string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.max, 1))); DateTime startTime = DateTime.Now; //ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); //Activity reply = activity.CreateReply(""); //reply.Recipient = activity.From; //reply.Type = "message"; //reply.Attachments = new List<Attachment>(); //reply.AttachmentLayout = AttachmentLayoutTypes.Carousel; // Db DbConnect db = new DbConnect(); List <Dialog> dlg = db.SelectDialog(3); Debug.WriteLine("!!!!!!!!!!! : " + dlg[0].dlgId); ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); Activity reply2 = activity.CreateReply(); reply2.Recipient = activity.From; reply2.Type = "message"; reply2.Attachments = new List <Attachment>(); reply2.AttachmentLayout = AttachmentLayoutTypes.Carousel; //reply.Recipient = activity.From; //reply.Type = "message"; //reply.Attachments = new List<Attachment>(); //reply.AttachmentLayout = AttachmentLayoutTypes.Carousel; List <Card> card = db.SelectDialogCard(dlg[0].dlgId); VideoCard[] plVideoCard = new VideoCard[card.Count]; HeroCard[] plHeroCard = new HeroCard[card.Count]; ReceiptCard[] plReceiptCard = new ReceiptCard[card.Count]; Attachment[] plAttachment = new Attachment[card.Count]; for (int i = 0; i < card.Count; i++) { List <Button> btn = db.SelectBtn(card[i].dlgId, card[i].cardId); List <Image> img = db.SelectImage(card[i].dlgId, card[i].cardId); List <Media> media = db.SelectMedia(card[i].dlgId, card[i].cardId); List <CardAction> cardButtons = new List <CardAction>(); CardAction[] plButton = new CardAction[btn.Count]; ThumbnailUrl plThumnail = new ThumbnailUrl(); List <MediaUrl> mediaURL = new List <MediaUrl>(); MediaUrl[] plMediaUrl = new MediaUrl[media.Count]; for (int n = 0; n < img.Count; n++) { if (img[n].imgUrl != null) { plThumnail.Url = img[n].imgUrl; } } for (int l = 0; l < media.Count; l++) { if (media[l].mediaUrl != null) { plMediaUrl[l] = new MediaUrl() { Url = media[l].mediaUrl }; } } mediaURL = new List <MediaUrl>(plMediaUrl); for (int m = 0; m < btn.Count; m++) { if (btn[m].btnTitle != null) { plButton[m] = new CardAction() { Value = btn[m].btnContext, Type = btn[m].btnType, Title = btn[m].btnTitle }; } } cardButtons = new List <CardAction>(plButton); if (card[i].cardType == "videocard") { plVideoCard[i] = new VideoCard() { Title = "**" + card[i].cardTitle + "**", Text = card[i].cardText, Subtitle = card[i].cardSubTitle, Media = mediaURL, Image = plThumnail, Buttons = cardButtons, Autostart = true }; plAttachment[i] = plVideoCard[i].ToAttachment(); reply2.Attachments.Add(plAttachment[i]); } } var reply1 = await connector.Conversations.SendToConversationAsync(reply2); Debug.WriteLine("activity : " + activity.Id); Debug.WriteLine("activity : " + activity.ChannelId); Debug.WriteLine("activity : " + activity.Conversation.Id); Debug.WriteLine("activity : " + activity.Properties); Debug.WriteLine("activity : " + activity.Recipient); Debug.WriteLine("activity : " + activity.From.Id); Debug.WriteLine("activity : " + activity.Recipient.Id); Debug.WriteLine("end activity.Timestamp : " + activity.Timestamp); DateTime endTime = DateTime.Now; Debug.WriteLine("프로그램 수행시간 : {0}/ms", ((endTime - startTime).Milliseconds)); //GetWeatherInfo(); Activity reply = activity.CreateReply(); for (int n = 0; n < dlg[0].dlgMent.Split(new string[] { "@@" }, StringSplitOptions.None).Length; n++)//new string[] {"@@"},StringSplitOptions.None { reply = activity.CreateReply(dlg[0].dlgMent.Split(new string[] { "@@" }, StringSplitOptions.None)[n]); await connector.Conversations.SendToConversationAsync(reply); } Activity testReply = activity.CreateReply("**가나다라마바사** `jumped`"); await connector.Conversations.SendToConversationAsync(testReply); Activity replyToConversation = activity.CreateReply("Should go to conversation, in carousel format"); replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel; //replyToConversation.AttachmentLayout = "test"; replyToConversation.Attachments = new List <Attachment>(); Dictionary <string, string> cardContentList = new Dictionary <string, string>(); cardContentList.Add("PigLatin", "http://webbot02.azurewebsites.net/hyundai/images/price/Grandeur_24spec.PNG"); cardContentList.Add("Pork Shoulder", "http://webbot02.azurewebsites.net/hyundai/images/price/Grandeur_24spec.PNG"); cardContentList.Add("Bacon", "http://webbot02.azurewebsites.net/hyundai/images/price/Grandeur_24spec.PNG"); foreach (KeyValuePair <string, string> cardContent in cardContentList) { List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: cardContent.Value)); List <CardAction> cardButtons = new List <CardAction>(); CardAction plButton = new CardAction() { Value = $"https://en.wikipedia.org/wiki/{cardContent.Key}", Type = "openUrl", Title = "WikiPedia Page" }; cardButtons.Add(plButton); NewHeroCard plCard = new NewHeroCard() { Title = $"I'm a hero card about {cardContent.Key}", Subtitle = $"{cardContent.Key} Wikipedia Page", Images = cardImages, Buttons = cardButtons, Kind = "test" }; Attachment attachment = plCard.ToAttachment(); replyToConversation.Attachments.Add(attachment); } await connector.Conversations.SendToConversationAsync(replyToConversation); } else if (activity.Type == ActivityTypes.Message) { //test.resume(); //Thread.Sleep(3000); //test.pause(); //Thread.Sleep(3000); //if (workerThread.IsAlive) //{ // workerObject.RequestStop(); // workerThread.Join(); // Debug.WriteLine("main thread: Worker thread has terminated."); //} DateTime startTime = DateTime.Now; long unixTime = ((DateTimeOffset)startTime).ToUnixTimeSeconds(); Debug.WriteLine("startTime : " + startTime); Debug.WriteLine("startTime Millisecond : " + unixTime); Debug.WriteLine("Debuging : " + activity.Text); LUIS Luis = await GetIntentFromLUIS(activity.Text); Debug.WriteLine("Debuging : " + Luis.intents[0].intent); Debug.WriteLine("Debuging : " + Luis.entities[0].entity); Debug.WriteLine("Debuging : " + Luis.entities[0].type); String entitiesStr = ""; for (int i = 0; i < Luis.entities.Count(); i++) { Debug.WriteLine("Split : " + Regex.Split(Luis.entities[i].type, "::")[1]); entitiesStr += Regex.Split(Luis.entities[i].type, "::")[1] + ","; } entitiesStr = entitiesStr.Substring(0, entitiesStr.Length - 1); Debug.WriteLine("entitiesStr : " + entitiesStr); ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); // Db DbConnect db = new DbConnect(); List <Luis> LuisDialogID = db.SelectLuis(Luis.intents[0].intent, entitiesStr); List <Dialog> dlg = db.SelectDialog(LuisDialogID[0].dlgId); if (dlg.Count > 0) { if (dlg[0].dlgMent != null) { // return our reply to the user Activity reply = activity.CreateReply(dlg[0].dlgMent); await connector.Conversations.ReplyToActivityAsync(reply); } } List <Card> card = db.SelectDialogCard(LuisDialogID[0].dlgId); if (card.Count > 0) { // HeroCard Activity replyToConversation = activity.CreateReply(""); Debug.WriteLine("activity : " + activity.Id); Debug.WriteLine("activity : " + activity.Properties); Debug.WriteLine("activity : " + activity.Recipient); Debug.WriteLine("activity : " + activity.Summary); Debug.WriteLine("activity : " + activity.ReplyToId); Debug.WriteLine("activity : " + activity.Recipient.Id); Debug.WriteLine("activity : " + activity.Conversation.Id); replyToConversation.Recipient = activity.From; replyToConversation.Type = "message"; replyToConversation.Attachments = new List <Attachment>(); replyToConversation.AttachmentLayout = AttachmentLayoutTypes.Carousel; for (int i = 0; i < card.Count; i++) { List <Button> btn = db.SelectBtn(card[i].dlgId, card[i].cardId); List <Image> img = db.SelectImage(card[i].dlgId, card[i].cardId); List <Media> media = db.SelectMedia(card[i].dlgId, card[i].cardId); List <CardImage> cardImages = new List <CardImage>(); CardImage[] plImage = new CardImage[img.Count]; ThumbnailUrl plThumnail = new ThumbnailUrl(); List <CardAction> cardButtons = new List <CardAction>(); CardAction[] plButton = new CardAction[btn.Count]; List <MediaUrl> mediaURL = new List <MediaUrl>(); MediaUrl[] plMediaUrl = new MediaUrl[media.Count]; ReceiptCard[] plReceiptCard = new ReceiptCard[card.Count]; HeroCard[] plHeroCard = new HeroCard[card.Count]; VideoCard[] plVideoCard = new VideoCard[card.Count]; Attachment[] plAttachment = new Attachment[card.Count]; for (int l = 0; l < img.Count; l++) { if (card[i].cardType == "herocard") { if (img[l].imgUrl != null) { plImage[l] = new CardImage() { Url = img[l].imgUrl }; } } else if (card[i].cardType == "videocard") { if (img[l].imgUrl != null) { plThumnail.Url = img[l].imgUrl; } } } cardImages = new List <CardImage>(plImage); for (int l = 0; l < media.Count; l++) { if (media[l].mediaUrl != null) { plMediaUrl[l] = new MediaUrl() { Url = media[l].mediaUrl }; } } mediaURL = new List <MediaUrl>(plMediaUrl); for (int m = 0; m < btn.Count; m++) { if (btn[m].btnTitle != null) { plButton[m] = new CardAction() { Value = btn[m].btnContext, Type = btn[m].btnType, Title = btn[m].btnTitle }; } } cardButtons = new List <CardAction>(plButton); if (card[i].cardType == "herocard") { plHeroCard[i] = new HeroCard() { Title = card[i].cardTitle, Text = card[i].cardText, Subtitle = card[i].cardSubTitle, Images = cardImages, Buttons = cardButtons }; plAttachment[i] = plHeroCard[i].ToAttachment(); replyToConversation.Attachments.Add(plAttachment[i]); } else if (card[i].cardType == "videocard") { plVideoCard[i] = new VideoCard() { Title = card[i].cardTitle, Text = card[i].cardText, Subtitle = card[i].cardSubTitle, Image = plThumnail, Media = mediaURL, Buttons = cardButtons, Autostart = true }; plAttachment[i] = plVideoCard[i].ToAttachment(); replyToConversation.Attachments.Add(plAttachment[i]); } } var reply1 = await connector.Conversations.SendToConversationAsync(replyToConversation); } DateTime endTime = DateTime.Now; Debug.WriteLine("프로그램 수행시간 : {0}/ms", ((endTime - startTime).Milliseconds)); //Debug.WriteLine("current main thread = {0}",workerThread.ManagedThreadId); //del_thread new_th = new del_thread(new_thread); //new_th.BeginInvoke("TEST", null, null); //Thread.Sleep(3000); //// Start the worker thread. //workerThread.Start(); //Debug.WriteLine("ID : "+ workerThread.ManagedThreadId); //Debug.WriteLine("main thread: Starting worker thread..."); //// Loop until worker thread activates. //while (!workerThread.IsAlive) ; //Thread.Sleep(5); //test.resume(); //Thread.Sleep(30000); } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }