!759 !include #1004810 // Concepts etc. //include #1004817 // externalize stuff (repeat less code) // What we think the user chose sclass OptionChosen extends Concept { new Ref userAnswer; S choice; } extend Answer { new Ref inResponseTo; } p { loadConcepts(); autoSaveConcepts(); action(); } static void processNewUserLine(UserLine ul) { S s = trim(ul.post.text); if (swic(s, "?id ")) { // assume "?id music" // action: ask user what he means by "music" S q = trim(s.substring(2)); print("ul= " + structure(ul)); Question question = findBackRefOfType(ul, Question.class); print("Question = " + struct(question)); // did we ask this yet? if not: do it if (question == null) { S text = "Uh... do you want some MUSIC or the MUSIC PLAYER?"; UserLine ulQ = postToChat(text); if (ulQ == null) { print("Couldn't post!"); ret; } question = new Question; question.userLine.set(ulQ); question = new Question(ul); } } } static void dbActions { // Scan through the answers we got from user and do something // with them. L userAnswers = conceptsOfType(UserAnswer.class); //printStructure("User Answers: ", userAnswers); for (UserAnswer ua : userAnswers) { // We only ask one question, so we know what this is about ELPost post = ua.userLine.get().post; //print("Processing: " + post.text); OptionChosen option = findBackRef(ua, OptionChosen.class); if (option == null) { // Interpret & save what user has said (-> "OptionChosen") option = new OptionChosen; option.userAnswer.set(ua); option.choice = getOption(trim(post.text)); // VERIFY new link assertSame(option, findBackRef(ua, OptionChosen.class)); } // Check if we answered already. Answer ourAnswer = findBackRef(option, Answer.class); if (ourAnswer != null) { //print(" We answered already."); } else { ourAnswer = new Answer; ourAnswer.inResponseTo.set(option); // VERIFY that we solved the condition above assertSame(ourAnswer, findBackRef(option, Answer.class)); S text = makeAnswer(option); UserLine ulA = postToChat(text); if (ulA == null) { print("Couldn't post!"); ret; } ourAnswer.userLine.set(ulA); } } } // learnable! [interpret user input] // returns "music", "player" or null (any unknown input) static S getOption(S s) { if (containsIgnoreCase(s, "player")) ret "player"; if (containsIgnoreCase(s, "music")) ret "music"; null; } static S makeAnswer(OptionChosen option) { if (eq(option.choice, "player")) ret "Current Music Player Is: " + snippetWithTitle("#1003779"); if (eq(option.choice, "music")) ret "Here's some polish music: " + snippetWithTitle("#1001093"); ret "I don't understand"; }