/// <summary> /// Initializes the excel bot. /// </summary> public static void Initialize() { Debug.Listeners.Add(new ConsoleTraceListener()); string currentDirectory = Path.GetDirectoryName(Path.GetFullPath(Assembly.GetExecutingAssembly().Location)); string inputPath = Path.Combine(currentDirectory, "BotConfig.xlsx"); InitializationHelper.BotConfigFilePath = Path.Combine(currentDirectory, "BotConfig.xml"); BotInfo botInfo = InteropHelper.LoadExcelSpreadsheet(inputPath, InitializationHelper.BotConfigFilePath); Logger.Log("Spreadhseet loaded, xml generated at " + InitializationHelper.BotConfigFilePath); UCBotHost ucBotHost = new UCBotHost(botInfo.ApplicationUserAgent, botInfo.ApplicationUserAgent, "Sorry, I was not able to understand you.".EncloseRtf()); ucBotHost.Run(); ucBotHost.ErrorOccurred += new BuildABot.Core.ErrorEventHandler(ucBotHost_ErrorOccurred); Logger.Log("UCBotHost created, now initializing..."); ucBotHost.Run(); }
/// <summary> /// Loads the excel spreadsheet. /// </summary> /// <param name="inputFilePath">The input file path.</param> /// <param name="outputFilePath">The output file path.</param> /// <returns>Bot configuration information.</returns> internal static BotInfo LoadExcelSpreadsheet(string inputFilePath, string outputFilePath) { Excel.Application xlApp = null; Excel.Workbook xlWorkBook = null; BotInfo botInfo = new BotInfo(); try { xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(inputFilePath); XDocument xDoc = new XDocument(); xDoc.Add(new XElement("bot")); foreach (Excel.Worksheet worksheet in xlWorkBook.Worksheets) { Excel.Range range = worksheet.UsedRange; if (worksheet.Name == "botInfo") { for (int rowCount = 1; rowCount <= range.Rows.Count; rowCount++) { string propertyName = (string)(range.Cells[rowCount, 1] as Excel.Range).Value2; string propertyValue = (string)(range.Cells[rowCount, 2] as Excel.Range).Value2; switch (propertyName) { case "ApplicationUrn": botInfo.ApplicationUrn = propertyValue; break; case "ApplicationUserAgent": botInfo.ApplicationUserAgent = propertyValue; break; } } } else { XElement qaRootNode; if (worksheet.Name == "static") { qaRootNode = new XElement("parameterlessQAs"); PopulateQARootNode(range, qaRootNode, "qa", "question", "answer"); } else { qaRootNode = new XElement("parameterizedQA"); qaRootNode.Add(new XAttribute("regexPattern", worksheet.Name.Replace('<', '[').Replace('>', ']'))); PopulateQARootNode(range, qaRootNode, "match", "term", "reply"); } xDoc.Root.Add(qaRootNode); ReleaseObject(worksheet); } } xDoc.Save(outputFilePath); return(botInfo); } finally { if (xlWorkBook != null) { xlWorkBook.Close(true, null, null); } if (xlApp != null) { xlApp.Quit(); } ReleaseObject(xlWorkBook); ReleaseObject(xlApp); } }