Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

136
LINES

< > BotCompany Repo | #1027648 // Cruddie v2 [moving speech recognition button in chat popup, not really working]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 911K of libraries. Click here for Pure Java version (16683L/96K).

!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");*/ }
  
  S frontendModuleLibID = "#1027602/ChatBotFrontend";
  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;
    
    S speechUI =
      hspan( // hdiv
        "&lt; Talk to me &gt;",
        id := 'speechResults,
        display := "inline") + " "
      + tag("button", "...", onclick := "startOrStop()", type := 'button, id := 'speechOnBtn, disabled := 'disabled, display := 'inline);
      
    chatBot = new WebChatBot;
    chatBot.baseLink = botLink();
    chatBot.thoughtBot = new ThoughtBot;
    
    chatBot.jsOnMsgHTML = "window.processNewStuff(src);";
    chatBot.afterHeading = 
        //noWebKit := "[" + targetBlank("http://www.google.com/chrome", "Use Chrome") + " if you want speech recognition]",
        speechUI;
    
    chatBot.templateHTML = () ->
      chatBot.templateHTML_base().replace("//MORESTUFF//", "window.startSpeechRecog();");

    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;
    if (startsWith(appendSlash(uri), "/bot/", m))
      ret chatBot.html("/" + m.rest(), params);
      
    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">(.*?)</g;
        var match = re.exec(src);
        var lastUtterance = null;
        while (match != null) {
          lastUtterance = match[1];
          match = re.exec(src);
        }
        // TODO: properly drop HTML tags/HTML-decode
        if (lastUtterance)
          say(lastUtterance);
      };
    ]];
    
    ret hhtml(hmobilefix() + hhead(
        htitle("CRUDDIE - I manage your anything")
      + hLoadJQuery2()
      + hJsMakeCookie())
      + hbody(hfullcenter(
        p(hsnippetimage(#1102905))
      + p("Chat bot is doing basic stuff. Documentation coming up.")
      + p("Server temperature is " + dm_cpuTemperature())
      + hWebKitSayFunction()
      + hjavascript(sayScript)
      + hSpeechRecognition(jsOnSpeech, false, "en-US", false,
        noWebKit := p("Use Chrome if you want speech recognition"),
        /*+speechUI*/)
      + hscript_src(botLink())
      )));
  }
  
  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> 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);
    }
  }
}

Author comment

Began life as a copy of #1027610

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027648
Snippet name: Cruddie v2 [moving speech recognition button in chat popup, not really working]
Eternal ID of this version: #1027648/10
Text MD5: f8a8fef642686f4042f8820241a00bcb
Transpilation MD5: bded49c5c99a2a09a9c4dc4c26b12540
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-29 11:58:23
Source code size: 4456 bytes / 136 lines
Pitched / IR pitched: No / No
Views / Downloads: 112 / 252
Version history: 9 change(s)
Referenced in: [show references]