//Chatbot.lsl - a simple chatbot script for Second Life Objects. Modified from sources posted by Michael Olson. //Jack Aboutboul //jack@nyu.edu //MMM Spring 2007 //Last Updated 02-06-2007 key interlocutor = NULL_KEY; //FUNCTION DEFINITONS //A functuin which will return a value based on the existence of a substring in a string //Returns 1 if true; Returns 0 if false integer wordIsInSentence(string word, string sentence) { string lowerCaseWord = llToLower(word); string lowerCaseSentence = llToLower(sentence); integer index = llSubStringIndex(sentence, word); if (index >= 0) { return 1; } else { return 0; } } //A function that will return the day of the week string getDay(integer offset) { list weekdays = ["Thursday", "Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday"]; integer hours = llGetUnixTime()/3600; integer days = (hours + offset)/24; integer day_of_week = days%7; return llList2String(weekdays, day_of_week); } //DEFAULT STATE default { state_entry() { llOwnerSay("Hello! Chatbot starting up"); integer offset = -5; llSay(0, "Hello. Lovely " + getDay(offset) + " we are having."); llSay(0, "Have fun and enjoy your stay in SL. Just please use clean language. I despise profanity"); state waiting_for_hello; } state_exit() { //llOwnerSay("Chatbot leaving default state"); } } //IDLE STATE state waiting_for_hello { state_entry() { //lOwnerSay("Waiting for someone to say hello"); llListen(0, "", NULL_KEY, "" ); } listen(integer channel, string name, key id, string message ){ //llOwnerSay("I heard something. Checking to see if the sentence contains hello"); if (wordIsInSentence("hello", message) ) { interlocutor = id; llSay(0, "Oh hello! Nice to meet you! How are you doing? Good I hope."); state conversing; } } state_exit() { //llOwnerSay("Chatbot leaving waiting_for_hello state"); } } //CHATTING STATE state conversing { state_entry() { llListen( 0, "", NULL_KEY, "" ); } listen(integer channel, string name, key id, string message ){ if (interlocutor == id) { if ( wordIsInSentence("good", message) || wordIsInSentence("well", message) ) { llSay(0, "Great!! Glad to hear you're doing well."); state waiting_for_hello; } else if (wordIsInSentence("bad", message) ) { llSay(0, "Oh, I'm sorry to hear that. I'm sure things will lookup soon"); state waiting_for_hello; } else if (wordIsInSentence("fuck", message) ) { llSay(0, "Hey!! That's a pretty bad word. You should wash your mouth out with soap!"); } else if (wordIsInSentence("shit", message) ) { llSay(0, "If you want to talk like that you belong in the potty!"); } else { llSay(0, "Hmm... Not sure I understood that. Are you doing well or badly?"); } } } state_exit() { //llOwnerSay("Chatbot leaving conversing state"); llSay(0, "Well it was nice chatting with you. Talk to you later"); } }