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

129
LINES

< > BotCompany Repo | #1027647 // Cruddie [backup with speech recognition on bottom]

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

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

1  
!7
2  
3  
cmodule Cruddie > DynPrintLogAndEnabled {
4  
  !include #1027628 // HTTP+HTTPS servers
5  
  
6  
  transient S salt;
7  
  transient WebChatBot chatBot;
8  
  
9  
  S myLink() { ret "https://cruddie.site/"; }
10  
  S botLink() { ret appendWithSlash(myLink(), "bot"); }
11  
  
12  
  S frontendModuleLibID = "#1027602/ChatBotFrontend";
13  
  S backendModuleLibID = "#1027591/DynamicClassesMultiCRUD";
14  
  transient S cmdsSnippetID = #1027616;
15  
16  
  start-thread {
17  
    File saltFile = secretProgramFile("salt.txt");
18  
    S salt = trimLoadTextFile(saltFile);
19  
    if (empty(salt)) {
20  
      saveTextFile(saltFile, salt = randomID());
21  
      print("Made salt"); 
22  
    }
23  
    dm_restartOnFieldChange enabled();
24  
    if (!enabled) ret;
25  
    chatBot = new WebChatBot;
26  
    chatBot.baseLink = botLink();
27  
    chatBot.thoughtBot = new ThoughtBot;
28  
    
29  
    chatBot.jsOnMsgHTML = "window.processNewStuff(src);";
30  
    
31  
    chatBot.start();
32  
    
33  
    set redirectHttpToHttps;
34  
    start_webServers(javaxSecretDir("cruddie.site/keystore.p12"), "botcompany");
35  
  }
36  
37  
  O webServe(S uri, SS params) {
38  
    printVars("webServe", +uri);
39  
    //S cookie = serveHttp_cookieHandling();
40  
    new Matches m;
41  
    if (startsWith(appendSlash(uri), "/bot/", m))
42  
      ret chatBot.html("/" + m.rest(), params);
43  
      
44  
    S jsOnSpeech = "if (transcript != 'stop listening') window.submitAMsg(transcript);";
45  
    
46  
    S sayScript = [[
47  
      window.processNewStuff = function(src) {
48  
        if ($("#speechResults") == null) return; // no speech
49  
        // we assume that webkit speech synthesis is present
50  
        // when there is webkit speech recognition
51  
        if (!bigOn) return; // not enabled
52  
        console.log("Got speech");
53  
        var match = src.match(/\d+/);
54  
        if (match == null) return;
55  
        if (src.match(/NEW DIALOG -->/)) return;
56  
        console.log("Got incremental");
57  
        var re = /bot-utterance">(.*?)</g;
58  
        var match = re.exec(src);
59  
        var lastUtterance = null;
60  
        while (match != null) {
61  
          lastUtterance = match[1];
62  
          match = re.exec(src);
63  
        }
64  
        // TODO: properly drop HTML tags/HTML-decode
65  
        if (lastUtterance)
66  
          say(lastUtterance);
67  
      };
68  
    ]];
69  
    
70  
    S speechUI =
71  
      hdiv(
72  
        "&lt; Talk to me &gt;",
73  
        id := 'speechResults,
74  
        style := "margin: 10px")
75  
      + p(tag("button", "...", onclick := "startOrStop()", type := 'button, id := 'speechOnBtn, disabled := 'disabled));
76  
      
77  
    ret hhtml(hmobilefix() + hhead(
78  
        htitle("CRUDDIE - I manage your anything")
79  
      + hLoadJQuery2()
80  
      + hJsMakeCookie())
81  
      + hbody(hfullcenter(
82  
        p(hsnippetimage(#1102905))
83  
      + p("Chat bot is doing basic stuff. Documentation coming up.")
84  
      + p("Server temperature is " + dm_cpuTemperature())
85  
      + hWebKitSayFunction()
86  
      + hjavascript(sayScript)
87  
      + hSpeechRecognition(jsOnSpeech, true, "en-US", false,
88  
        noWebKit := p("Use Chrome if you want speech recognition"),
89  
        +speechUI)
90  
      + hscript_src(botLink())
91  
      )));
92  
  }
93  
  
94  
  S cookieToCaseID(S cookie) {
95  
    ret md5(cookie + salt);
96  
  }
97  
  
98  
  class Request {
99  
    S cookie, caseID;
100  
    S frontend, backend; // module IDs
101  
    
102  
    *(S *cookie) {
103  
      caseID = cookieToCaseID(cookie);
104  
      frontend = dm_makeModuleWithParams_systemQ(frontendModuleLibID, +caseID);
105  
      backend = dm_makeModuleWithParams_systemQ(backendModuleLibID, +caseID);
106  
      dm_call(frontend, 'connectToBackend, backend);
107  
      dm_call(frontend, 'importCmdsFromSnippetIfEmpty, cmdsSnippetID);
108  
      printVars(+caseID, +backend);
109  
    }
110  
  }
111  
  
112  
  class ThoughtBot {
113  
    new ThreadLocal<Request> request;
114  
    
115  
    void setSession(S cookie, SS params) {
116  
      //session.set(uniq_sync(Session, +cookie));
117  
      request.set(new Request(cookie));
118  
    }
119  
    
120  
    S initialMessage() {
121  
      //ret "Hello from module " + request->backend;
122  
      ret (S) dm_call(request->backend, 'answer, "stats");
123  
    }
124  
    
125  
    S answer(S s) {
126  
      ret (S) dm_call(request->frontend, 'answer, s);
127  
    }
128  
  }
129  
}

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: #1027647
Snippet name: Cruddie [backup with speech recognition on bottom]
Eternal ID of this version: #1027647/1
Text MD5: 9d4bfb98ecfaa033d0f607b6695b2c5f
Transpilation MD5: 8d712a312edebf6ee15395889c18c326
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:33:43
Source code size: 4102 bytes / 129 lines
Pitched / IR pitched: No / No
Views / Downloads: 163 / 231
Referenced in: [show references]