!7
cmodule Cruddie > DynPrintLogAndEnabled {
!include #1027628 // HTTP+HTTPS servers
transient S salt;
transient WebChatBot chatBot;
S myLink() { ret "https://cruddie.site/"; }
S botLink() { ret "bot"; /*ret appendWithSlash(myLink(), "bot");*/ }
switchable S frontendModuleLibID = "#1027675/ChatBotFrontend";
switchable S backendModuleLibID = "#1027591/DynamicClassesMultiCRUD";
transient S cmdsSnippetID = #1027616;
start-thread {
File saltFile = secretProgramFile("salt.txt");
S salt = trimLoadTextFile(saltFile);
if (empty(salt)) {
saveTextFile(saltFile, salt = randomID());
print("Made salt");
}
dm_restartOnFieldChange enabled();
if (!enabled) ret;
chatBot = new WebChatBot;
chatBot.templateID = #1027690;
chatBot.baseLink = botLink();
chatBot.thoughtBot = new ThoughtBot;
chatBot.jsOnMsgHTML = "window.processNewStuff(src);";
chatBot.onBotShown = [[ {
var input = $("#status_message")[0];
console.log("input: " + input);
if (input)
new Awesomplete(input, {
minChars: 1,
list: ["I call you Fido", "What is your name?"]
});
} ]];
chatBot.afterHeading = "` + ('webkitSpeechRecognition' in window ? ` " + tag("button", "...", onclick := "startOrStop()", type := 'button, class := 'speechOnBtn, disabled := 'disabled, display := 'inline)
/*+ hjs([[console.log("Updating"); window.srUpdate();]])*/ + "` : ``) + `";
chatBot.moreStuff = "window.srUpdate();";
chatBot.start();
set redirectHttpToHttps;
start_webServers(javaxSecretDir("cruddie.site/keystore.p12"), "botcompany");
}
O webServe(S uri, SS params) {
printVars("webServe", +uri);
//S cookie = serveHttp_cookieHandling();
new Matches m;
S uri2 = appendSlash(uri);
if (startsWith(uri2, "/bot/", m))
ret chatBot.html("/" + m.rest(), params);
if (eq(uri, "/awesomplete.css")) ret serveWithContentType(loadSnippet(#2000595), "text/css");
if (eq(uri, "/awesomplete.js")) ret serveText(loadSnippet(#2000594));
S jsOnSpeech = "if (transcript != 'stop listening') window.submitAMsg(transcript);";
S sayScript = [[
window.processNewStuff = function(src) {
if ($("#speechResults") == null) return; // no speech
// we assume that webkit speech synthesis is present
// when there is webkit speech recognition
if (!bigOn) return; // not enabled
console.log("Got speech");
var match = src.match(/\d+/);
if (match == null) return;
if (src.match(/NEW DIALOG -->/)) return;
console.log("Got incremental");
var re = /bot-utterance">(.*?)]] // took out async
)
+ hbody(hfullcenter(
p(hsnippetimage(#1102905))
+ p("Chat bot is doing basic stuff. Documentation coming up.")
+ stats()
+ hWebKitSayFunction()
+ hjavascript(sayScript)
+ hSpeechRecognition(jsOnSpeech, true, "en-US", false,
noWebKit := p("Use Chrome if you want speech recognition"),
+speechUI)
+ hjs((S) chatBot.html("/", litmap(), returnJS := true))
)/*, onLoad := "startAwesomplete()"*/));
}
S cookieToCaseID(S cookie) {
ret md5(cookie + salt);
}
class Request {
S cookie, caseID;
S frontend, backend; // module IDs
*(S *cookie) {
caseID = cookieToCaseID(cookie);
frontend = dm_makeModuleWithParams_systemQ(frontendModuleLibID, +caseID);
backend = dm_makeModuleWithParams_systemQ(backendModuleLibID, +caseID);
dm_call(frontend, 'connectToBackend, backend);
dm_call(frontend, 'importCmdsFromSnippetIfEmpty, cmdsSnippetID);
printVars(+caseID, +backend);
}
}
class ThoughtBot {
new ThreadLocal request;
void setSession(S cookie, SS params) {
//session.set(uniq_sync(Session, +cookie));
request.set(new Request(cookie));
}
S initialMessage() {
//ret "Hello from module " + request->backend;
ret (S) dm_call(request->backend, 'answer, "stats");
}
S answer(S s) {
ret (S) dm_call(request->frontend, 'answer, s);
}
}
S stats() {
ret p(joinWithBR(
"Server temperature is " + dm_cpuTemperature(),
n2(numberOfCruddies(), "cruddie") + ", " + n2(vmBus_countResponses chatBotFrontend()) + " loaded",
));
}
int numberOfCruddies() {
ret countDirsInDir(getProgramDir(beforeSlash(frontendModuleLibID)));
}
}