!7 sO thoughtBot; sclass Msg { long time; bool fromUser; S text; *() {} *(bool *fromUser, S *text) { time = now(); } } concept Conversation { S cookie; new L msgs; void add(Msg m) { msgs.add(m); change(); } } p { db(); thoughtBot = runDependent(#1008534); } html { registerVisitor(); if (eq(uri, "/logs")) ret (S) withDBLock(func { new L l; for (Conversation conv : sortByCalculatedFieldDesc(list(Conversation), func(Conversation c) { empty(c.msgs) ? c.created : last(c.msgs).time })) { new L lc; for (Msg m : conv.msgs) lc.add(htmlencode((m.fromUser ? "> " : "< ") + m.text)); l.add(conv.id + ul(lc)); } ret h3_htitle("Chat Logs") + ul(l); }); fS cookie = cookieSent(); final new Var _conv; S message = trim(params.get("message")); withDBLock(r { _conv.set(uniq(Conversation, +cookie)); }); Conversation conv = _conv!; if (match("clear", message)) { conv.msgs.clear(); conv.change(); message = null; } if (empty(conv.msgs)) conv.add(new Msg(false, initialMessage())); if (nempty(message) && !lastUserMessageWas(conv, message)) conv.add(new Msg(true, message)); if (nempty(conv.msgs) && last(conv.msgs).fromUser) { S reply = "I'm sorry, I don't understand"; pcall { reply = or2(makeReply(last(conv.msgs).text), reply); } conv.add(new Msg(false, reply)); } S html = loadSnippet(#1008315); new StringBuilder buf; for (Msg m : conv.msgs) if (m.fromUser || neq(m.text, "-")) appendMsg(buf, m.fromUser ? "Esteemed Customer" : "Cubes Bot", formatTime(m.time), htmlencode(m.text), !m.fromUser); html = html.replace("$HEADING", "Stefan's Chat Bot"); html = html.replace("", str(buf)); html = hreplaceTitle(html, "Stefan's Chat Bot"); html = hmobilefix(html); ret html; } svoid appendMsg(StringBuilder buf, S name, S time, S text, bool bot) { S imgLink = snippetImgLink(bot ? #1008323 : #1008359); buf.append([[
$NAME
message user image
$TEXT
$TIME
]].replace("$IMG", imgLink) .replace("$NAME", name) .replace("$TIME", time) .replace("$TEXT", text)); } svoid appendDate(StringBuilder buf, S date) { buf.append([[
DATE
]].replace("DATE", date)); } sS initialMessage() { ret (S) call(thoughtBot, "initialMessage"); } static bool lastUserMessageWas(Conversation conv, S message) { Msg m = last(conv.msgs); ret m != null && m.fromUser && eq(m.text, message); } sS makeReply(S message) { call(thoughtBot, "setSession", cookieSent()); ret callStaticAnswerMethod(thoughtBot, message); } sS formatTime(long time) { ret formatGMT_24(time); }