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

132
LINES

< > BotCompany Repo | #1027649 // Cruddie v2.1 [just putting the button in the popup]

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

Uses 911K of libraries. Click here for Pure Java version (16710L/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 "bot"; /*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.afterHeading = "` + ('webkitSpeechRecognition' in window ? ` &nbsp; " + tag("button", "...", onclick := "startOrStop()", type := 'button, class := 'speechOnBtn, disabled := 'disabled, display := 'inline)
32  
      + hjs([[console.log("Updating"); window.srUpdate();]]) + "` : ``) + `";
33  
    
34  
    chatBot.start();
35  
    
36  
    set redirectHttpToHttps;
37  
    start_webServers(javaxSecretDir("cruddie.site/keystore.p12"), "botcompany");
38  
  }
39  
40  
  O webServe(S uri, SS params) {
41  
    printVars("webServe", +uri);
42  
    //S cookie = serveHttp_cookieHandling();
43  
    new Matches m;
44  
    if (startsWith(appendSlash(uri), "/bot/", m))
45  
      ret chatBot.html("/" + m.rest(), params);
46  
      
47  
    S jsOnSpeech = "if (transcript != 'stop listening') window.submitAMsg(transcript);";
48  
    
49  
    S sayScript = [[
50  
      window.processNewStuff = function(src) {
51  
        if ($("#speechResults") == null) return; // no speech
52  
        // we assume that webkit speech synthesis is present
53  
        // when there is webkit speech recognition
54  
        if (!bigOn) return; // not enabled
55  
        console.log("Got speech");
56  
        var match = src.match(/\d+/);
57  
        if (match == null) return;
58  
        if (src.match(/NEW DIALOG -->/)) return;
59  
        console.log("Got incremental");
60  
        var re = /bot-utterance">(.*?)</g;
61  
        var match = re.exec(src);
62  
        var lastUtterance = null;
63  
        while (match != null) {
64  
          lastUtterance = match[1];
65  
          match = re.exec(src);
66  
        }
67  
        // TODO: properly drop HTML tags/HTML-decode
68  
        if (lastUtterance)
69  
          say(lastUtterance);
70  
      };
71  
    ]];
72  
    
73  
    S speechUI =
74  
      hdiv(
75  
        "&lt; Talk to me &gt;",
76  
        id := 'speechResults,
77  
        style := "margin: 10px")
78  
      /*+ p(tag("button", "...", onclick := "startOrStop()", type := 'button, id := 'speechOnBtn, disabled := 'disabled))*/;
79  
      
80  
    ret hhtml(hmobilefix() + hhead(
81  
        htitle("CRUDDIE - I manage your anything")
82  
      + hLoadJQuery2()
83  
      + hJsMakeCookie())
84  
      + hbody(hfullcenter(
85  
        p(hsnippetimage(#1102905))
86  
      + p("Chat bot is doing basic stuff. Documentation coming up.")
87  
      + p("Server temperature is " + dm_cpuTemperature())
88  
      + hWebKitSayFunction()
89  
      + hjavascript(sayScript)
90  
      + hSpeechRecognition(jsOnSpeech, true, "en-US", false,
91  
        noWebKit := p("Use Chrome if you want speech recognition"),
92  
        +speechUI)
93  
      + hscript_src(botLink())
94  
      )));
95  
  }
96  
  
97  
  S cookieToCaseID(S cookie) {
98  
    ret md5(cookie + salt);
99  
  }
100  
  
101  
  class Request {
102  
    S cookie, caseID;
103  
    S frontend, backend; // module IDs
104  
    
105  
    *(S *cookie) {
106  
      caseID = cookieToCaseID(cookie);
107  
      frontend = dm_makeModuleWithParams_systemQ(frontendModuleLibID, +caseID);
108  
      backend = dm_makeModuleWithParams_systemQ(backendModuleLibID, +caseID);
109  
      dm_call(frontend, 'connectToBackend, backend);
110  
      dm_call(frontend, 'importCmdsFromSnippetIfEmpty, cmdsSnippetID);
111  
      printVars(+caseID, +backend);
112  
    }
113  
  }
114  
  
115  
  class ThoughtBot {
116  
    new ThreadLocal<Request> request;
117  
    
118  
    void setSession(S cookie, SS params) {
119  
      //session.set(uniq_sync(Session, +cookie));
120  
      request.set(new Request(cookie));
121  
    }
122  
    
123  
    S initialMessage() {
124  
      //ret "Hello from module " + request->backend;
125  
      ret (S) dm_call(request->backend, 'answer, "stats");
126  
    }
127  
    
128  
    S answer(S s) {
129  
      ret (S) dm_call(request->frontend, 'answer, s);
130  
    }
131  
  }
132  
}

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: #1027649
Snippet name: Cruddie v2.1 [just putting the button in the popup]
Eternal ID of this version: #1027649/10
Text MD5: 60ee002184d343b4521b95ab84deb710
Transpilation MD5: 4ce510db21306e562d4a28cd431b05d8
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 12:35:33
Source code size: 4423 bytes / 132 lines
Pitched / IR pitched: No / No
Views / Downloads: 195 / 447
Version history: 9 change(s)
Referenced in: [show references]