├── .gitignore ├── BotExamples ├── App.config ├── BotExample.ps1 ├── BotExamples.csproj ├── ExampleRules.xml ├── LyncExample.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── SpeechExample.cs └── packages.config ├── BotTests ├── BotTests.csproj ├── LinkedListTest.cs └── Properties │ └── AssemblyInfo.cs ├── ChatBot.jpg ├── ChatBot.sln ├── ChatBot ├── App.config ├── BotResponse.cs ├── ChatBot.cs ├── ChatBot.csproj ├── ChatBot.nuspec ├── ChatBotXmlSchema.xsd ├── ChatSessions │ ├── ConsoleChatSession.cs │ ├── ConsoleSpeechChatSession.cs │ ├── IChatSessionInterface.cs │ ├── LyncChatSession.cs │ ├── SpeechConversation.cs │ └── TimeOutConsoleChatSession.cs ├── DescComparer.cs ├── Generators │ ├── BotRuleCodeCompiler.cs │ └── ChatBotRuleGenerator.cs ├── LinkedList.cs ├── MethodExtensions │ └── ChatBotXmlNodeExtensions.cs ├── Properties │ └── AssemblyInfo.cs ├── Rules │ ├── BotRule.cs │ ├── BotRuleContainer.cs │ ├── ConditionBotRule.cs │ ├── PowershellBotRule.cs │ ├── RandomAnswersBotRule.cs │ └── ReplacementBotRule.cs ├── SessionStorage.cs ├── Visualizers │ ├── ConsoleBotRuleVisualizer.cs │ └── IBotRuleVisualizerInterface.cs └── packages.config ├── LICENSE ├── README.md └── nuget.ps1 /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studo 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | *_i.c 42 | *_p.c 43 | *_i.h 44 | *.ilk 45 | *.meta 46 | *.obj 47 | *.pch 48 | *.pdb 49 | *.pgc 50 | *.pgd 51 | *.rsp 52 | *.sbr 53 | *.tlb 54 | *.tli 55 | *.tlh 56 | *.tmp 57 | *.tmp_proj 58 | *.log 59 | *.vspscc 60 | *.vssscc 61 | .builds 62 | *.pidb 63 | *.svclog 64 | *.scc 65 | 66 | # Chutzpah Test files 67 | _Chutzpah* 68 | 69 | # Visual C++ cache files 70 | ipch/ 71 | *.aps 72 | *.ncb 73 | *.opensdf 74 | *.sdf 75 | *.cachefile 76 | 77 | # Visual Studio profiler 78 | *.psess 79 | *.vsp 80 | *.vspx 81 | 82 | # TFS 2012 Local Workspace 83 | $tf/ 84 | 85 | # Guidance Automation Toolkit 86 | *.gpState 87 | 88 | # ReSharper is a .NET coding add-in 89 | _ReSharper*/ 90 | *.[Rr]e[Ss]harper 91 | *.DotSettings.user 92 | 93 | # JustCode is a .NET coding addin-in 94 | .JustCode 95 | 96 | # TeamCity is a build add-in 97 | _TeamCity* 98 | 99 | # DotCover is a Code Coverage Tool 100 | *.dotCover 101 | 102 | # NCrunch 103 | _NCrunch_* 104 | .*crunch*.local.xml 105 | 106 | # MightyMoose 107 | *.mm.* 108 | AutoTest.Net/ 109 | 110 | # Web workbench (sass) 111 | .sass-cache/ 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.[Pp]ublish.xml 131 | *.azurePubxml 132 | # TODO: Comment the next line if you want to checkin your web deploy settings 133 | # but database connection strings (with potential passwords) will be unencrypted 134 | *.pubxml 135 | *.publishproj 136 | 137 | # NuGet Packages 138 | *.nupkg 139 | # The packages folder can be ignored because of Package Restore 140 | **/packages/* 141 | # except build/, which is used as an MSBuild target. 142 | !**/packages/build/ 143 | # Uncomment if necessary however generally it will be regenerated when needed 144 | #!**/packages/repositories.config 145 | 146 | # Windows Azure Build Output 147 | csx/ 148 | *.build.csdef 149 | 150 | # Windows Store app package directory 151 | AppPackages/ 152 | 153 | # Others 154 | *.[Cc]ache 155 | ClientBin/ 156 | [Ss]tyle[Cc]op.* 157 | ~$* 158 | *~ 159 | *.dbmdl 160 | *.dbproj.schemaview 161 | *.pfx 162 | *.publishsettings 163 | node_modules/ 164 | bower_components/ 165 | 166 | # RIA/Silverlight projects 167 | Generated_Code/ 168 | 169 | # Backup & report files from converting an old project file 170 | # to a newer Visual Studio version. Backup files are not needed, 171 | # because we have git ;-) 172 | _UpgradeReport_Files/ 173 | Backup*/ 174 | UpgradeLog*.XML 175 | UpgradeLog*.htm 176 | 177 | # SQL Server files 178 | *.mdf 179 | *.ldf 180 | 181 | # Business Intelligence projects 182 | *.rdl.data 183 | *.bim.layout 184 | *.bim_*.settings 185 | 186 | # Microsoft Fakes 187 | FakesAssemblies/ 188 | 189 | # Node.js Tools for Visual Studio 190 | .ntvs_analysis.dat 191 | 192 | # Visual Studio 6 build log 193 | *.plg 194 | 195 | # Visual Studio 6 workspace options file 196 | *.opt 197 | -------------------------------------------------------------------------------- /BotExamples/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /BotExamples/BotExample.ps1: -------------------------------------------------------------------------------- 1 | 2 | cd $PSScriptRoot 3 | 4 | try 5 | { 6 | <#$refs = @( "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Microsoft.CSharp.dll", 7 | "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.dll", 8 | "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll", 9 | "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.dll", 10 | "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Data.DataSetExtensions.dll", 11 | "C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll", 12 | "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Speech.dll", 13 | "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.dll", 14 | "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Xml.Linq.dll" ) 15 | Add-Type -Path "bin\ReleaseWithoutLync\ChatBot.dll" -ReferencedAssemblies $refs #> 16 | 17 | Add-Type -Path ".\bin\ReleaseWithoutLync\ChatBot.dll" 18 | } 19 | catch 20 | { 21 | $_.LoaderExceptions | % 22 | { 23 | Write-Error $_.Message 24 | } 25 | } 26 | 27 | 28 | 29 | 30 | $ruleGenerator = New-Object -TypeName QXS.ChatBot.ChatBotRuleGenerator 31 | 32 | <# 33 | # PARSE FROM STRING: 34 | $xmlstring = "" 35 | Get-Content ".\ExampleRules.xml" | foreach { $xmlstring += "$_`n" } 36 | $chatbot = New-Object QXS.ChatBot.ChatBot $ruleGenerator.Parse($xmlstring) 37 | #> 38 | 39 | # PARSE FROM FILE: 40 | $chatbot = New-Object QXS.ChatBot.ChatBot $ruleGenerator.ParseFromFile($PSScriptRoot + "\ExampleRules.xml") 41 | 42 | # Run the ChatBot in a console chat session 43 | $consoleChatSession = New-Object QXS.ChatBot.ConsoleChatSession 44 | $chatbot.talkWith($consoleChatSession) -------------------------------------------------------------------------------- /BotExamples/BotExamples.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722} 8 | Exe 9 | Properties 10 | QXS.ChatBot.Examples 11 | ChatBotExamples 12 | v4.5 13 | 512 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | TRACE;DEBUG;WITHLYNC 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE;WITHLYNC 31 | prompt 32 | 4 33 | 34 | 35 | true 36 | bin\DebugWithoutLync\ 37 | TRACE;DEBUG 38 | full 39 | AnyCPU 40 | prompt 41 | MinimumRecommendedRules.ruleset 42 | true 43 | 44 | 45 | bin\ReleaseWithoutLync\ 46 | TRACE 47 | true 48 | pdbonly 49 | AnyCPU 50 | prompt 51 | MinimumRecommendedRules.ruleset 52 | true 53 | 54 | 55 | 56 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Lync.Controls.dll 57 | 58 | 59 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Design\Microsoft.Lync.Controls.Design.dll 60 | 61 | 62 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Lync.Controls.Framework.dll 63 | 64 | 65 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Design\Microsoft.Lync.Controls.VisualStudio.Design.dll 66 | 67 | 68 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Lync.Model.dll 69 | 70 | 71 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Lync.Utilities.dll 72 | 73 | 74 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Office.Uc.dll 75 | True 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | {9ce1c326-a14c-4cac-864a-9465b904c3e2} 99 | ChatBot 100 | 101 | 102 | 103 | 104 | Designer 105 | 106 | 107 | 108 | 115 | -------------------------------------------------------------------------------- /BotExamples/ExampleRules.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 45 7 | 8 | i repeat your sentence: $s$sentence$ 9 | $s$BotName$ repeats your sentence: $s$sentence$ 10 | 11 | 12 | 13 | 14 | .*)]]> 15 | 40 16 | 17 | i repeat your sentence: $r$sentence$ 18 | $s$BotName$ repeats your sentence: $r$sentence$ 19 | 20 | 21 | $r$sentence$ 22 | 23 | 24 | 25 | 26 | \S+)]]> 27 | 40 28 | 35 | 36 | 37 | 38 | 39 | 30 40 | 51 | 52 | 53 | 54 | 55 | 20 56 | 57 | i feel ok 58 | i am a bit bored 59 | 60 | 61 | 62 | 63 | .*)]]> 64 | 20 65 | 66 | your name is now $r$username$ 67 | you are now $r$username$ 68 | pleased to meet you $r$username$ 69 | 70 | 71 | $r$username$ 72 | 73 | 74 | 75 | 76 | 77 | 20 78 | 79 | i don't know your name 80 | who are you? 81 | 82 | 83 | 84 | 85 | .*)]]> 86 | 20 87 | 88 | my name is now $r$botname$ 89 | i am now $r$botname$ 90 | 91 | 92 | $r$botname$ 93 | 94 | 95 | 96 | 97 | 98 | 20 99 | 100 | i do not have a name 101 | 102 | 103 | 104 | 105 | 106 | 60 107 | 108 | 109 | 110 | 111 | 112 | 113 | 20 114 | 115 | My name is $s$botname$ 116 | I am $s$botname$ 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 60 125 | 126 | 127 | 128 | 129 | 130 | 131 | 20 132 | 133 | Your name is $s$username$ 134 | You are $s$username$ 135 | 136 | 137 | 138 | 139 | 140 | 141 | -------------------------------------------------------------------------------- /BotExamples/LyncExample.cs: -------------------------------------------------------------------------------- 1 | #if WITHLYNC 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Microsoft.Lync.Model; 9 | using Microsoft.Lync.Model.Conversation; 10 | 11 | 12 | namespace QXS.ChatBot.Examples 13 | { 14 | 15 | public class LyncExample 16 | { 17 | static LyncClient client; 18 | static Conversation _conversation; 19 | static LyncConversation _LyncConversation; 20 | 21 | public static void LyncChat(List rules) 22 | { 23 | client = LyncClient.GetClient(); 24 | 25 | client.ConversationManager.ConversationAdded += ConversationManager_ConversationAdded; 26 | 27 | client.ConversationManager.ConversationRemoved += ConversationManager_ConversationRemoved; 28 | 29 | while (_conversation == null) 30 | { 31 | Thread.Sleep(1000); 32 | } 33 | 34 | ChatBot cb = new ChatBot(rules); 35 | cb.TalkWith(_LyncConversation); 36 | Console.ReadKey(); 37 | } 38 | 39 | 40 | static void ConversationManager_ConversationRemoved(object sender, ConversationManagerEventArgs e) 41 | { 42 | Conversation conversation = e.Conversation; 43 | 44 | if (conversation != null) 45 | { 46 | Console.WriteLine("Closed conversation details: "); 47 | if (conversation.Participants != null) 48 | { 49 | foreach (Participant p in conversation.Participants) 50 | { 51 | Console.WriteLine(" P:" + p.Contact.Uri); 52 | } 53 | 54 | } 55 | 56 | if (_conversation == conversation) 57 | { 58 | _conversation = null; 59 | _LyncConversation = null; 60 | } 61 | } 62 | } 63 | 64 | static void ConversationManager_ConversationAdded(object sender, ConversationManagerEventArgs e) 65 | { 66 | 67 | Conversation conversation = e.Conversation; 68 | 69 | var details = "Hello Lync 2013 SDK!" + Environment.NewLine; 70 | 71 | 72 | 73 | if (conversation != null) 74 | { 75 | if (e.Conversation.Modalities[ModalityTypes.InstantMessage].State == ModalityState.Notified) 76 | { 77 | 78 | 79 | if (conversation.Properties.ContainsKey(ConversationProperty.Inviter)) 80 | { 81 | 82 | var contact = (Contact)conversation.Properties[ConversationProperty.Inviter]; 83 | 84 | if (contact != null) 85 | { 86 | 87 | details += " INVITE FROM: " + contact.Uri + Environment.NewLine; 88 | 89 | } 90 | 91 | } 92 | 93 | foreach (Participant p in conversation.Participants) 94 | { 95 | details += " Participiant:" + p.Contact.Uri + Environment.NewLine; 96 | } 97 | 98 | if (_conversation == null) 99 | { 100 | _conversation = conversation; 101 | _LyncConversation = new LyncConversation(conversation); 102 | foreach (Participant p in conversation.Participants) 103 | { 104 | if (client.Self.Contact.Uri.ToLower() != p.Contact.Uri.ToLower()) 105 | { 106 | ((InstantMessageModality)p.Modalities[ModalityTypes.InstantMessage]).InstantMessageReceived += _LyncConversation.InstantMessageReceived; 107 | } 108 | } 109 | } 110 | } 111 | } 112 | 113 | Console.WriteLine("Incomming conversation details: " + details); 114 | 115 | 116 | } 117 | 118 | 119 | 120 | } 121 | 122 | } 123 | #endif -------------------------------------------------------------------------------- /BotExamples/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using QXS.ChatBot; 7 | using System.Text.RegularExpressions; 8 | using System.IO; 9 | using System.Xml; 10 | 11 | namespace QXS.ChatBot.Examples 12 | { 13 | class Program 14 | { 15 | public static List CreateBotRulesFromXml(string xmlfile) 16 | { 17 | List rules = (new ChatBotRuleGenerator()).ParseFromFile(xmlfile); 18 | 19 | // append debug rule 20 | rules.Add(GenerateVarDumpRule()); 21 | 22 | return rules; 23 | } 24 | 25 | /// 26 | /// Generate a rule that will show the values in the chatboot SessionStorage and also display the chatboot messages history 27 | /// 28 | /// 29 | public static BotRule GenerateVarDumpRule() { 30 | return new BotRule( 31 | Name: "var_dump", 32 | Weight: 200, 33 | MessagePattern: new Regex("^var_?dump$", RegexOptions.IgnoreCase), 34 | Process: delegate(Match match, IChatSessionInterface session) 35 | { 36 | string answer = "Variables: \n"; 37 | foreach (string key in session.SessionStorage.Values.Keys) 38 | { 39 | answer += " " + key + " = " + session.SessionStorage.Values[key] + "\n"; 40 | } 41 | answer += "---\n"; 42 | answer += "History: \n"; 43 | int i = 0; 44 | foreach (BotResponse response in session.GetResponseHistory()) 45 | { 46 | answer += " " + (++i) + ". " + response.RuleName + "\n"; 47 | answer += " " + response.Question.Replace("\n", "\n ") + "\n"; 48 | answer += " " + response.Answer.Split('\n')[0] + "\n"; 49 | } 50 | return answer; 51 | } 52 | ); 53 | } 54 | 55 | public static List CreateBotRulesFromCs() 56 | { 57 | return new List() 58 | { 59 | new PowershellBotRule("pstest", 10, new Regex("powershell"), @" ( ""Hi from PowerShell "" + $PSVersionTable.PSVersion) "), 60 | // debug rule 61 | GenerateVarDumpRule(), 62 | // chatbot specific behaviour 63 | new ConditionBotRule( 64 | "conditionBot", 65 | 50, 66 | new Tuple[] { 67 | new Tuple("BotName", ConditionBotRule.Operator.EqualIgnoreCase, "chatbot") 68 | }, 69 | new BotRule[] { 70 | // chatbot just knows positive feelings... 71 | new RandomAnswersBotRule("getfeeling2", 40, new Regex("how (are you|do you feel)", RegexOptions.IgnoreCase), new string[] {"i feel super", "i feel perfect", "i feel happy"}), 72 | } 73 | ), 74 | 75 | // repet the last known sentence 76 | new ReplacementBotRule("repeatLast", 41, new Regex("(please )?repeat the last sentence", RegexOptions.IgnoreCase), new string[] { "i repeat your last sentence:$s$sentence$", "$s$BotName$ repeats your last sentence:$s$sentence$"}), 77 | // repet a sentence 78 | new ReplacementBotRule("repeat", 40, new Regex("(please )?repeat(? .*)", RegexOptions.IgnoreCase), new string[] { "i repeat your sentence:$r$sentence$", "$s$BotName$ repeats your sentence:$r$sentence$"}, new Dictionary() { {"sentence", "$r$sentence$"} }), 79 | // reports your feelings 80 | new RandomAnswersBotRule("getfeeling", 40, new Regex("how (are you|do you feel)", RegexOptions.IgnoreCase), new string[] {"i feel great", "i feel tired", "i feel bored"}), 81 | 82 | // set the name of the bot 83 | new BotRule( 84 | Name: "setbotname", 85 | Weight: 10, 86 | MessagePattern: new Regex("(your name is|you are) (now )?(.*)", RegexOptions.IgnoreCase), 87 | Process: delegate(Match match, IChatSessionInterface session) { 88 | session.SessionStorage.Values["BotName"] = match.Groups[3].Value; 89 | return "My name is now " + session.SessionStorage.Values["BotName"]; 90 | } 91 | ), 92 | 93 | // show the bot's name 94 | new BotRule( 95 | Name: "getbotname", 96 | Weight: 10, 97 | MessagePattern: new Regex("(who are you|(what is|say) your name)", RegexOptions.IgnoreCase), 98 | Process: delegate(Match match, IChatSessionInterface session) { 99 | if (!session.SessionStorage.Values.ContainsKey("BotName")) 100 | { 101 | return "I do not have a name"; 102 | } 103 | if (match.Value.ToLower() == "who are you") 104 | { 105 | return "i am " + session.SessionStorage.Values["BotName"]; 106 | } 107 | return "My name is " + session.SessionStorage.Values["BotName"]; 108 | } 109 | ), 110 | 111 | // set the name of the user 112 | new BotRule( 113 | Name: "setusername", 114 | Weight: 10, 115 | MessagePattern: new Regex("my name is (now )?(.*)", RegexOptions.IgnoreCase), 116 | Process: delegate(Match match, IChatSessionInterface session) { 117 | session.SessionStorage.Values["UserName"] = match.Groups[2].Value; 118 | return "Hi " + session.SessionStorage.Values["UserName"]; 119 | } 120 | ), 121 | 122 | // show the user's name 123 | new BotRule( 124 | Name: "getusername", 125 | Weight: 20, 126 | MessagePattern: new Regex("(what is|say) my name", RegexOptions.IgnoreCase), 127 | Process: delegate(Match match, IChatSessionInterface session) { 128 | if (!session.SessionStorage.Values.ContainsKey("UserName")) 129 | { 130 | return "Sorry, but you have not told my your name"; 131 | } 132 | return "Your name is " + session.SessionStorage.Values["UserName"]; 133 | } 134 | ), 135 | 136 | // greet 137 | new BotRule( 138 | Name: "greet", 139 | Weight: 2, 140 | MessagePattern: new Regex("(hi|hello)", RegexOptions.IgnoreCase), 141 | Process: delegate(Match match, IChatSessionInterface session) { 142 | string answer = "Hi"; 143 | 144 | if (session.SessionStorage.Values.ContainsKey("UserName")) 145 | { 146 | answer += " " + session.SessionStorage.Values["UserName"]; 147 | } 148 | answer += "!"; 149 | if (session.SessionStorage.Values.ContainsKey("BotName")) 150 | { 151 | answer += " I'm " + session.SessionStorage.Values["BotName"]; 152 | } 153 | return answer; 154 | } 155 | ), 156 | 157 | // default 158 | new BotRule( 159 | Name: "default", 160 | Weight: 1, 161 | MessagePattern: new Regex(".*", RegexOptions.IgnoreCase), 162 | Process: delegate(Match match, IChatSessionInterface session) { 163 | string answer = "well, i have to think about that"; 164 | 165 | if (session.SessionStorage.Values.ContainsKey("UserName")) 166 | { 167 | answer += ", " + session.SessionStorage.Values["UserName"]; 168 | } 169 | 170 | return answer; 171 | } 172 | ) 173 | }; 174 | } 175 | 176 | static void Main(string[] args) 177 | { 178 | List rules; 179 | string xmlfile; 180 | Console.WriteLine("Press X for xml ruleset demo or C for C# ruleset"); 181 | switch (Console.ReadKey().Key) 182 | { 183 | case ConsoleKey.X: 184 | xmlfile = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location))) + @"\ExampleRules.xml"; 185 | rules = CreateBotRulesFromXml(xmlfile); 186 | break; 187 | default: 188 | xmlfile = "C#"; 189 | rules = CreateBotRulesFromCs(); 190 | break; 191 | } 192 | 193 | Console.WriteLine(Environment.NewLine + "Created ruleset from: " + xmlfile); 194 | 195 | // Visualize the rules 196 | (new ConsoleBotRuleVisualizer()).Visualize(rules); 197 | 198 | Console.WriteLine(Environment.NewLine + "Press C for console demo, S for speech demo or L for lync demo or X for consolespeech"); 199 | switch (Console.ReadKey().Key) 200 | { 201 | case ConsoleKey.X: 202 | Console.WriteLine(Environment.NewLine + "Console Chat - Please use your keyboard and write something"); 203 | (new ChatBot(rules)).TalkWith(new ConsoleSpeechChatSession()); ; 204 | break; 205 | case ConsoleKey.C: 206 | Console.WriteLine(Environment.NewLine + "Console Chat - Please use your keyboard and write something"); 207 | (new ChatBot(rules)).TalkWith(new ConsoleChatSession());; 208 | break; 209 | case ConsoleKey.L: 210 | #if WITHLYNC 211 | Console.WriteLine(Environment.NewLine + "Lync Chat - Please use Lync and write a chat message to the client, where this application is running"); 212 | LyncExample.LyncChat(rules); 213 | #else 214 | Console.WriteLine(Environment.NewLine + "This version does not support Lync"); 215 | #endif 216 | break; 217 | case ConsoleKey.S: 218 | Console.WriteLine(Environment.NewLine + "Speek Chat - Please use your mic and say something"); 219 | SpeechExample.SpeechChat(rules); 220 | break; 221 | default: 222 | Console.WriteLine(Environment.NewLine + "Invalid selection! Bye..."); 223 | break; 224 | } 225 | } 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /BotExamples/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("BotExamples")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("BotExamples")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("8a381ba1-fd9b-487b-8d37-d01562695e42")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /BotExamples/SpeechExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Speech.Synthesis; 7 | using System.Speech.Recognition; 8 | 9 | namespace QXS.ChatBot.Examples 10 | { 11 | class SpeechExample 12 | { 13 | static SpeechConversation _SpeechConversation; 14 | 15 | public static void SpeechChat(List rules) 16 | { 17 | using(SpeechRecognitionEngine speechRecognition = new SpeechRecognitionEngine( 18 | new System.Globalization.CultureInfo("en-US") 19 | //new System.Globalization.CultureInfo("fr-FR") 20 | )) 21 | { 22 | // Create a default dictation grammar. 23 | DictationGrammar defaultDictationGrammar = new DictationGrammar() 24 | { 25 | Name = "default dictation", 26 | Enabled = true 27 | 28 | }; 29 | speechRecognition.LoadGrammar(defaultDictationGrammar); 30 | // Create the spelling dictation grammar. 31 | DictationGrammar spellingDictationGrammar = new DictationGrammar("grammar:dictation#spelling") 32 | { 33 | Name = "spelling dictation", 34 | Enabled = true 35 | }; 36 | speechRecognition.LoadGrammar(spellingDictationGrammar); 37 | 38 | // Add Grammar for the demo, to make it more reliable: 39 | // https://msdn.microsoft.com/en-us/library/hh362944(v=office.14).aspx 40 | // http://dailydotnettips.com/2014/01/18/using-wildcard-with-grammar-builder-kinect-speech-recognition/ 41 | 42 | 43 | // Configure input to the speech recognizer. 44 | speechRecognition.SetInputToDefaultAudioDevice(); 45 | 46 | 47 | using (_SpeechConversation = new SpeechConversation(speechRecognition: speechRecognition)) 48 | { 49 | ChatBot cb = new ChatBot(rules); 50 | cb.TalkWith(_SpeechConversation); 51 | Console.ReadKey(); 52 | } 53 | } 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /BotExamples/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /BotTests/BotTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Debug 5 | AnyCPU 6 | {00FC8F33-137A-448E-B260-EF44EDF5E180} 7 | Library 8 | Properties 9 | QXS.ChatBot.Tests 10 | ChatBotTests 11 | v4.5 12 | 512 13 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 14 | 10.0 15 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 16 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 17 | False 18 | UnitTest 19 | 20 | 21 | true 22 | full 23 | false 24 | bin\Debug\ 25 | TRACE;DEBUG;WITHLYNC 26 | prompt 27 | 4 28 | 29 | 30 | pdbonly 31 | true 32 | bin\Release\ 33 | TRACE;WITHLYNC 34 | prompt 35 | 4 36 | 37 | 38 | true 39 | bin\DebugWithoutLync\ 40 | TRACE;DEBUG 41 | full 42 | AnyCPU 43 | prompt 44 | MinimumRecommendedRules.ruleset 45 | 46 | 47 | bin\ReleaseWithoutLync\ 48 | TRACE 49 | true 50 | pdbonly 51 | AnyCPU 52 | prompt 53 | MinimumRecommendedRules.ruleset 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | {9ce1c326-a14c-4cac-864a-9465b904c3e2} 77 | ChatBot 78 | 79 | 80 | 81 | 82 | 83 | 84 | False 85 | 86 | 87 | False 88 | 89 | 90 | False 91 | 92 | 93 | False 94 | 95 | 96 | 97 | 98 | 99 | 100 | 107 | -------------------------------------------------------------------------------- /BotTests/LinkedListTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | using QXS.ChatBot; 4 | 5 | namespace QXS.ChatBot.Tests 6 | { 7 | [TestClass] 8 | public class LinkedListTest 9 | { 10 | 11 | [TestMethod] 12 | public void TestCount() 13 | { 14 | LinkedList liste = new LinkedList(); 15 | Assert.AreEqual(0, liste.Count, "Count Method returns a wrong result."); 16 | 17 | for (int i = 1; i <= 3; i++) 18 | { 19 | liste.Push(i); 20 | Assert.AreEqual(i, liste.Count, "Count Method returns a wrong result."); 21 | } 22 | 23 | } 24 | 25 | [TestMethod] 26 | public void TestAdd() 27 | { 28 | LinkedList liste = new LinkedList(); 29 | liste.Add(1); 30 | liste.Add(2); 31 | liste.Add(3); 32 | 33 | int ii = 1; 34 | foreach (int i in liste) 35 | { 36 | Assert.AreEqual(ii++, i, "Add Method inserted a wrong result."); 37 | } 38 | Assert.AreEqual(1, liste.First, "First Value is incorrect."); 39 | Assert.AreEqual(3, liste.Last, "Last Value is incorrect."); 40 | } 41 | 42 | [TestMethod] 43 | public void TestEnqueue() 44 | { 45 | LinkedList liste = new LinkedList(); 46 | liste.Enqueue(1); 47 | liste.Enqueue(2); 48 | liste.Enqueue(3); 49 | 50 | int ii = 3; 51 | foreach (int i in liste) 52 | { 53 | Assert.AreEqual(ii--, i, "Enqueue Method inserted a wrong result."); 54 | } 55 | Assert.AreEqual(3, liste.First, "First Value is incorrect."); 56 | Assert.AreEqual(1, liste.Last, "Last Value is incorrect."); 57 | } 58 | 59 | [TestMethod] 60 | public void TestPush() 61 | { 62 | LinkedList liste = new LinkedList(); 63 | liste.Push(1); 64 | liste.Push(2); 65 | liste.Push(3); 66 | 67 | int ii = 3; 68 | foreach (int i in liste) 69 | { 70 | Assert.AreEqual(ii--, i, "Push Method inserted a wrong result."); 71 | } 72 | Assert.AreEqual(3, liste.First, "First Value is incorrect."); 73 | Assert.AreEqual(1, liste.Last, "Last Value is incorrect."); 74 | } 75 | 76 | [TestMethod] 77 | public void TestPop() 78 | { 79 | LinkedList liste = new LinkedList(); 80 | liste.Push(1); 81 | liste.Push(2); 82 | liste.Push(3); 83 | 84 | for (int i = 3; i >= 1; i--) 85 | { 86 | Assert.AreEqual(i, liste.Pop(), "Pop Method returns a wrong result."); 87 | } 88 | Assert.AreEqual(0, liste.Count, "Count returns a wrong result."); 89 | Assert.AreEqual(0, liste.First, "First is not 0."); 90 | Assert.AreEqual(0, liste.Last, "Last is not 0."); 91 | 92 | LinkedList liste2 = new LinkedList(); 93 | liste2.Push("1"); 94 | liste2.Push("2"); 95 | liste2.Push("3"); 96 | 97 | for (int i = 3; i >= 1; i--) 98 | { 99 | Assert.AreEqual(i.ToString(), liste2.Pop(), "Pop Method returns a wrong result."); 100 | } 101 | Assert.AreEqual(0, liste2.Count, "Count returns a wrong result."); 102 | Assert.AreEqual(null, liste2.First, "First is not null."); 103 | Assert.AreEqual(null, liste2.Last, "Last is not null."); 104 | } 105 | 106 | [TestMethod] 107 | public void TestPopWithLimit() 108 | { 109 | LinkedList liste = new LinkedList(3, false); 110 | liste.Push(1); 111 | liste.Push(2); 112 | liste.Push(3); 113 | liste.Push(4); 114 | 115 | for (int i = 4; i >= 2; i--) 116 | { 117 | Assert.AreEqual(i, liste.Pop(), "Pop Method returns a wrong result."); 118 | } 119 | Assert.AreEqual(0, liste.Count, "Count returns a wrong result."); 120 | Assert.AreEqual(0, liste.First, "First is not 0."); 121 | Assert.AreEqual(0, liste.Last, "Last is not 0."); 122 | 123 | LinkedList liste2 = new LinkedList(3,false); 124 | liste2.Push("1"); 125 | liste2.Push("2"); 126 | liste2.Push("3"); 127 | liste2.Push("4"); 128 | for (int i = 4; i >= 2; i--) 129 | { 130 | Assert.AreEqual(i.ToString(), liste2.Pop(), "Pop Method returns a wrong result."); 131 | } 132 | Assert.AreEqual(0, liste2.Count, "Count returns a wrong result."); 133 | Assert.AreEqual(null, liste2.First, "First is not null."); 134 | Assert.AreEqual(null, liste2.Last, "Last is not null."); 135 | } 136 | 137 | [TestMethod] 138 | public void TestDequeue() 139 | { 140 | LinkedList liste = new LinkedList(); 141 | liste.Enqueue(1); 142 | liste.Enqueue(2); 143 | liste.Enqueue(3); 144 | 145 | for (int i = 1; i <= 3; i++) 146 | { 147 | Assert.AreEqual(i, liste.Dequeue(), "Dequeue Method returns a wrong result."); 148 | } 149 | Assert.AreEqual(0, liste.Count, "Count returns a wrong result."); 150 | Assert.AreEqual(0, liste.First, "First is not 0."); 151 | Assert.AreEqual(0, liste.Last, "Last is not 0."); 152 | } 153 | 154 | [TestMethod] 155 | public void TestDequeueWithLimit() 156 | { 157 | LinkedList liste = new LinkedList(3, false); 158 | liste.Enqueue(1); 159 | liste.Enqueue(2); 160 | liste.Enqueue(3); 161 | liste.Enqueue(4); 162 | 163 | for (int i = 2; i <= 4; i++) 164 | { 165 | Assert.AreEqual(i, liste.Dequeue(), "Dequeue Method returns a wrong result."); 166 | } 167 | Assert.AreEqual(0, liste.Count, "Count returns a wrong result."); 168 | Assert.AreEqual(0, liste.First, "First is not 0."); 169 | Assert.AreEqual(0, liste.Last, "Last is not 0."); 170 | } 171 | 172 | [TestMethod, ExpectedException(typeof(LinkedListException))] 173 | public void TestDequeueThrowsException() 174 | { 175 | LinkedList liste = new LinkedList(); 176 | liste.Enqueue(1); 177 | 178 | liste.Dequeue(); 179 | liste.Dequeue(); 180 | } 181 | 182 | [TestMethod, ExpectedException(typeof(LinkedListException))] 183 | public void TestPopThrowsException() 184 | { 185 | LinkedList liste = new LinkedList(); 186 | liste.Push(1); 187 | 188 | liste.Pop(); 189 | liste.Pop(); 190 | } 191 | 192 | [TestMethod, ExpectedException(typeof(LinkedListException))] 193 | public void TestPushWithLimitThrowsException() 194 | { 195 | LinkedList liste = new LinkedList(3); 196 | liste.Push(1); 197 | liste.Push(2); 198 | liste.Push(3); 199 | liste.Push(4); 200 | 201 | } 202 | 203 | [TestMethod, ExpectedException(typeof(LinkedListException))] 204 | public void TestEnqueueWithLimitThrowsException() 205 | { 206 | LinkedList liste = new LinkedList(3); 207 | liste.Enqueue(1); 208 | liste.Enqueue(2); 209 | liste.Enqueue(3); 210 | liste.Enqueue(4); 211 | 212 | } 213 | 214 | } 215 | } 216 | -------------------------------------------------------------------------------- /BotTests/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("BotTests")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("BotTests")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e5c7de57-a7fe-44b3-8270-398f1415ea9e")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /ChatBot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qxsch/ChatBot/9ba3c772d6c468d24caa4c1209c3cdb1aa8c88d4/ChatBot.jpg -------------------------------------------------------------------------------- /ChatBot.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.21005.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotExamples", "BotExamples\BotExamples.csproj", "{22EBF76F-812C-4C02-BB23-EAD7BA3B3722}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatBot", "ChatBot\ChatBot.csproj", "{9CE1C326-A14C-4CAC-864A-9465B904C3E2}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BotTests", "BotTests\BotTests.csproj", "{00FC8F33-137A-448E-B260-EF44EDF5E180}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | DebugWithoutLync|Any CPU = DebugWithoutLync|Any CPU 16 | Release|Any CPU = Release|Any CPU 17 | ReleaseWithoutLync|Any CPU = ReleaseWithoutLync|Any CPU 18 | EndGlobalSection 19 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 20 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722}.DebugWithoutLync|Any CPU.ActiveCfg = DebugWithoutLync|Any CPU 23 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722}.DebugWithoutLync|Any CPU.Build.0 = DebugWithoutLync|Any CPU 24 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722}.ReleaseWithoutLync|Any CPU.ActiveCfg = ReleaseWithoutLync|Any CPU 27 | {22EBF76F-812C-4C02-BB23-EAD7BA3B3722}.ReleaseWithoutLync|Any CPU.Build.0 = ReleaseWithoutLync|Any CPU 28 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2}.DebugWithoutLync|Any CPU.ActiveCfg = DebugWithoutLync|Any CPU 31 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2}.DebugWithoutLync|Any CPU.Build.0 = DebugWithoutLync|Any CPU 32 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2}.ReleaseWithoutLync|Any CPU.ActiveCfg = ReleaseWithoutLync|Any CPU 35 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2}.ReleaseWithoutLync|Any CPU.Build.0 = ReleaseWithoutLync|Any CPU 36 | {00FC8F33-137A-448E-B260-EF44EDF5E180}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {00FC8F33-137A-448E-B260-EF44EDF5E180}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {00FC8F33-137A-448E-B260-EF44EDF5E180}.DebugWithoutLync|Any CPU.ActiveCfg = DebugWithoutLync|Any CPU 39 | {00FC8F33-137A-448E-B260-EF44EDF5E180}.DebugWithoutLync|Any CPU.Build.0 = DebugWithoutLync|Any CPU 40 | {00FC8F33-137A-448E-B260-EF44EDF5E180}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {00FC8F33-137A-448E-B260-EF44EDF5E180}.Release|Any CPU.Build.0 = Release|Any CPU 42 | {00FC8F33-137A-448E-B260-EF44EDF5E180}.ReleaseWithoutLync|Any CPU.ActiveCfg = ReleaseWithoutLync|Any CPU 43 | {00FC8F33-137A-448E-B260-EF44EDF5E180}.ReleaseWithoutLync|Any CPU.Build.0 = ReleaseWithoutLync|Any CPU 44 | EndGlobalSection 45 | GlobalSection(SolutionProperties) = preSolution 46 | HideSolutionNode = FALSE 47 | EndGlobalSection 48 | EndGlobal 49 | -------------------------------------------------------------------------------- /ChatBot/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ChatBot/BotResponse.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace QXS.ChatBot 8 | { 9 | public class BotResponse 10 | { 11 | public BotResponse(string RuleName, string Question, string Answer) 12 | { 13 | this.RuleName = RuleName; 14 | this.Question = Question; 15 | this.Answer = (Answer.Length <= _MaxAnswerSize ? Answer : Answer.Substring(0, _MaxAnswerSize - 3)); 16 | } 17 | 18 | public readonly string RuleName; 19 | public readonly string Question; 20 | public readonly string Answer; 21 | 22 | 23 | protected static int _MaxAnswerSize = 4096; 24 | public static int MaxAnswerSize 25 | { 26 | get { return _MaxAnswerSize; } 27 | set 28 | { 29 | if (value < 10) 30 | { 31 | throw new ArgumentOutOfRangeException("Size must be at least 10."); 32 | } 33 | } 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /ChatBot/ChatBot.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Text.RegularExpressions; 7 | using System.Threading.Tasks; 8 | 9 | namespace QXS.ChatBot 10 | { 11 | /// 12 | /// The Chatbot 13 | /// 14 | public class ChatBot 15 | { 16 | /// 17 | /// A conversation started 18 | /// 19 | public event Action OnConverationStarted; 20 | 21 | /// 22 | /// A conversation ended 23 | /// 24 | public event Action OnConverationEnded; 25 | 26 | /// 27 | /// The chatbot received a messsage 28 | /// 29 | public event Action OnMessageReceived; 30 | 31 | /// 32 | /// The chatbot replied to a message 33 | /// 34 | public event Action OnMessageSent; 35 | 36 | /// 37 | /// Sets the Exit Condition for an ending conversation 38 | /// 39 | public Func ExitCondition; 40 | 41 | /// 42 | /// Sets the default resonse in case no appropriate Rule was found 43 | /// 44 | public Func DefaultAnswer; 45 | 46 | protected Stack _commandHistory = new Stack(); 47 | protected SortedList> _botRules = new SortedList>(new DescComparer()); 48 | 49 | /// 50 | /// Creaates the Chatbot 51 | /// 52 | /// 53 | public ChatBot(IEnumerable Rules) 54 | { 55 | Dictionary ruleNames = new Dictionary(); 56 | 57 | foreach (BotRule rule in Rules) 58 | { 59 | if (rule.Process == null) 60 | { 61 | throw new ArgumentException("Process is null.", "Rules"); 62 | } 63 | if (rule.MessagePattern == null) 64 | { 65 | throw new ArgumentException("MessagePattern is null.", "Rules"); 66 | } 67 | if (ruleNames.ContainsKey(rule.Name)) 68 | { 69 | throw new ArgumentException("Names are not unique. Duplicate key found for rule name \"" + rule.Name + "\".", "Rules"); 70 | } 71 | 72 | ruleNames[rule.Name] = true; 73 | 74 | // Add the rule to the _botRules with the same rule weight 75 | if (!this._botRules.ContainsKey(rule.Weight)) 76 | { 77 | this._botRules[rule.Weight] = new List(); 78 | } 79 | this._botRules[rule.Weight].Add(rule); 80 | } 81 | 82 | ExitCondition = this.IsGoodBye; 83 | } 84 | 85 | /// 86 | /// Find a matching rule/reponse to a question/message 87 | /// 88 | /// The session, that should be used 89 | /// The message that came in 90 | /// the response string or null in case no answer was found 91 | protected string FindAnswer(IChatSessionInterface session, string messageIn) 92 | { 93 | foreach (List rules in this._botRules.Values) 94 | { 95 | foreach (BotRule rule in rules) 96 | { 97 | Match match = rule.MessagePattern.Match(messageIn); 98 | if (match.Success) 99 | { 100 | string msg = rule.Process(match, session); 101 | if (msg != null) 102 | { 103 | session.AddResponseToHistory(new BotResponse(rule.Name, messageIn, msg)); 104 | return msg; 105 | } 106 | } 107 | } 108 | } 109 | 110 | return null; 111 | } 112 | 113 | protected void SendResponse(IChatSessionInterface session, string messageOut) 114 | { 115 | session.SendMessage(messageOut); 116 | if (OnMessageSent != null) 117 | { 118 | OnMessageSent(session, messageOut); 119 | } 120 | } 121 | 122 | /// 123 | /// Starts a conversation over a session 124 | /// 125 | /// 126 | public void TalkWith(IChatSessionInterface session) 127 | { 128 | if (session == null) 129 | { 130 | return; 131 | } 132 | 133 | if (OnConverationStarted != null) 134 | { 135 | OnConverationStarted(session); 136 | } 137 | 138 | 139 | string messageIn=""; 140 | string messageOut=""; 141 | for (messageIn = session.ReadMessage(); !this.ExitCondition(messageIn); messageIn = session.ReadMessage()) 142 | { 143 | if (OnMessageReceived != null) 144 | { 145 | OnMessageReceived(session, messageIn); 146 | } 147 | 148 | messageOut = FindAnswer(session, messageIn); 149 | if (messageOut == null) 150 | { 151 | // do we have a default answer? 152 | if (DefaultAnswer != null) 153 | { 154 | messageOut=DefaultAnswer(messageIn); 155 | } 156 | // still null? 157 | if (messageOut == null) 158 | { 159 | SendResponse(session, "What did you say?"); 160 | } 161 | else 162 | { 163 | SendResponse(session, messageOut); 164 | } 165 | 166 | } 167 | else 168 | { 169 | SendResponse(session, messageOut); 170 | } 171 | // still interactive? 172 | if (!session.IsInteractive) 173 | { 174 | break; 175 | } 176 | } 177 | 178 | if (OnConverationEnded != null) 179 | { 180 | OnConverationEnded(session); 181 | } 182 | } 183 | 184 | /// 185 | /// Sets the default Exit Condition for the conversation 186 | /// 187 | /// 188 | /// Message, that came in 189 | /// Returns true, in case the conversation should be ended 190 | public bool IsGoodBye(string message) 191 | { 192 | switch(message.ToLower()) 193 | { 194 | case "quit": return true; 195 | case "exit": return true; 196 | case "goodbye": return true; 197 | case "good bye": return true; 198 | case "bye": return true; 199 | } 200 | return false; 201 | } 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /ChatBot/ChatBot.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {9CE1C326-A14C-4CAC-864A-9465B904C3E2} 8 | Library 9 | Properties 10 | QXS.ChatBot 11 | ChatBot 12 | v4.5 13 | 512 14 | 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | TRACE;DEBUG;WITHLYNC 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE;WITHLYNC 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | true 40 | bin\DebugWithoutLync\ 41 | TRACE;DEBUG 42 | full 43 | AnyCPU 44 | prompt 45 | MinimumRecommendedRules.ruleset 46 | 47 | 48 | bin\ReleaseWithoutLync\ 49 | TRACE 50 | true 51 | pdbonly 52 | AnyCPU 53 | prompt 54 | MinimumRecommendedRules.ruleset 55 | 56 | 57 | 58 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Lync.Controls.dll 59 | 60 | 61 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Design\Microsoft.Lync.Controls.Design.dll 62 | 63 | 64 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Lync.Controls.Framework.dll 65 | 66 | 67 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Design\Microsoft.Lync.Controls.VisualStudio.Design.dll 68 | 69 | 70 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Lync.Model.dll 71 | 72 | 73 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Lync.Utilities.dll 74 | 75 | 76 | ..\packages\Lync2013SDK.15.0.4466.1000\lib\net40\Microsoft.Office.Uc.dll 77 | True 78 | 79 | 80 | 81 | 82 | False 83 | C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | Designer 121 | PreserveNewest 122 | 123 | 124 | 125 | 126 | 127 | 134 | -------------------------------------------------------------------------------- /ChatBot/ChatBot.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $id$ 5 | $version$ 6 | QXS.ChatBot 7 | QXSCH 8 | QXSCH 9 | https://github.com/qxsch/ChatBot/blob/master/LICENSE 10 | https://github.com/qxsch/ChatBot 11 | 12 | false 13 | QXSCH ChatBot 14 | Initial Release 15 | Copyright 2015 16 | Chat Lync 17 | 18 | -------------------------------------------------------------------------------- /ChatBot/ChatBotXmlSchema.xsd: -------------------------------------------------------------------------------- 1 |  2 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /ChatBot/ChatSessions/ConsoleChatSession.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.CompilerServices; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace QXS.ChatBot 9 | { 10 | public class ConsoleChatSession : IChatSessionInterface 11 | { 12 | /// 13 | /// The session received a messsage 14 | /// 15 | public event Action OnMessageReceived; 16 | 17 | /// 18 | /// The session replied to a message 19 | /// 20 | public event Action OnMessageSent; 21 | 22 | public ConsoleChatSession() 23 | { 24 | SessionStorage = new SessionStorage(); 25 | } 26 | 27 | public string ReadMessage() 28 | { 29 | Console.Write("YOU> "); 30 | string s = Console.ReadLine(); 31 | if (s != null && OnMessageReceived != null) 32 | { 33 | OnMessageReceived(this, s); 34 | } 35 | return s; 36 | } 37 | public void SendMessage(string message) 38 | { 39 | Console.ForegroundColor = ConsoleColor.Yellow; 40 | Console.Write("BOT> "); 41 | Console.WriteLine(message.Replace("\n", "\n ")); 42 | Console.ResetColor(); 43 | if (message != null && OnMessageSent != null) 44 | { 45 | OnMessageSent(this, message); 46 | } 47 | } 48 | 49 | public string AskQuestion(string message) 50 | { 51 | SendMessage(message); 52 | Console.Write("YOU> "); 53 | return ReadMessage(); 54 | } 55 | 56 | public bool IsInteractive { get { return true; } set { } } 57 | 58 | public SessionStorage SessionStorage { get; set; } 59 | 60 | 61 | public void SetResponseHistorySize(int Size) 62 | { 63 | _ResponseHistory = new LinkedList(_ResponseHistory, Size, false); 64 | } 65 | protected LinkedList _ResponseHistory = new LinkedList(10, false); 66 | public void AddResponseToHistory(BotResponse Response) 67 | { 68 | _ResponseHistory.Push(Response); 69 | } 70 | 71 | public Stack GetResponseHistory() 72 | { 73 | return new Stack(_ResponseHistory.GetAsReverseArray()); 74 | } 75 | 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /ChatBot/ChatSessions/ConsoleSpeechChatSession.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.CompilerServices; 5 | using System.Speech.Synthesis; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace QXS.ChatBot 10 | { 11 | public class ConsoleSpeechChatSession : IChatSessionInterface 12 | { 13 | protected SpeechSynthesizer _speechSynthesizer; 14 | 15 | /// 16 | /// The session received a messsage 17 | /// 18 | public event Action OnMessageReceived; 19 | 20 | /// 21 | /// The session replied to a message 22 | /// 23 | public event Action OnMessageSent; 24 | 25 | public ConsoleSpeechChatSession() 26 | { 27 | SessionStorage = new SessionStorage(); 28 | 29 | SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(); 30 | speechSynthesizer.SetOutputToDefaultAudioDevice(); 31 | _speechSynthesizer = speechSynthesizer; 32 | } 33 | 34 | public ConsoleSpeechChatSession(SpeechSynthesizer speechSynthesizer) 35 | { 36 | SessionStorage = new SessionStorage(); 37 | if (speechSynthesizer == null) 38 | { 39 | speechSynthesizer = new SpeechSynthesizer(); 40 | speechSynthesizer.SetOutputToDefaultAudioDevice(); 41 | } 42 | _speechSynthesizer = speechSynthesizer; 43 | } 44 | 45 | public string ReadMessage() 46 | { 47 | Console.Write("YOU> "); 48 | string s = Console.ReadLine(); 49 | if (s != null && OnMessageReceived != null) 50 | { 51 | OnMessageReceived(this, s); 52 | } 53 | return s; 54 | } 55 | public void SendMessage(string message) 56 | { 57 | Console.ForegroundColor = ConsoleColor.Yellow; 58 | Console.Write("BOT> "); 59 | Console.WriteLine(message.Replace("\n", "\n ")); 60 | Console.ResetColor(); 61 | _speechSynthesizer.Speak(message); 62 | if (message != null && OnMessageSent != null) 63 | { 64 | OnMessageSent(this, message); 65 | } 66 | } 67 | 68 | public string AskQuestion(string message) 69 | { 70 | SendMessage(message); 71 | Console.Write("YOU> "); 72 | return ReadMessage(); 73 | } 74 | 75 | public bool IsInteractive { get { return true; } set { } } 76 | 77 | public SessionStorage SessionStorage { get; set; } 78 | 79 | 80 | public void SetResponseHistorySize(int Size) 81 | { 82 | _ResponseHistory = new LinkedList(_ResponseHistory, Size, false); 83 | } 84 | protected LinkedList _ResponseHistory = new LinkedList(10, false); 85 | public void AddResponseToHistory(BotResponse Response) 86 | { 87 | _ResponseHistory.Push(Response); 88 | } 89 | 90 | public Stack GetResponseHistory() 91 | { 92 | return new Stack(_ResponseHistory.GetAsReverseArray()); 93 | } 94 | 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /ChatBot/ChatSessions/IChatSessionInterface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.CompilerServices; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace QXS.ChatBot 9 | { 10 | public interface IChatSessionInterface 11 | { 12 | /// 13 | /// The session received a messsage 14 | /// 15 | event Action OnMessageReceived; 16 | 17 | /// 18 | /// The session replied to a message 19 | /// 20 | event Action OnMessageSent; 21 | 22 | string ReadMessage(); 23 | void SendMessage(string message); 24 | 25 | string AskQuestion(string message); 26 | 27 | bool IsInteractive { get; set; } 28 | 29 | SessionStorage SessionStorage { get; set; } 30 | 31 | 32 | void SetResponseHistorySize(int Size); 33 | void AddResponseToHistory(BotResponse Response); 34 | Stack GetResponseHistory(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /ChatBot/ChatSessions/LyncChatSession.cs: -------------------------------------------------------------------------------- 1 | #if WITHLYNC 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Microsoft.Lync.Model; 9 | using Microsoft.Lync.Model.Conversation; 10 | 11 | namespace QXS.ChatBot 12 | { 13 | public class LyncConversation : IChatSessionInterface 14 | { 15 | 16 | protected Conversation _conversation; 17 | 18 | /// 19 | /// The session received a messsage 20 | /// 21 | public event Action OnMessageReceived; 22 | 23 | /// 24 | /// The session replied to a message 25 | /// 26 | public event Action OnMessageSent; 27 | 28 | public LyncConversation(Conversation Conversation) 29 | { 30 | _conversation = Conversation; 31 | SessionStorage = new SessionStorage(); 32 | } 33 | 34 | protected Queue _IncomingMessages = new Queue(); 35 | 36 | protected bool _MessageSent = false; 37 | 38 | public void InstantMessageReceived(object sender, MessageSentEventArgs e) 39 | { 40 | _IncomingMessages.Enqueue(e.Text); 41 | } 42 | 43 | public void SendMessage(string message) 44 | { 45 | Console.WriteLine("WRITE #" + message + "#"); 46 | _MessageSent = false; 47 | try 48 | { 49 | IDictionary textMessage = new Dictionary(); 50 | textMessage.Add(InstantMessageContentType.PlainText, message); 51 | if (((InstantMessageModality)_conversation.Modalities[ModalityTypes.InstantMessage]).CanInvoke(ModalityAction.SendInstantMessage)) 52 | { 53 | ((InstantMessageModality)_conversation.Modalities[ModalityTypes.InstantMessage]).BeginSendMessage( 54 | textMessage 55 | , SendMessageCallback 56 | , textMessage); 57 | } 58 | 59 | if (message != null && OnMessageSent != null) 60 | { 61 | OnMessageSent(this, message); 62 | } 63 | } 64 | catch (Exception e) 65 | { 66 | Console.WriteLine("Client Platform Exception: " + e.Message, "Send Message"); 67 | } 68 | } 69 | 70 | public void SendMessageCallback(IAsyncResult ar) 71 | { 72 | //((InstantMessageModality)_conversation.Modalities[ModalityTypes.InstantMessage]).BeginSetComposing(false, ComposingCallback, null); 73 | if (ar.IsCompleted == true) 74 | { 75 | _MessageSent = true; 76 | try 77 | { 78 | ((InstantMessageModality)_conversation.Modalities[ModalityTypes.InstantMessage]).EndSendMessage(ar); 79 | } 80 | catch (LyncClientException lce) 81 | { 82 | Console.WriteLine("Lync Client Exception on EndSendMessage " + lce.Message); 83 | } 84 | 85 | } 86 | } 87 | 88 | 89 | public string ReadMessage() 90 | { 91 | while (_IncomingMessages.Count <= 0) 92 | { 93 | Thread.Sleep(500); 94 | } 95 | string s = _IncomingMessages.Dequeue().Trim(); 96 | Console.WriteLine("READ #" + s + "#"); 97 | 98 | if (s != null && OnMessageReceived != null) 99 | { 100 | OnMessageReceived(this, s); 101 | } 102 | 103 | return s; 104 | } 105 | 106 | public string AskQuestion(string message) 107 | { 108 | _IncomingMessages.Clear(); 109 | SendMessage(message); 110 | return ReadMessage(); 111 | } 112 | 113 | public bool IsInteractive { get { return true; } set { } } 114 | 115 | public SessionStorage SessionStorage { get; set; } 116 | 117 | 118 | public void SetResponseHistorySize(int Size) 119 | { 120 | _ResponseHistory = new LinkedList(_ResponseHistory, Size, false); 121 | } 122 | protected LinkedList _ResponseHistory = new LinkedList(10, false); 123 | public void AddResponseToHistory(BotResponse Response) 124 | { 125 | _ResponseHistory.Push(Response); 126 | } 127 | 128 | public Stack GetResponseHistory() 129 | { 130 | return new Stack(_ResponseHistory.GetAsReverseArray()); 131 | } 132 | } 133 | 134 | } 135 | #endif -------------------------------------------------------------------------------- /ChatBot/ChatSessions/SpeechConversation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Speech.Recognition; 7 | using System.Speech.Synthesis; 8 | 9 | namespace QXS.ChatBot 10 | { 11 | public class SpeechConversation : IChatSessionInterface, IDisposable 12 | { 13 | protected SpeechSynthesizer _speechSynthesizer; 14 | protected SpeechRecognitionEngine _speechRecognition; 15 | 16 | /// 17 | /// The session received a messsage 18 | /// 19 | public event Action OnMessageReceived; 20 | 21 | /// 22 | /// The session replied to a message 23 | /// 24 | public event Action OnMessageSent; 25 | 26 | public SpeechConversation(SpeechSynthesizer speechSynthesizer = null, SpeechRecognitionEngine speechRecognition = null) 27 | { 28 | SessionStorage = new SessionStorage(); 29 | if(speechSynthesizer==null) 30 | { 31 | speechSynthesizer = new SpeechSynthesizer(); 32 | speechSynthesizer.SetOutputToDefaultAudioDevice(); 33 | } 34 | _speechSynthesizer = speechSynthesizer; 35 | if(speechRecognition==null) 36 | { 37 | speechRecognition = new SpeechRecognitionEngine( 38 | new System.Globalization.CultureInfo("en-US") 39 | ); 40 | // Create a default dictation grammar. 41 | DictationGrammar defaultDictationGrammar = new DictationGrammar(); 42 | defaultDictationGrammar.Name = "default dictation"; 43 | defaultDictationGrammar.Enabled = true; 44 | speechRecognition.LoadGrammar(defaultDictationGrammar); 45 | // Create the spelling dictation grammar. 46 | DictationGrammar spellingDictationGrammar = new DictationGrammar("grammar:dictation#spelling"); 47 | spellingDictationGrammar.Name = "spelling dictation"; 48 | spellingDictationGrammar.Enabled = true; 49 | speechRecognition.LoadGrammar(spellingDictationGrammar); 50 | 51 | // Configure input to the speech recognizer. 52 | speechRecognition.SetInputToDefaultAudioDevice(); 53 | } 54 | _speechRecognition = speechRecognition; 55 | } 56 | 57 | public void Dispose() 58 | { 59 | _speechRecognition.Dispose(); 60 | } 61 | 62 | public string ReadMessage() 63 | { 64 | RecognitionResult result = null; 65 | while( result == null) 66 | result = _speechRecognition.Recognize(new TimeSpan(0, 0, 30)); 67 | Console.WriteLine("YOU> " + result.Text); 68 | 69 | if (result.Text != null && OnMessageReceived != null) 70 | { 71 | OnMessageReceived(this, result.Text); 72 | } 73 | 74 | return result.Text; 75 | } 76 | 77 | public void SendMessage(string message) 78 | { 79 | Console.ForegroundColor = ConsoleColor.Yellow; 80 | Console.Write("BOT> "); 81 | Console.WriteLine(message.Replace("\n", "\n ")); 82 | Console.ResetColor(); 83 | _speechSynthesizer.Speak(message); 84 | if (message != null && OnMessageSent != null) 85 | { 86 | OnMessageSent(this, message); 87 | } 88 | } 89 | 90 | public string AskQuestion(string message) 91 | { 92 | SendMessage(message); 93 | Console.Write("YOU> "); 94 | return ReadMessage(); 95 | } 96 | 97 | public bool IsInteractive { get { return true; } set { } } 98 | 99 | public SessionStorage SessionStorage { get; set; } 100 | 101 | 102 | public void SetResponseHistorySize(int Size) 103 | { 104 | _ResponseHistory = new LinkedList(_ResponseHistory, Size, false); 105 | } 106 | 107 | protected LinkedList _ResponseHistory = new LinkedList(10, false); 108 | 109 | public void AddResponseToHistory(BotResponse Response) 110 | { 111 | _ResponseHistory.Push(Response); 112 | } 113 | 114 | public Stack GetResponseHistory() 115 | { 116 | return new Stack(_ResponseHistory.GetAsReverseArray()); 117 | } 118 | 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /ChatBot/ChatSessions/TimeOutConsoleChatSession.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace QXS.ChatBot.ChatSessions 8 | { 9 | class TimeOutConsoleChatSession : IChatSessionInterface 10 | { 11 | /// 12 | /// The session received a messsage 13 | /// 14 | public event Action OnMessageReceived; 15 | 16 | /// 17 | /// The session replied to a message 18 | /// 19 | public event Action OnMessageSent; 20 | 21 | public int readTimeoutMS = 6000; 22 | 23 | public TimeOutConsoleChatSession() 24 | { 25 | SessionStorage = new SessionStorage(); 26 | } 27 | 28 | public static string ReadLineWithTimeout(int timeoutms = 5000) 29 | { 30 | ReadLineDelegate d = Console.ReadLine; 31 | IAsyncResult result = d.BeginInvoke(null, null); 32 | result.AsyncWaitHandle.WaitOne(timeoutms); //timeout e.g. 15000 for 15 secs 33 | if (result.IsCompleted) 34 | { 35 | string resultstr = d.EndInvoke(result); 36 | return resultstr; 37 | } 38 | else 39 | { 40 | throw new TimeoutException("Timed Out!"); 41 | } 42 | } 43 | 44 | delegate string ReadLineDelegate(); 45 | 46 | public string ReadMessage() 47 | { 48 | Console.Write("YOU> "); 49 | string s = ReadLineWithTimeout(readTimeoutMS); 50 | if (s != null && OnMessageReceived != null) 51 | { 52 | OnMessageReceived(this, s); 53 | } 54 | return s; 55 | } 56 | public void SendMessage(string message) 57 | { 58 | Console.ForegroundColor = ConsoleColor.Yellow; 59 | Console.Write("BOT> "); 60 | Console.WriteLine(message.Replace("\n", "\n ")); 61 | Console.ResetColor(); 62 | if (message != null && OnMessageSent != null) 63 | { 64 | OnMessageSent(this, message); 65 | } 66 | } 67 | 68 | public string AskQuestion(string message) 69 | { 70 | SendMessage(message); 71 | Console.Write("YOU> "); 72 | return ReadMessage(); 73 | } 74 | 75 | public bool IsInteractive { get { return true; } set { } } 76 | 77 | public SessionStorage SessionStorage { get; set; } 78 | 79 | 80 | public void SetResponseHistorySize(int Size) 81 | { 82 | _ResponseHistory = new LinkedList(_ResponseHistory, Size, false); 83 | } 84 | 85 | protected LinkedList _ResponseHistory = new LinkedList(10, false); 86 | 87 | public void AddResponseToHistory(BotResponse Response) 88 | { 89 | _ResponseHistory.Push(Response); 90 | } 91 | 92 | public Stack GetResponseHistory() 93 | { 94 | return new Stack(_ResponseHistory.GetAsReverseArray()); 95 | } 96 | 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /ChatBot/DescComparer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace QXS.ChatBot 4 | { 5 | internal class DescComparer : IComparer 6 | { 7 | public int Compare(T x, T y) 8 | { 9 | return Comparer.Default.Compare(y, x); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /ChatBot/Generators/BotRuleCodeCompiler.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CSharp; 2 | using System; 3 | using System.CodeDom.Compiler; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Reflection; 7 | using System.Text; 8 | using System.Text.RegularExpressions; 9 | using System.Threading.Tasks; 10 | 11 | namespace QXS.ChatBot 12 | { 13 | /// 14 | /// Uses CSharpCodeProvider to compile the C# code into a dll in memory 15 | /// and get the reference to a MethodInfo for later execution. 16 | /// 17 | public class BotRuleCodeCompiler 18 | { 19 | public const string DefaultNamspace = "QXS.ChatBot.CompiledBotRuleCode"; 20 | public readonly string Code; 21 | public readonly string ClassName; 22 | 23 | protected CompilerResults results; 24 | protected MethodInfo method; 25 | 26 | public BotRuleCodeCompiler(string code, List AssemblyReferences = null, List NameSpaceUsings = null) 27 | { 28 | ClassName = "Program" + (Guid.NewGuid()).ToString("N"); // just set the pure ClassName 29 | 30 | this.Code = 31 | "using System;\n" + 32 | "using System.Text;\n" + 33 | "using System.Text.RegularExpressions;\n" + 34 | "using QXS.ChatBot;\n" 35 | 36 | ; 37 | if (NameSpaceUsings == null) 38 | { 39 | this.Code += 40 | "using System.Collections;\n" + 41 | "using System.Collections.Generic;\n" + 42 | "using System.Linq;\n" + 43 | "using System.Reflection;\n" 44 | ; 45 | } 46 | else 47 | { 48 | Regex namspaceValidator = new Regex(@"^([a-z]+[a-z0-9]*)(\.[a-z]+[a-z0-9]*)*$", RegexOptions.IgnoreCase); 49 | foreach (string nspace in NameSpaceUsings) 50 | { 51 | if (!namspaceValidator.IsMatch(nspace)) 52 | { 53 | throw new ArgumentException("Invalid namespace using for value \"" + nspace + "\"!"); 54 | } 55 | this.Code += "using " + nspace + ";\n"; 56 | } 57 | } 58 | 59 | this.Code += @" 60 | namespace " + DefaultNamspace + @" 61 | { 62 | public class " + ClassName + @" 63 | { 64 | public static string Process(Match match, ChatSessionInterface session) 65 | { 66 | " + code.Replace("\n", "\n ").TrimEnd() + @" 67 | return null; 68 | } 69 | } 70 | } 71 | "; 72 | CSharpCodeProvider provider = new CSharpCodeProvider(); 73 | 74 | CompilerParameters parameters = new CompilerParameters(); 75 | parameters.ReferencedAssemblies.Add("System.dll"); 76 | parameters.ReferencedAssemblies.Add("mscorlib.dll"); 77 | parameters.ReferencedAssemblies.Add("ChatBot.dll"); 78 | if (AssemblyReferences == null) 79 | { 80 | parameters.ReferencedAssemblies.Add("System.Core.dll"); 81 | parameters.ReferencedAssemblies.Add("System.Data.dll"); 82 | parameters.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll"); 83 | parameters.ReferencedAssemblies.Add("System.Xml.dll"); 84 | parameters.ReferencedAssemblies.Add("System.Xml.Linq.dll"); 85 | } 86 | else 87 | { 88 | foreach (string aref in AssemblyReferences) 89 | { 90 | parameters.ReferencedAssemblies.Add(aref); 91 | } 92 | } 93 | // True - memory generation, false - external file generation 94 | parameters.GenerateInMemory = true; 95 | // True - exe file generation, false - dll file generation 96 | parameters.GenerateExecutable = false; 97 | 98 | results = provider.CompileAssemblyFromSource(parameters, this.Code); 99 | 100 | if (results.Errors.HasErrors) 101 | { 102 | StringBuilder sb = new StringBuilder(); 103 | 104 | foreach (CompilerError error in results.Errors) 105 | { 106 | sb.AppendLine(String.Format("Error ({0}): {1}", error.ErrorNumber, error.ErrorText)); 107 | } 108 | 109 | throw new InvalidOperationException(sb.ToString()); 110 | } 111 | 112 | ClassName = DefaultNamspace + "." + ClassName; // set the full ClassName 113 | method = results.CompiledAssembly.GetType(ClassName).GetMethod("Process"); 114 | } 115 | 116 | 117 | /// 118 | /// Execute the C# code that was compiled in a dll (in memory) and refered with the MethodInfo 119 | /// 120 | /// 121 | /// 122 | /// 123 | public string Execute(Match match, IChatSessionInterface session) 124 | { 125 | object result = method.Invoke(null, new object[] { match, session }); 126 | if (result == null) 127 | { 128 | return null; 129 | } 130 | return (string)result; 131 | } 132 | 133 | } 134 | } 135 | 136 | -------------------------------------------------------------------------------- /ChatBot/Generators/ChatBotRuleGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using System.Xml; 9 | 10 | namespace QXS.ChatBot 11 | { 12 | public class ChatBotRuleGenerator 13 | { 14 | public readonly string[] Prefixes; 15 | 16 | public ChatBotRuleGenerator() 17 | { 18 | Prefixes = new string[] { "", "QXS.ChatBot." }; 19 | } 20 | 21 | public ChatBotRuleGenerator(string[] prefixes) 22 | : base() 23 | { 24 | if (prefixes == null) 25 | { 26 | 27 | } 28 | } 29 | 30 | 31 | 32 | public string GetRuleName(XmlNode node) 33 | { 34 | return node.Attributes["Name"].Value; 35 | } 36 | 37 | public string GetRulePattern(XmlNode node) 38 | { 39 | foreach (XmlNode subnode in node.SelectChatBotNodes("cb:Pattern")) 40 | { 41 | return subnode.InnerText; 42 | } 43 | return ""; 44 | } 45 | 46 | public int GetRuleWeight(XmlNode node) 47 | { 48 | int weight = -100; 49 | foreach (XmlNode subnode in node.SelectChatBotNodes("cb:Weight")) 50 | { 51 | if (Int32.TryParse(subnode.InnerText.Trim(), out weight)) 52 | { 53 | return weight; 54 | } 55 | } 56 | return weight; 57 | } 58 | 59 | public Type ResolveBotRuleTypeByName(string name) 60 | { 61 | // resolve type 62 | Type type = null; 63 | foreach (string prefix in Prefixes) 64 | { 65 | type = Type.GetType(prefix + name); 66 | if (type != null) 67 | { 68 | break; 69 | } 70 | 71 | } 72 | // instance of BotRule? 73 | if (!typeof(BotRule).IsAssignableFrom(type)) 74 | { 75 | return null; 76 | } 77 | return type; 78 | } 79 | 80 | /// 81 | /// Uses reflection to call the "CreateRuleFromXml" method present in the type 82 | /// 83 | /// 84 | /// 85 | /// 86 | protected BotRule CreateRuleFromXml(Type type, XmlNode node) 87 | { 88 | MethodInfo method; 89 | while (type != null) 90 | { 91 | method = type.GetMethod("CreateRuleFromXml", new Type[] { typeof(ChatBotRuleGenerator), typeof(XmlNode) }); 92 | if (method != null) 93 | { 94 | return (BotRule)method.Invoke(null, new object[] { this, node }); 95 | } 96 | type = type.BaseType; 97 | } 98 | return null; 99 | } 100 | 101 | 102 | protected BotRule ProcessNode(XmlNode node) 103 | { 104 | if (node.Attributes["Type"] == null || node.Attributes["Name"] == null) 105 | { 106 | return null; 107 | } 108 | 109 | Type type = ResolveBotRuleTypeByName(node.Attributes["Type"].Value); 110 | if (type == null) 111 | { 112 | return null; 113 | } 114 | 115 | return CreateRuleFromXml(type, node); 116 | } 117 | 118 | public List ParseFromFile(string filename) 119 | { 120 | using (FileStream xml = new FileStream(filename, FileMode.Open)) 121 | { 122 | return Parse(xml); 123 | } 124 | } 125 | 126 | public List Parse(string xml) 127 | { 128 | XmlDocument doc = new XmlDocument(); 129 | doc.LoadXml(xml); 130 | return Parse(doc); 131 | } 132 | 133 | public List Parse(Stream inStream) 134 | { 135 | XmlDocument doc = new XmlDocument(); 136 | doc.Load(inStream); 137 | return Parse(doc); 138 | } 139 | 140 | public List Parse(TextReader reader) 141 | { 142 | XmlDocument doc = new XmlDocument(); 143 | doc.Load(reader); 144 | return Parse(doc); 145 | } 146 | 147 | public List Parse(XmlReader reader) 148 | { 149 | XmlDocument doc = new XmlDocument(); 150 | doc.Load(reader); 151 | return Parse(doc); 152 | } 153 | 154 | public List Parse(XmlDocument document, XmlNode startNode=null) 155 | { 156 | 157 | List liste = new List(); 158 | if (startNode == null) 159 | { 160 | foreach (XmlNode node in document.SelectChatBotNodes("/cb:ChatBot/cb:Rules/cb:Rule")) 161 | { 162 | BotRule rule = ProcessNode(node); 163 | if (rule != null) 164 | { 165 | liste.Add(rule); 166 | } 167 | } 168 | } 169 | else 170 | { 171 | foreach (XmlNode node in startNode.SelectChatBotNodes("cb:Rules/cb:Rule")) 172 | { 173 | BotRule rule = ProcessNode(node); 174 | if (rule != null) 175 | { 176 | liste.Add(rule); 177 | } 178 | } 179 | } 180 | 181 | return liste; 182 | } 183 | 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /ChatBot/LinkedList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace QXS.ChatBot 9 | { 10 | public class LinkedListException : Exception 11 | { 12 | public LinkedListException(string message) 13 | :base(message) 14 | { 15 | 16 | } 17 | public LinkedListException(string message, Exception innerException) 18 | : base(message, innerException) 19 | { 20 | 21 | } 22 | } 23 | 24 | 25 | internal class LinkedValue 26 | { 27 | public T current; 28 | public LinkedValue previous; 29 | public LinkedValue next; 30 | } 31 | 32 | 33 | public class LinkedListEnumerator : IEnumerator, IEnumerator 34 | { 35 | private LinkedList list; 36 | private LinkedValue now; 37 | public LinkedListEnumerator(LinkedList list) 38 | { 39 | this.list = list; 40 | this.now = null; 41 | } 42 | 43 | public bool MoveNext() 44 | { 45 | if (this.now == null) 46 | { 47 | this.now = list.firstValue; 48 | if (this.now == null) 49 | { 50 | return false; 51 | } 52 | return true; 53 | } 54 | 55 | if (this.now.next == null) 56 | { 57 | return false; 58 | } 59 | this.now = this.now.next; 60 | return true; 61 | } 62 | 63 | public void Reset() 64 | { 65 | this.now = null; 66 | } 67 | 68 | void IDisposable.Dispose() 69 | { 70 | list = null; 71 | now = null; 72 | } 73 | 74 | public T Current 75 | { 76 | get { return this.now.current; } 77 | } 78 | 79 | 80 | object IEnumerator.Current 81 | { 82 | get { return (object)Current; } 83 | } 84 | 85 | } 86 | 87 | 88 | public class LinkedList : ICollection, IEnumerable, IEnumerable 89 | { 90 | 91 | internal LinkedValue firstValue; 92 | internal LinkedValue lastValue; 93 | public readonly int Size; 94 | private bool _throwWhenFull; 95 | private int _elements; 96 | 97 | 98 | public LinkedList(IEnumerable data, int size=0, bool throwWhenFull=true) 99 | : this(size, throwWhenFull) 100 | { 101 | foreach (T elem in data) 102 | { 103 | Add(elem); 104 | } 105 | } 106 | public LinkedList(int size=0, bool throwWhenFull=true) 107 | { 108 | this.Size = size; 109 | this._throwWhenFull = throwWhenFull; 110 | this._elements = 0; 111 | } 112 | 113 | public int Count { get { return _elements; } } 114 | public bool IsReadOnly { get { return false; } } 115 | 116 | public T First 117 | { 118 | get 119 | { 120 | if (firstValue != null) 121 | { 122 | return firstValue.current; 123 | } 124 | return default(T); 125 | } 126 | } 127 | public T Last 128 | { 129 | get 130 | { 131 | if (lastValue != null) 132 | { 133 | return lastValue.current; 134 | } 135 | return default(T); 136 | } 137 | } 138 | 139 | public IEnumerator GetEnumerator() 140 | { 141 | return new LinkedListEnumerator(this); 142 | } 143 | 144 | IEnumerator IEnumerable.GetEnumerator() 145 | { 146 | return (IEnumerator)new LinkedListEnumerator(this); 147 | } 148 | 149 | 150 | public void CopyTo(T[] array, int arrayIndex) 151 | { 152 | LinkedValue val = firstValue; 153 | 154 | while (val != null) { 155 | array[arrayIndex++] = val.current; 156 | val = val.next; 157 | } 158 | } 159 | 160 | public bool Remove(T item) 161 | { 162 | LinkedValue val = firstValue; 163 | while (val != null) 164 | { 165 | if (val.current.Equals(item)) 166 | { 167 | val.previous.next = val.next; 168 | val.next.previous = val.previous; 169 | val.next = null; 170 | val.previous = null; 171 | return true; 172 | } 173 | 174 | val = val.next; 175 | } 176 | return false; 177 | } 178 | 179 | public bool Contains(T item) 180 | { 181 | LinkedValue val = firstValue; 182 | while (val != null) 183 | { 184 | if (val.current.Equals(item)) 185 | { 186 | return true; 187 | } 188 | 189 | val = val.next; 190 | } 191 | return false; 192 | } 193 | public void Add(T item) 194 | { 195 | lastValue = new LinkedValue() { current = item, previous = lastValue }; 196 | _elements++; 197 | if (firstValue == null) 198 | { 199 | firstValue = lastValue; 200 | } 201 | if (lastValue.previous != null) 202 | { 203 | lastValue.previous.next = lastValue; 204 | } 205 | 206 | if (_elements > Size && Size > 0) 207 | { 208 | if (_throwWhenFull) 209 | { 210 | throw new LinkedListException("The linked list has reached the limit of " + Size + " items."); 211 | } 212 | _elements = Size; 213 | LinkedValue value = firstValue; 214 | firstValue = value.next; 215 | firstValue.previous = null; 216 | value.next = null; 217 | value.previous = null; 218 | } 219 | 220 | } 221 | 222 | public void Push(T item) { 223 | firstValue = new LinkedValue() { current = item, next = firstValue }; 224 | _elements++; 225 | if (lastValue == null) 226 | { 227 | lastValue = firstValue; 228 | } 229 | if (firstValue.next != null) 230 | { 231 | firstValue.next.previous = firstValue; 232 | } 233 | 234 | if (_elements > Size && Size > 0) 235 | { 236 | if (_throwWhenFull) 237 | { 238 | throw new LinkedListException("The linked list has reached the limit of " + Size + " items."); 239 | } 240 | _elements = Size; 241 | LinkedValue value = lastValue; 242 | lastValue = value.previous; 243 | lastValue.next = null; 244 | value.previous = null; 245 | value.next = null; 246 | } 247 | } 248 | 249 | public void Enqueue(T item) { 250 | Push(item); 251 | } 252 | 253 | public T[] GetAsArray() 254 | { 255 | T[] array = new T[_elements]; 256 | 257 | int i = 0; 258 | LinkedValue val = firstValue; 259 | while (val != null) 260 | { 261 | array[i++] = val.current; 262 | val = val.next; 263 | } 264 | return array; 265 | } 266 | 267 | public T[] GetAsReverseArray() 268 | { 269 | T[] array = new T[_elements]; 270 | 271 | int i=0; 272 | LinkedValue val = lastValue; 273 | while (val != null) 274 | { 275 | array[i++] = val.current; 276 | val = val.previous; 277 | } 278 | return array; 279 | } 280 | 281 | public void Clear() 282 | { 283 | firstValue = null; 284 | lastValue = null; 285 | _elements = 0; 286 | } 287 | 288 | 289 | public T Dequeue() 290 | { 291 | if(lastValue==null) 292 | { 293 | throw new LinkedListException("The linked list is empty."); 294 | } 295 | _elements--; 296 | 297 | LinkedValue value = lastValue; 298 | if (value.previous != null) 299 | { 300 | lastValue = value.previous; 301 | lastValue.next = null; 302 | } 303 | else 304 | { 305 | firstValue = null; 306 | lastValue = null; 307 | } 308 | 309 | return value.current; 310 | } 311 | 312 | 313 | public T Pop() 314 | { 315 | if(firstValue==null) 316 | { 317 | throw new LinkedListException("The linked list is empty."); 318 | } 319 | _elements--; 320 | 321 | LinkedValue value = firstValue; 322 | if (value.next != null) 323 | { 324 | firstValue = value.next; 325 | firstValue.previous = null; 326 | } 327 | else 328 | { 329 | firstValue = null; 330 | lastValue = null; 331 | } 332 | 333 | return value.current; 334 | } 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /ChatBot/MethodExtensions/ChatBotXmlNodeExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using System.Xml; 9 | 10 | namespace QXS.ChatBot 11 | { 12 | public static class ChatBotXmlNodeExtensions 13 | { 14 | public const string ChatBotNamespace = "http://www.qxs.ch/ChatBotSchema.xsd"; 15 | 16 | public static XmlDocument GetXmlDocument(this XmlNode node) 17 | { 18 | XmlNode parentNode = node; 19 | while (!(parentNode is XmlDocument) && parentNode != null) 20 | { 21 | if (parentNode.OwnerDocument != null) 22 | { 23 | return parentNode.OwnerDocument; 24 | } 25 | parentNode = node.ParentNode; 26 | } 27 | if (parentNode == null) 28 | { 29 | return null; 30 | } 31 | return (XmlDocument)parentNode; 32 | } 33 | 34 | public static XmlNodeList SelectChatBotNodes(this XmlNode node, string xpath) 35 | { 36 | XmlNamespaceManager ns = new XmlNamespaceManager(node.GetXmlDocument().NameTable); 37 | ns.AddNamespace("cb", ChatBotNamespace); 38 | return node.SelectNodes(xpath, ns); 39 | } 40 | 41 | public static XmlNode SelectSingleChatBotNode(this XmlNode node, string xpath) 42 | { 43 | XmlNamespaceManager ns = new XmlNamespaceManager(node.GetXmlDocument().NameTable); 44 | ns.AddNamespace("cb", ChatBotNamespace); 45 | return node.SelectSingleNode(xpath, ns); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /ChatBot/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ChatBot")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ChatBot")] 13 | [assembly: AssemblyCopyright("Copyright © 2015")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("b111c61d-73e8-42c2-92d8-236fb5e9d264")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /ChatBot/Rules/BotRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Text.RegularExpressions; 7 | using System.Xml; 8 | using System.Collections; 9 | 10 | namespace QXS.ChatBot 11 | { 12 | public class BotRule 13 | { 14 | protected BotRule(string Name, int Weight) 15 | { 16 | if (Name == null) 17 | { 18 | throw new ArgumentException("Name is null.", nameof(Name)); 19 | } 20 | 21 | this._Name = Name; 22 | this._Weight = Weight; 23 | } 24 | 25 | protected BotRule(string Name, int Weight, Regex MessagePattern) 26 | : this(Name, Weight) 27 | { 28 | if (MessagePattern == null) 29 | { 30 | throw new ArgumentException("MessagePattern is null.", nameof(MessagePattern)); 31 | } 32 | this._MessagePattern = MessagePattern; 33 | } 34 | 35 | 36 | public BotRule(string Name, int Weight, Regex MessagePattern, Func Process) 37 | : this(Name, Weight, MessagePattern) 38 | { 39 | if (Process == null) 40 | { 41 | throw new ArgumentException("Process is null.", nameof(Process)); 42 | } 43 | this._Process = Process; 44 | } 45 | 46 | protected string _Name; 47 | public string Name { get { return _Name; } } 48 | 49 | protected int _Weight; 50 | public int Weight { get { return _Weight; } } 51 | 52 | protected Regex _MessagePattern; 53 | public Regex MessagePattern { get { return _MessagePattern; } } 54 | 55 | protected Func _Process; 56 | public Func Process { get { return _Process; } } 57 | 58 | public static BotRule CreateRuleFromXml(ChatBotRuleGenerator generator, XmlNode node) 59 | { 60 | BotRuleCodeCompiler brcc = new BotRuleCodeCompiler(node.SelectChatBotNodes("cb:Process").Cast().First().InnerText); 61 | 62 | return new BotRule( 63 | generator.GetRuleName(node), 64 | generator.GetRuleWeight(node), 65 | new Regex(generator.GetRulePattern(node)), 66 | delegate(Match match, IChatSessionInterface session) { 67 | // Capture the BotRuleCodeCompiler in the lambda to execute the C# code 68 | return brcc.Execute(match, session); 69 | } 70 | ); 71 | } 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /ChatBot/Rules/BotRuleContainer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Text.RegularExpressions; 6 | using System.Threading.Tasks; 7 | 8 | namespace QXS.ChatBot 9 | { 10 | public abstract class BotRuleContainer : BotRule 11 | { 12 | protected BotRuleContainer(string Name, int Weight) 13 | : base(Name, Weight) 14 | { 15 | } 16 | 17 | protected BotRuleContainer(string Name, int Weight, Regex MessagePattern) 18 | : base(Name, Weight, MessagePattern) 19 | { 20 | } 21 | 22 | protected BotRuleContainer(string Name, int Weight, Regex MessagePattern, Func Process) 23 | : base(Name, Weight, MessagePattern, Process) 24 | { 25 | 26 | } 27 | 28 | protected SortedList> _NestedBotRules = new SortedList>(new DescComparer()); 29 | public SortedList> NestedBotRules { get { return _NestedBotRules; } } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ChatBot/Rules/ConditionBotRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Text.RegularExpressions; 7 | using System.Xml; 8 | using System.Collections; 9 | 10 | namespace QXS.ChatBot 11 | { 12 | public class ConditionBotRule : BotRuleContainer 13 | { 14 | public enum Operator 15 | { 16 | Equal, 17 | EqualIgnoreCase, 18 | NotEqual, 19 | NotEqualIgnoreCase, 20 | ContainsKey, 21 | ContainsValue, 22 | ContainsValueIgnoreCase, 23 | } 24 | 25 | protected IEnumerable> _Conditions; 26 | //protected SortedList> _BotRules = new SortedList>(new DescComparer()); 27 | 28 | public ConditionBotRule(string Name, int Weight, IEnumerable> Conditions, IEnumerable Rules) 29 | : base(Name, Weight) 30 | { 31 | this._MessagePattern = new Regex("^.*$"); 32 | this._Conditions = Conditions; 33 | Dictionary ruleNames = new Dictionary(); 34 | foreach (BotRule rule in Rules) 35 | { 36 | if (rule.Process == null) 37 | { 38 | throw new ArgumentException("Process is null.", "Rules"); 39 | } 40 | if (rule.MessagePattern == null) 41 | { 42 | throw new ArgumentException("MessagePattern is null.", "Rules"); 43 | } 44 | if (ruleNames.ContainsKey(rule.Name)) 45 | { 46 | throw new ArgumentException("Names are not unique. Duplicate key found for rule name \"" + rule.Name + "\".", "Rules"); 47 | } 48 | ruleNames[rule.Name] = true; 49 | if (!this._NestedBotRules.ContainsKey(rule.Weight)) 50 | { 51 | this._NestedBotRules[rule.Weight] = new List(); 52 | } 53 | this._NestedBotRules[rule.Weight].Add(rule); 54 | } 55 | 56 | this._Process = this.ProcessSubrules; 57 | } 58 | 59 | public string ProcessSubrules(Match match, IChatSessionInterface session) 60 | { 61 | foreach (Tuple condition in this._Conditions) 62 | { 63 | if (!session.SessionStorage.Values.ContainsKey(condition.Item1)) 64 | { 65 | return null; 66 | } 67 | switch (condition.Item2) 68 | { 69 | case Operator.Equal: 70 | if (session.SessionStorage.Values[condition.Item1] != condition.Item3) 71 | { 72 | return null; 73 | } 74 | break; 75 | case Operator.NotEqual: 76 | if (session.SessionStorage.Values[condition.Item1] == condition.Item3) 77 | { 78 | return null; 79 | } 80 | break; 81 | case Operator.EqualIgnoreCase: 82 | if (session.SessionStorage.Values[condition.Item1].ToLower() != condition.Item3.ToLower()) 83 | { 84 | return null; 85 | } 86 | break; 87 | case Operator.NotEqualIgnoreCase: 88 | if (session.SessionStorage.Values[condition.Item1].ToLower() == condition.Item3.ToLower()) 89 | { 90 | return null; 91 | } 92 | break; 93 | case Operator.ContainsKey: 94 | if (!session.SessionStorage.Values.ContainsKey(condition.Item1)) 95 | { 96 | return null; 97 | } 98 | break; 99 | case Operator.ContainsValue: 100 | if (!session.SessionStorage.Values[condition.Item1].Contains(condition.Item3)) 101 | { 102 | return null; 103 | } 104 | break; 105 | case Operator.ContainsValueIgnoreCase: 106 | if (!session.SessionStorage.Values[condition.Item1].ToLower().Contains(condition.Item3.ToLower())) 107 | { 108 | return null; 109 | } 110 | break; 111 | } 112 | } 113 | 114 | foreach (List rules in this._NestedBotRules.Values) 115 | { 116 | foreach (BotRule rule in rules) 117 | { 118 | Match submatch = rule.MessagePattern.Match(match.Value); 119 | if (submatch.Success) 120 | { 121 | 122 | string msg = rule.Process(submatch, session); 123 | if (msg != null) 124 | { 125 | return msg; 126 | } 127 | } 128 | } 129 | } 130 | // no hit found 131 | return null; 132 | } 133 | 134 | new public static BotRule CreateRuleFromXml(ChatBotRuleGenerator generator, XmlNode node) 135 | { 136 | // get unique setters 137 | List> conditions = new List>(); 138 | foreach (XmlNode subnode in node.SelectChatBotNodes("cb:Conditions/cb:Condition").Cast().Where(n => n.Attributes["Key"] != null && n.Attributes["Operator"] != null)) 139 | { 140 | switch (subnode.Attributes["Operator"].Value.Trim().ToLower()) 141 | { 142 | case "equal": 143 | case "eq": 144 | conditions.Add(new Tuple(subnode.Attributes["Key"].Value, Operator.Equal, subnode.InnerText)); 145 | break; 146 | case "equalignorecase": 147 | case "ieq": 148 | conditions.Add(new Tuple(subnode.Attributes["Key"].Value, Operator.EqualIgnoreCase, subnode.InnerText)); 149 | break; 150 | case "notequal": 151 | case "ne": 152 | conditions.Add(new Tuple(subnode.Attributes["Key"].Value, Operator.NotEqual, subnode.InnerText)); 153 | break; 154 | case "notequalignorecase": 155 | case "ine": 156 | conditions.Add(new Tuple(subnode.Attributes["Key"].Value, Operator.NotEqualIgnoreCase, subnode.InnerText)); 157 | break; 158 | case "containskey": 159 | case "ck": 160 | conditions.Add(new Tuple(subnode.Attributes["Key"].Value, Operator.ContainsKey, subnode.InnerText)); 161 | break; 162 | case "containsvalue": 163 | case "cv": 164 | conditions.Add(new Tuple(subnode.Attributes["Key"].Value, Operator.ContainsValue, subnode.InnerText)); 165 | break; 166 | case "containsvalueignorecase": 167 | case "icv": 168 | conditions.Add(new Tuple(subnode.Attributes["Key"].Value, Operator.ContainsValueIgnoreCase, subnode.InnerText)); 169 | break; 170 | 171 | } 172 | 173 | } 174 | 175 | return new ConditionBotRule( 176 | generator.GetRuleName(node), 177 | generator.GetRuleWeight(node), 178 | conditions, 179 | generator.Parse(node.OwnerDocument, node) 180 | ); 181 | } 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /ChatBot/Rules/PowershellBotRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Text.RegularExpressions; 6 | using System.Threading.Tasks; 7 | using System.Management.Automation; 8 | using System.Collections.ObjectModel; 9 | using System.Xml; 10 | 11 | namespace QXS.ChatBot 12 | { 13 | /// 14 | /// Uses PowerShell to execute the powershell script 15 | /// 16 | public class PowershellBotRule : BotRule 17 | { 18 | protected string _script; 19 | protected bool _showErrors = true; 20 | 21 | public PowershellBotRule(string name, int weight, Regex messagePattern, string script) 22 | : base(name, weight, messagePattern) 23 | { 24 | this._script = script; 25 | this._Process = this.ProcessScript; 26 | } 27 | 28 | public PowershellBotRule(string name, int weight, Regex messagePattern, string script, bool showErrors) 29 | : this(name, weight, messagePattern, script) 30 | { 31 | this._showErrors = showErrors; 32 | } 33 | 34 | 35 | /// 36 | /// Process the powershell script 37 | /// 38 | /// 39 | /// 40 | /// 41 | public string ProcessScript(Match match, IChatSessionInterface session) 42 | { 43 | string output = ""; 44 | 45 | using(PowerShell ps = PowerShell.Create()) 46 | { 47 | // we must import the parameters $session, $match 48 | ps.AddScript("Param($match, $session)\n" + this._script); 49 | ps.AddParameter("match", match); 50 | ps.AddParameter("session", session); 51 | foreach (PSObject outputItem in ps.Invoke()) 52 | { 53 | output += outputItem.BaseObject.ToString() + "\n"; 54 | } 55 | if (this._showErrors) 56 | { 57 | foreach (ErrorRecord e in ps.Streams.Error) 58 | { 59 | output += "ERROR: " + e.ToString() + "\n"; 60 | } 61 | } 62 | } 63 | 64 | output = output.Trim(); 65 | if (output == "") 66 | { 67 | return null; 68 | } 69 | 70 | return output; 71 | } 72 | 73 | new public static BotRule CreateRuleFromXml(ChatBotRuleGenerator generator, XmlNode node) 74 | { 75 | return new PowershellBotRule( 76 | generator.GetRuleName(node), 77 | generator.GetRuleWeight(node), 78 | new Regex(generator.GetRulePattern(node)), 79 | node.SelectChatBotNodes("cb:Script").Cast().First().InnerText 80 | ); 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /ChatBot/Rules/RandomAnswersBotRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Text.RegularExpressions; 7 | using System.Xml; 8 | using System.Collections; 9 | 10 | namespace QXS.ChatBot 11 | { 12 | /// 13 | /// Bot rule which randomly returns one of the messages 14 | /// 15 | public class RandomAnswersBotRule : BotRule 16 | { 17 | protected Random _rnd = new Random(); 18 | 19 | protected string[] _messages; 20 | 21 | public RandomAnswersBotRule(string name, int Weight, Regex messagePattern, string[] messages) 22 | : base(name, Weight, messagePattern) 23 | { 24 | this._messages = messages; 25 | this._Process = this.SendRandomMessage; 26 | } 27 | 28 | /// 29 | /// Sends randomly one of the messages 30 | /// 31 | /// 32 | /// 33 | /// 34 | public string SendRandomMessage(Match match, IChatSessionInterface session) 35 | { 36 | return this._messages[_rnd.Next(this._messages.Length)]; 37 | } 38 | 39 | new public static BotRule CreateRuleFromXml(ChatBotRuleGenerator generator, XmlNode node) 40 | { 41 | return new RandomAnswersBotRule( 42 | generator.GetRuleName(node), 43 | generator.GetRuleWeight(node), 44 | new Regex(generator.GetRulePattern(node)), 45 | node.SelectChatBotNodes("cb:Messages/cb:Message").Cast().Select(n => n.InnerText).ToArray() 46 | ); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /ChatBot/Rules/ReplacementBotRule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Text.RegularExpressions; 7 | using System.Xml; 8 | using System.Collections; 9 | 10 | namespace QXS.ChatBot 11 | { 12 | public class ReplacementBotRule : BotRule 13 | { 14 | protected Random rnd = new Random(); 15 | protected string[] _Replacements; 16 | protected Regex _Regex = new Regex("\\$(r|s)\\$([a-z0-9]+)\\$", RegexOptions.IgnoreCase); 17 | protected Dictionary _setters = new Dictionary(); 18 | 19 | public ReplacementBotRule(string Name, int Weight, Regex MessagePattern, string Replacement, Dictionary setters) 20 | : this(Name, Weight, MessagePattern, Replacement) 21 | { 22 | this._setters = setters; 23 | } 24 | public ReplacementBotRule(string Name, int Weight, Regex MessagePattern, string[] Replacements, Dictionary setters) 25 | : this(Name, Weight, MessagePattern, Replacements) 26 | { 27 | this._setters = setters; 28 | } 29 | 30 | public ReplacementBotRule(string Name, int Weight, Regex MessagePattern, string Replacement) 31 | : base(Name, Weight, MessagePattern) 32 | { 33 | this._Replacements = new string[] { Replacement }; 34 | this._Process = this.ReplaceMessage; 35 | } 36 | 37 | public ReplacementBotRule(string Name, int Weight, Regex MessagePattern, string[] Replacements) 38 | : base(Name, Weight, MessagePattern) 39 | { 40 | this._Replacements = Replacements; 41 | this._Process = this.ReplaceMessage; 42 | } 43 | 44 | public string ReplaceMessage(Match match, IChatSessionInterface session) 45 | { 46 | // set the setters 47 | foreach (string key in this._setters.Keys) 48 | { 49 | session.SessionStorage.Values[key] = this._Regex.Replace( 50 | this._setters[key], 51 | (Match m) => 52 | { 53 | switch (m.Groups[1].Value.ToLower()) 54 | { 55 | case "s": 56 | if (session.SessionStorage.Values.ContainsKey(m.Groups[2].Value)) 57 | { 58 | return session.SessionStorage.Values[m.Groups[2].Value]; 59 | } 60 | break; 61 | case "r": 62 | return match.Groups[m.Groups[2].Value].Value; 63 | } 64 | return ""; 65 | } 66 | ); 67 | 68 | } 69 | 70 | // send a anwer 71 | 72 | if (this._Replacements.Length == 0) 73 | { 74 | return ""; 75 | } 76 | string msg; 77 | if (this._Replacements.Length > 1) 78 | { 79 | msg = this._Replacements[rnd.Next(this._Replacements.Length)]; 80 | } 81 | else 82 | { 83 | msg = this._Replacements[0]; 84 | } 85 | 86 | return this._Regex.Replace( 87 | msg, 88 | (Match m) => 89 | { 90 | switch (m.Groups[1].Value.ToLower()) 91 | { 92 | case "s": 93 | if (session.SessionStorage.Values.ContainsKey(m.Groups[2].Value)) 94 | { 95 | return session.SessionStorage.Values[m.Groups[2].Value]; 96 | } 97 | break; 98 | case "r": 99 | return match.Groups[m.Groups[2].Value].Value; 100 | } 101 | return ""; 102 | } 103 | ); 104 | } 105 | 106 | new public static BotRule CreateRuleFromXml(ChatBotRuleGenerator generator, XmlNode node) 107 | { 108 | // get unique setters 109 | Dictionary setters = new Dictionary(); 110 | foreach (XmlNode subnode in node.SelectChatBotNodes("cb:Setters/cb:Set").Cast().Where(n => n.Attributes["Key"] != null)) 111 | { 112 | setters[subnode.Attributes["Key"].Value] = subnode.InnerText; 113 | } 114 | 115 | return new ReplacementBotRule( 116 | generator.GetRuleName(node), 117 | generator.GetRuleWeight(node), 118 | new Regex(generator.GetRulePattern(node)), 119 | node.SelectChatBotNodes("cb:Messages/cb:Message").Cast().Select(n => n.InnerText).ToArray(), 120 | setters 121 | ); 122 | } 123 | } 124 | } -------------------------------------------------------------------------------- /ChatBot/SessionStorage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace QXS.ChatBot 8 | { 9 | /// 10 | /// Session Storage fr the chatbot 11 | /// 12 | public class SessionStorage 13 | { 14 | public Dictionary Values = new Dictionary(); 15 | public Stack Stack = new Stack(); 16 | 17 | public void TrimStack(int elementsToKeep=5) 18 | { 19 | string[] newStack = Stack.ToArray(); 20 | Stack = new Stack(newStack.Skip(Math.Max(0, newStack.Length - elementsToKeep))); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ChatBot/Visualizers/ConsoleBotRuleVisualizer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace QXS.ChatBot 8 | { 9 | public class ConsoleBotRuleVisualizer : IBotRuleVisualizerInterface 10 | { 11 | 12 | public void Visualize(IEnumerable Rules) 13 | { 14 | WriteRules(Rules); 15 | } 16 | 17 | protected void WriteRules(IEnumerable Rules, int indent=0) 18 | { 19 | string prefix = new String(' ', indent * 3); 20 | foreach (BotRule rule in Rules) 21 | { 22 | Console.WriteLine(prefix + "Type: " + rule.GetType().FullName); 23 | Console.WriteLine(prefix + "Name: " + rule.Name); 24 | Console.WriteLine(prefix + "Weight: " + rule.Weight); 25 | Console.WriteLine(prefix + "Regex: " + rule.MessagePattern); 26 | Console.WriteLine(); 27 | if (rule is BotRuleContainer brc) 28 | { 29 | List NestedRules = new List(); 30 | foreach(KeyValuePair> kv in brc.NestedBotRules) 31 | { 32 | foreach (BotRule b in kv.Value) 33 | { 34 | NestedRules.Add(b); 35 | } 36 | } 37 | WriteRules(NestedRules, ++indent); 38 | } 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /ChatBot/Visualizers/IBotRuleVisualizerInterface.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace QXS.ChatBot 8 | { 9 | public interface IBotRuleVisualizerInterface 10 | { 11 | void Visualize(IEnumerable Rules); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ChatBot/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | 676 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ChatBot 2 | 3 | Just a Proof of Concept ;-) 4 | 5 | * Multiple BotRules are supported 6 | * BotRule 7 | `Runs a delegate / Extend this rule to create custom ones` 8 | * ConditionBotRule 9 | `Runs nested Rules, in case conditions are satisfied` 10 | * PowershellBotRule 11 | `Uses the powershell` 12 | * RandomAnswersBotRule 13 | `Generates a random answer` 14 | * ReplacementBotRule 15 | `Returns a answer, that contains words from the previous conversations, and can also be used to learn something` 16 | * Supported Conversation types: 17 | * Console Conversations 18 | * Lync Conversations 19 | * Text-To-Speech & Speech-Recognition Conversations 20 | * Any other Conversation, that implements the ChatSessionInterface 21 | * Supports Bot-Definition from: 22 | * XML 23 | * C#/.NET/Powershell 24 | * or any mixture of it 25 | * Visualizers, that visualize your current ruleset 26 | 27 | 28 | Checkout the example 29 | 30 | ![Image of a chatbot conversation](https://raw.githubusercontent.com/qxsch/ChatBot/master/ChatBot.jpg) 31 | 32 | 33 | ### C# Example 34 | ```cs 35 | using System; 36 | using System.Collections.Generic; 37 | using System.Linq; 38 | using System.Text; 39 | using System.Threading.Tasks; 40 | using QXS.ChatBot; 41 | using System.Text.RegularExpressions; 42 | 43 | namespace QXS.ChatBot.Examples 44 | { 45 | class Program 46 | { 47 | static void Main(string[] args) 48 | { 49 | 50 | ChatBot cb = new ChatBot( 51 | new List() 52 | { 53 | new ReplacementBotRule("repeat", 40, new Regex("(please )?repeat(? .*)", RegexOptions.IgnoreCase), new string[] { "i repeat your sentence:$r$sentence$", "$s$BotName$ repeats your sentence:$r$sentence$"}), 54 | new RandomAnswersBotRule("getfeeling", 40, new Regex("how do you feel", RegexOptions.IgnoreCase), new string[] {"i feel great", "i feel tired", "i feel awful", "i feel happy"}), 55 | new BotRule( 56 | Name: "var_dump", 57 | Weight: 200, 58 | MessagePattern: new Regex("^var_?dump$", RegexOptions.IgnoreCase), 59 | Process: delegate(Match match, ChatSessionInterface session) { 60 | string answer = "Variables: \n"; 61 | foreach (string key in session.SessionStorage.Values.Keys) 62 | { 63 | answer += " " + key + " = " + session.SessionStorage.Values[key] + "\n"; 64 | } 65 | answer += "---\n"; 66 | answer += "History: \n"; 67 | int i=0; 68 | foreach (string ruleName in session.GetRuleHistory()) 69 | { 70 | answer += " " + (++i) + ". " + ruleName + "\n"; 71 | } 72 | return answer; 73 | } 74 | ), 75 | new BotRule( 76 | Name: "setbotname", 77 | Weight: 10, 78 | MessagePattern: new Regex("(your name is|you are) (now )?(.*)", RegexOptions.IgnoreCase), 79 | Process: delegate(Match match, ChatSessionInterface session) { 80 | session.SessionStorage.Values["BotName"] = match.Groups[3].Value; 81 | return "My name is now " + session.SessionStorage.Values["BotName"]; 82 | } 83 | ), 84 | new BotRule( 85 | Name: "getbotname", 86 | Weight: 10, 87 | MessagePattern: new Regex("(who are you|(what is|say) your name)", RegexOptions.IgnoreCase), 88 | Process: delegate(Match match, ChatSessionInterface session) { 89 | if (!session.SessionStorage.Values.ContainsKey("BotName")) 90 | { 91 | return "I do not have a name"; 92 | } 93 | if (match.Value.ToLower() == "who are you") 94 | { 95 | return "i am " + session.SessionStorage.Values["BotName"]; 96 | } 97 | return "My name is " + session.SessionStorage.Values["BotName"]; 98 | } 99 | ), 100 | new BotRule( 101 | Name: "setusername", 102 | Weight: 10, 103 | MessagePattern: new Regex("my name is (now )?(.*)", RegexOptions.IgnoreCase), 104 | Process: delegate(Match match, ChatSessionInterface session) { 105 | session.SessionStorage.Values["UserName"] = match.Groups[2].Value; 106 | return "Hi " + session.SessionStorage.Values["UserName"]; 107 | } 108 | ), 109 | new BotRule( 110 | Name: "getusername", 111 | Weight: 20, 112 | MessagePattern: new Regex("(what is|say) my name", RegexOptions.IgnoreCase), 113 | Process: delegate(Match match, ChatSessionInterface session) { 114 | if (!session.SessionStorage.Values.ContainsKey("UserName")) 115 | { 116 | return "Sorry, but you have not told my your name"; 117 | } 118 | return "Your name is " + session.SessionStorage.Values["UserName"]; 119 | } 120 | ), 121 | new BotRule( 122 | Name: "greet", 123 | Weight: 1, 124 | MessagePattern: new Regex("(hi|hello)", RegexOptions.IgnoreCase), 125 | Process: delegate(Match match, ChatSessionInterface session) { 126 | string answer = "Hi"; 127 | 128 | if (session.SessionStorage.Values.ContainsKey("UserName")) 129 | { 130 | answer += " " + session.SessionStorage.Values["UserName"]; 131 | } 132 | answer += "!"; 133 | if (session.SessionStorage.Values.ContainsKey("BotName")) 134 | { 135 | answer += " I'm " + session.SessionStorage.Values["BotName"]; 136 | } 137 | return answer; 138 | } 139 | 140 | ) 141 | } 142 | ); 143 | 144 | cb.TalkWith(new ConsoleChatSession()); 145 | } 146 | } 147 | } 148 | ``` 149 | 150 | ### XML Example 151 | 152 | ```xml 153 | 154 | 155 | 156 | 157 | 158 | 45 159 | 160 | i repeat your sentence: $s$sentence$ 161 | $s$BotName$ repeats your sentence: $s$sentence$ 162 | 163 | 164 | 165 | 166 | .*)]]> 167 | 40 168 | 169 | i repeat your sentence: $r$sentence$ 170 | $s$BotName$ repeats your sentence: $r$sentence$ 171 | 172 | 173 | $r$sentence$ 174 | 175 | 176 | 177 | 178 | \S+)]]> 179 | 40 180 | 187 | 188 | 189 | 190 | 191 | 30 192 | 203 | 204 | 205 | 206 | 207 | 20 208 | 209 | i feel ok 210 | i am a bit bored 211 | 212 | 213 | 214 | 215 | .*)]]> 216 | 20 217 | 218 | your name is now $r$username$ 219 | you are now $r$username$ 220 | pleased to meet you $r$username$ 221 | 222 | 223 | $r$username$ 224 | 225 | 226 | 227 | 228 | 229 | 20 230 | 231 | i don't know your name 232 | who are you? 233 | 234 | 235 | 236 | 237 | .*)]]> 238 | 20 239 | 240 | my name is now $r$botname$ 241 | i am now $r$botname$ 242 | 243 | 244 | $r$botname$ 245 | 246 | 247 | 248 | 249 | 250 | 20 251 | 252 | i do not have a name 253 | 254 | 255 | 256 | 257 | 258 | 60 259 | 260 | 261 | 262 | 263 | 264 | 265 | 20 266 | 267 | My name is $s$botname$ 268 | I am $s$botname$ 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 60 277 | 278 | 279 | 280 | 281 | 282 | 283 | 20 284 | 285 | Your name is $s$username$ 286 | You are $s$username$ 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | ``` 295 | -------------------------------------------------------------------------------- /nuget.ps1: -------------------------------------------------------------------------------- 1 | cd ChatBot 2 | # ..\..\..\NuGet-Signed.exe spec 3 | ..\..\..\NuGet-Signed.exe pack ChatBot.csproj 4 | 5 | # UPLOAD 6 | # ..\..\..\nuget setApiKey Your-API-Key 7 | # ..\..\..\nuget push ChatBot.1.0.0.0.nupkg --------------------------------------------------------------------------------