public JArrayString ProcessMessage(RbAppMasterCache rbappmc, RbAppRouterCache rbapprc, RbHeader rbh, string rbBodyString) { // RbAppLog var appName = Path.GetFileNameWithoutExtension(this.GetType().Assembly.Location); string storageConnectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", rbappmc.StorageAccount, rbappmc.StorageKey); RbAppLog RbAppLog = new RbAppLog(); RbAppLog.Initialize(storageConnectionString, "RbAppLog", appName); // Request Message var requestBody = new ReqBody(); var jo_reqBody = (JObject)JsonConvert.DeserializeObject(rbBodyString); requestBody.visitor = (string)jo_reqBody["visitor"]; requestBody.visitor_id = (string)jo_reqBody["visitor_id"]; // Response Message JArray jArrayMessages = new JArray(); // rbh.MessageId == "finish" if (rbh.MessageId == typeFinish) { var resBody = new ResBody(); resBody.visitor = requestBody.visitor; resBody.visitor_id = requestBody.visitor_id; resBody.type = typeFinish; return(BuildMessages(jArrayMessages, rbh, resBody)); } // rbh.MessageId == "init" or "talkXxxx" requestBody.talkByMe = (string)jo_reqBody["talkByMe"]; // AppMaster JObject jo_appInfo = (JObject)JsonConvert.DeserializeObject(rbappmc.AppInfo); sqlConnString = (string)jo_appInfo["SqlConnString"]; try { rinnaApiEndpoint = (string)jo_appInfo["RinnaApiEndpoint"]; rinnaKey = (string)jo_appInfo["RinnaKey"]; rinnaId = (string)jo_appInfo["RinnaId"]; } catch { rinnaApiEndpoint = string.Empty; rinnaKey = string.Empty; rinnaId = string.Empty; } luisApiEndpoint = (string)jo_appInfo["LuisApiEndpoint"]; luisPgmApiEndpoint = (string)jo_appInfo["LuisPgmApiEndpoint"]; luisKey = (string)jo_appInfo["LuisKey"]; luisAppId = (string)jo_appInfo["LuisAppId"]; // Edit Response Message var responseBody = new ResBodyTalk(); responseBody.visitor = requestBody.visitor; responseBody.visitor_id = requestBody.visitor_id; responseBody.talkByAi = new List <TalkMessage>(); IntentState intentState = new IntentState(); string intent = string.Empty; // Call LUIS API if (rbh.MessageId == typeInit) { try { intentState = GetIntent(requestBody.talkByMe); } catch (Exception ex) { ResBodyWhenError resBodyWhenError = new ResBodyWhenError(); resBodyWhenError.success = "false"; resBodyWhenError.error_message = ex.Message; RbAppLog.WriteError("E001", ex.ToString()); return(BuildMessages(jArrayMessages, rbh, resBodyWhenError)); } intentState.processState = stateBegin; intent = (string)intentState.topScoringIntent["intent"]; // set Conversation State ConversationState convState = new ConversationState(sqlConnString, rbh.SourceDeviceId, rbh.AppId); string conversationStateString = JsonConvert.SerializeObject(intentState); ApiResult apiResult = convState.SetState(conversationStateString); if (!apiResult.IsSuccessStatusCode) { ResBodyWhenError resBodyWhenError = new ResBodyWhenError(); resBodyWhenError.success = "false"; resBodyWhenError.error_message = "** Error ** Conversation State Save failed !! : " + apiResult.Message; RbAppLog.WriteError("E002", apiResult.Message); return(BuildMessages(jArrayMessages, rbh, resBodyWhenError)); } } else { // get Conversation State ConversationState convState = new ConversationState(sqlConnString, rbh.SourceDeviceId, rbh.AppId); AppResult appResult = convState.GetState(); if (appResult.apiResult.IsSuccessStatusCode) { var joIntentState = (JObject)JsonConvert.DeserializeObject(appResult.conversationStateString); intentState.processState = (string)joIntentState["processState"]; intentState.query = (string)joIntentState["query"]; intentState.topScoringIntent = (JObject)joIntentState["topScoringIntent"]; try { intentState.actionList = (JArray)joIntentState["actionList"]; } catch { intentState.actionList = null; } } else { ResBodyWhenError resBodyWhenError = new ResBodyWhenError(); resBodyWhenError.success = "false"; resBodyWhenError.error_message = "** Error ** Conversation State Get failed !! : " + appResult.apiResult.Message; RbAppLog.WriteError("E003", appResult.apiResult.Message); return(BuildMessages(jArrayMessages, rbh, resBodyWhenError)); } } // Continue talk session bool boolFinishTalk = false; if (rbh.MessageId.Substring(0, 4) == "talk") { intent = rbh.MessageId; // Check intent of finishing talk boolFinishTalk = CheckIntentOfFinishTalk(requestBody.talkByMe); } // Finish Talk if (boolFinishTalk) { responseBody.visitor = requestBody.visitor; responseBody.visitor_id = requestBody.visitor_id; responseBody.type = typeFinishTalk; var talkMessage = new TalkMessage(); talkMessage.SayText = "会話を終了します。再度、ご用件をお話しください。"; responseBody.talkByAi.Add(talkMessage); return(BuildMessages(jArrayMessages, rbh, responseBody)); } // Call Rinna API if (intent == intentTalkRinna || intent == intentNone) { if (rinnaApiEndpoint != string.Empty) { ApiResult apiResult = TalkRinna(rinnaKey, rinnaId, requestBody.talkByMe); if (apiResult.IsSuccessStatusCode) { responseBody.success = "true"; responseBody.type = typeTalkRinna; JObject jo_result = (JObject)JsonConvert.DeserializeObject(apiResult.Result); var ja_responses = (JArray)jo_result["Responses"]; foreach (var jo_content in ja_responses) { var talkMessage = new TalkMessage(); string text = (string)jo_content["Content"]["Text"]; // Replace Emoji talkMessage.SayText = ReplaceTalkMessage(text); responseBody.talkByAi.Add(talkMessage); } } else { ResBodyWhenError resBodyWhenError = new ResBodyWhenError(); resBodyWhenError.success = "false"; resBodyWhenError.error_message = "** Error ** Rinna API failed !! : " + apiResult.Message; RbAppLog.WriteError("E004", apiResult.Message); return(BuildMessages(jArrayMessages, rbh, resBodyWhenError)); } } else { responseBody.success = "true"; responseBody.type = typeFinishTalk; var talkMessage = new TalkMessage(); talkMessage.SayText = ReplaceTalkMessage("上手く聞き取ることが出来ませんでした。再度、ご用件をお話しください。"); responseBody.talkByAi.Add(talkMessage); } } // Various Talk Service defined by LUIS web service else if (intent.Length >= 4 && intent.Substring(0, 4) == "talk") { var talkMessage = new TalkMessage(); string sayText = string.Empty; if (intentState.processState == stateBegin) { sayText = GetNextTalkMessage(ref intentState); } else { sayText = GetNextTalkMessageWithUpdate(ref intentState, requestBody.talkByMe); } if (sayText == string.Empty) { responseBody.type = typeFinishTalk; sayText = CompleteTalkMessage(ref intentState); } else { responseBody.type = intent; } responseBody.success = "true"; talkMessage.SayText = sayText; responseBody.talkByAi.Add(talkMessage); // set Conversation State ConversationState convState = new ConversationState(sqlConnString, rbh.SourceDeviceId, rbh.AppId); string conversationStateString = JsonConvert.SerializeObject(intentState); ApiResult apiResult = convState.SetState(conversationStateString); if (!apiResult.IsSuccessStatusCode) { ResBodyWhenError resBodyWhenError = new ResBodyWhenError(); resBodyWhenError.success = "false"; resBodyWhenError.error_message = "** Error ** Conversation State Save(2) failed !! : " + apiResult.Message; RbAppLog.WriteError("E005", apiResult.Message); return(BuildMessages(jArrayMessages, rbh, resBodyWhenError)); } } else { responseBody.success = "true"; responseBody.type = intent; } return(BuildMessages(jArrayMessages, rbh, responseBody)); }