private static string SmallTalk(string text, IChatBotUser to) { string[] responses = null; bool isToEveryone = to.Name.ToLower().StartsWith("everyone") || to.UserId == 0; var smallTalk = cfg.SmallTalkSequences; if (smallTalk != null) { foreach (var word in text.GetWordsInSentence()) { if (smallTalk.TryGetValue(word.ToLower(), out responses)) { break; } } } var response = ChooseRandomResponse(responses, to); RememberThatISaid(response, to); // Add one-time message if needed if ((!isToEveryone) && ((response == null) || ((response != null) && (!response.StartsWith("{"))))) { response = ExpandOneTimeMsg(response, to); } return(response); }
private static void RememberThatISaid(string text, IChatBotUser to) { if (text == null) { return; } if (text.StartsWith("{") || text.StartsWith("/")) { // Don't remember these... return; } HashSet <string> thingsISaid = null; if (!ThingsISaidToUserId.TryGetValue(to.UserId, out thingsISaid)) { thingsISaid = new HashSet <string>(); ThingsISaidToUserId[to.UserId] = thingsISaid; } thingsISaid.Add(text); if (!ThingsISaidToName.TryGetValue(to.Name, out thingsISaid)) { thingsISaid = new HashSet <string>(); ThingsISaidToName[to.Name] = thingsISaid; } thingsISaid.Add(text); }
private static string OneTimeHi(string text, IChatBotUser to) { string response = null; string key = "_onetimehi_"; if (cfg.OneTimeHiSequences == null) { return(null); } // Try to give a specific response foreach (var word in text.GetWordsInSentence()) { if (cfg.OneTimeHiSequences.TryGetValue(word.ToLower(), out response)) { break; } } if (DidISay(key, to)) { // I already said it return(null); } // No specific response? Pick something at random if (response == null) { response = ChooseRandomResponse(cfg.OneTimeHiSequences.Values.ToArray <string>(), to); } RememberThatISaid(key, to); return(response); }
private static string ChooseRandomResponse(string[] responses, IChatBotUser to) { // TBD: Track things we've said so we don't say them more than once. if (responses == null) { return(null); } // Don't repeat things I've already said HashSet <string> thingsISaid = null; HashSet <string> allThingsISaid = new HashSet <string>(); if ((to.UserId != 0) && ThingsISaidToUserId.TryGetValue(to.UserId, out thingsISaid)) { allThingsISaid.UnionWith(thingsISaid); } if (ThingsISaidToName.TryGetValue(to.Name, out thingsISaid)) { allThingsISaid.UnionWith(thingsISaid); } HashSet <string> responseHS = new HashSet <string>(responses); responseHS.ExceptWith(allThingsISaid); if (responseHS.Count == 0) { return(null); } // Choose something random from somehing I haven't already said return(responseHS.RandomElement()); }
public string Converse(string input, IChatBotUser from) { string response = null; lock (SettingsLock) { response = RandomTalk(input); } return(response); }
/// <summary> /// Takes input and returns conversational output from the chatbot. /// It is assumed that the chatbot will prompt for input with "] ". /// </summary> public string Converse(string input, IChatBotUser from) { if (!bStarted) { return(null); } hostApp.Log(LogType.DBG, "chatBot > {0}", repr(input)); p.StandardInput.WriteLine(input); // TBD: Implement some sort of timeout or something so we don't hang forever if things go wonky string line = p.StandardOutput.ReadLine(); hostApp.Log(LogType.DBG, "chatBot < {0}", repr(line)); return(line); }
private static string ExpandOneTimeMsg(string text, IChatBotUser to) { string key = "_onetimemsg_"; string msg = null; if (!DidISay(key, to) && (cfg.OneTimeMsg != null)) { if (text == null) { text = cfg.OneTimeMsg; } else { text += " " + cfg.OneTimeMsg; } RememberThatISaid(key, to); } return(text); }
private static bool DidISay(string text, IChatBotUser to) { HashSet <string> thingsSaid; if ((to.UserId != 0) && ThingsISaidToUserId.TryGetValue(to.UserId, out thingsSaid)) { if (thingsSaid.Contains(text)) { return(true); } } if (ThingsISaidToName.TryGetValue(to.Name, out thingsSaid)) { if (thingsSaid.Contains(text)) { return(true); } } return(false); }
public string Converse(string text, IChatBotUser from) { string response = null; lock (SettingsLock) { var justSayHi = text == "_onetimehi_"; if (justSayHi) { // Use time-appropriate greeting text = "morning"; } // Say hi if we haven't already var hi = OneTimeHi(text, from); if (justSayHi) { return(hi); } var smalltalk = SmallTalk(text, from); if ((hi != null) && (smalltalk != null)) { response = hi + " " + smalltalk; } else if (smalltalk != null) { response = smalltalk; } else { response = hi; } } return(response); }