public string ProcessScript(Match match, ChatSessionInterface session) { string output = ""; using (PowerShell ps = PowerShell.Create()) { // we múst import the parameters $session, $match ps.AddScript("Param($match, $session)\n" + this._Script); ps.AddParameter("match", match); ps.AddParameter("session", session); foreach (PSObject outputItem in ps.Invoke()) { output += outputItem.BaseObject.ToString() + "\n"; } if (this._showErrors) { foreach (ErrorRecord e in ps.Streams.Error) { output += "ERROR: " + e.ToString() + "\n"; } } } output = output.Trim(); if (output == "") { return(null); } return(output); }
public Tuple <string, object> FindAnswer(ChatSessionInterface session, string messageIn) { foreach (List <BotRule> rules in this._botRules.Values) { foreach (BotRule rule in rules) { Match match = rule.MessagePattern.Match(messageIn); if (match.Success) { Tuple <string, object> res; if (rule.GetType() == typeof(ExerciseBotRule)) { ExerciseBotRule exerciseBotRule = rule as ExerciseBotRule; res = exerciseBotRule.ProcessSpecial(match, session, _exerciseService); } else { res = new Tuple <string, object>(rule.Process(match, session), null); } if (res != null) { //session.AddResponseToHistory(new BotResponse(rule.Name, messageIn, msg)); return(res); } } } } return(null); }
public string ProcessScript(Match match, ChatSessionInterface session) { string output = ""; using(PowerShell ps = PowerShell.Create()) { // we múst import the parameters $session, $match ps.AddScript("Param($match, $session)\n" + this._Script); ps.AddParameter("match", match); ps.AddParameter("session", session); foreach (PSObject outputItem in ps.Invoke()) { output += outputItem.BaseObject.ToString() + "\n"; } if (this._showErrors) { foreach (ErrorRecord e in ps.Streams.Error) { output += "ERROR: " + e.ToString() + "\n"; } } } output = output.Trim(); if (output == "") { return null; } return output; }
protected void sendResponse(ChatSessionInterface session, string messageOut) { session.SendMessage(messageOut); if (OnMessageSent != null) { OnMessageSent(session, messageOut); } }
/// <summary> /// Starts a conversation over a session /// </summary> /// <param name="session"></param> public void talkWith(ChatSessionInterface session) { if (session == null) { return; } if (OnConverationStarted != null) { OnConverationStarted(session); } string messageIn = ""; string messageOut = ""; for (messageIn = session.ReadMessage(); !this.ExitCondition(messageIn); messageIn = session.ReadMessage()) { if (OnMessageReceived != null) { OnMessageReceived(session, messageIn); } messageOut = findAnswer(session, messageIn); if (messageOut == null) { // do we have a default answer? if (DefaultAnswer != null) { messageOut = DefaultAnswer(messageIn); } // still null? if (messageOut == null) { sendResponse(session, "What did you say?"); } else { sendResponse(session, messageOut); } } else { sendResponse(session, messageOut); } // still interactive? if (!session.IsInteractive) { break; } } if (OnConverationEnded != null) { OnConverationEnded(session); } }
public string Execute(Match match, ChatSessionInterface session) { object result = method.Invoke(null, new object[] { match, session }); if (result == null) { return(null); } return((string)result); }
/// <summary> /// Find a matching rule/reponse to a question/message /// </summary> /// <param name="session">The session, that should be used</param> /// <param name="messageIn">The message that came in</param> /// <returns>the response string or null in case no answer was found</returns> public string findAnswer(ChatSessionInterface session, string messageIn) { foreach (List <BotRule> rules in this._botRules.Values) { foreach (BotRule rule in rules) { Match match = rule.MessagePattern.Match(messageIn); if (match.Success) { string msg = rule.Process(match, session); if (msg != null) { session.AddResponseToHistory(new BotResponse(rule.Name, messageIn, msg)); return(msg); } } } } return(null); }
public string ReplaceMessage(Match match, ChatSessionInterface session) { // set the setters foreach (string key in this._setters.Keys) { session.SessionStorage.Values[key] = this._Regex.Replace( this._setters[key], (Match m) => { switch (m.Groups[1].Value.ToLower()) { case "s": if (session.SessionStorage.Values.ContainsKey(m.Groups[2].Value)) { return session.SessionStorage.Values[m.Groups[2].Value]; } break; case "r": return match.Groups[m.Groups[2].Value].Value; } return ""; } ); } // send a anwer if (this._Replacements.Length == 0) { return ""; } string msg; if (this._Replacements.Length > 1) { msg = this._Replacements[rnd.Next(this._Replacements.Length)]; } else { msg = this._Replacements[0]; } return this._Regex.Replace( msg, (Match m) => { switch (m.Groups[1].Value.ToLower()) { case "s": if (session.SessionStorage.Values.ContainsKey(m.Groups[2].Value)) { return session.SessionStorage.Values[m.Groups[2].Value]; } break; case "r": return match.Groups[m.Groups[2].Value].Value; } return ""; } ); }
public string ProcessSubrules(Match match, ChatSessionInterface session) { foreach (Tuple <string, Operator, string> condition in this._Conditions) { if (!session.SessionStorage.Values.ContainsKey(condition.Item1)) { return(null); } switch (condition.Item2) { case Operator.Equal: if (session.SessionStorage.Values[condition.Item1] != condition.Item3) { return(null); } break; case Operator.NotEqual: if (session.SessionStorage.Values[condition.Item1] == condition.Item3) { return(null); } break; case Operator.EqualIgnoreCase: if (session.SessionStorage.Values[condition.Item1].ToLower() != condition.Item3.ToLower()) { return(null); } break; case Operator.NotEqualIgnoreCase: if (session.SessionStorage.Values[condition.Item1].ToLower() == condition.Item3.ToLower()) { return(null); } break; case Operator.ContainsKey: if (!session.SessionStorage.Values.ContainsKey(condition.Item1)) { return(null); } break; case Operator.ContainsValue: if (!session.SessionStorage.Values[condition.Item1].Contains(condition.Item3)) { return(null); } break; case Operator.ContainsValueIgnoreCase: if (!session.SessionStorage.Values[condition.Item1].ToLower().Contains(condition.Item3.ToLower())) { return(null); } break; } } foreach (List <BotRule> rules in this._NestedBotRules.Values) { foreach (BotRule rule in rules) { Match submatch = rule.MessagePattern.Match(match.Value); if (submatch.Success) { string msg = rule.Process(submatch, session); if (msg != null) { return(msg); } } } } // no hit found return(null); }
public string SendRandomMessage(Match match, ChatSessionInterface session) { return(this._messages[rnd.Next(this._messages.Length)]); }
public string ProcessSubrules(Match match, ChatSessionInterface session) { foreach (Tuple<string, Operator, string> condition in this._Conditions) { if (!session.SessionStorage.Values.ContainsKey(condition.Item1)) { return null; } switch (condition.Item2) { case Operator.Equal: if (session.SessionStorage.Values[condition.Item1] != condition.Item3) { return null; } break; case Operator.NotEqual: if (session.SessionStorage.Values[condition.Item1] == condition.Item3) { return null; } break; case Operator.EqualIgnoreCase: if (session.SessionStorage.Values[condition.Item1].ToLower() != condition.Item3.ToLower()) { return null; } break; case Operator.NotEqualIgnoreCase: if (session.SessionStorage.Values[condition.Item1].ToLower() == condition.Item3.ToLower()) { return null; } break; case Operator.ContainsKey: if (!session.SessionStorage.Values.ContainsKey(condition.Item1)) { return null; } break; case Operator.ContainsValue: if (!session.SessionStorage.Values[condition.Item1].Contains(condition.Item3)) { return null; } break; case Operator.ContainsValueIgnoreCase: if (!session.SessionStorage.Values[condition.Item1].ToLower().Contains(condition.Item3.ToLower())) { return null; } break; } } foreach (List<BotRule> rules in this._NestedBotRules.Values) { foreach (BotRule rule in rules) { Match submatch = rule.MessagePattern.Match(match.Value); if (submatch.Success) { string msg = rule.Process(submatch, session); if (msg != null) { return msg; } } } } // no hit found return null; }
public string ReplaceMessage(Match match, ChatSessionInterface session) { // set the setters foreach (string key in this._setters.Keys) { session.SessionStorage.Values[key] = this._Regex.Replace( this._setters[key], (Match m) => { switch (m.Groups[1].Value.ToLower()) { case "s": if (session.SessionStorage.Values.ContainsKey(m.Groups[2].Value)) { return(session.SessionStorage.Values[m.Groups[2].Value]); } break; case "r": return(match.Groups[m.Groups[2].Value].Value); } return(""); } ); } // send a anwer if (this._Replacements.Length == 0) { return(""); } string msg; if (this._Replacements.Length > 1) { msg = this._Replacements[rnd.Next(this._Replacements.Length)]; } else { msg = this._Replacements[0]; } return(this._Regex.Replace( msg, (Match m) => { switch (m.Groups[1].Value.ToLower()) { case "s": if (session.SessionStorage.Values.ContainsKey(m.Groups[2].Value)) { return session.SessionStorage.Values[m.Groups[2].Value]; } break; case "r": return match.Groups[m.Groups[2].Value].Value; } return ""; } )); }
public string SendRandomMessage(Match match, ChatSessionInterface session) { return this._messages[rnd.Next(this._messages.Length)]; }
public string Execute(Match match, ChatSessionInterface session) { object result = method.Invoke(null, new object[] { match, session }); if (result == null) { return null; } return (string)result; }