// Answer Questions Bot knows how to answer questions to any other bot. !747 m { static new AList questions; !include #1001495 // AList static class Question { S botID; S questionPattern; new Code code; } static class Code { S snippetID; boolean blessed; // is it known to be safe to run? } p { readLocally("questions"); makeAndroid3("Questions Bot."); } static synchronized S answer(S s, L history) { new Matches m; if (match3("how can question * to bot * be answered?", s, m)) { S botID = formatSnippetID(m.unq(0)); S text = m.unq(1); L l = findQuestion(botID, text); if (l.isEmpty()) ret "I don't know."; Code code = l.get(0).code; ret format3("code: *, (" + (code.blessed ? "blessed" : "non-blessed") + ")", code.snippetID); } if (match3("add: bot *, question *, code snippet: *, blessed: *", s, m)) { new Question q; q.botID = m.fsi(0); q.questionPattern = m.unq(1); q.code.snippetID = m.fsi(2); q.code.blessed = m.bool(3); } ret standardQuery(s, "questions"); } static L findQuestion(S botID, S text) { new L l; // It is thread-safe to iterate over an AList :) for (Question q : questions) if (sameSnippetID(q.botID, botID) && match3(q.questionPattern, text)) l.add(q); ret l; } }