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

145
LINES

< > BotCompany Repo | #1027689 // Cruddie [backup before awesomplete]

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

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

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

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: #1027689
Snippet name: Cruddie [backup before awesomplete]
Eternal ID of this version: #1027689/1
Text MD5: 17a7e98ad824c1a5084dd37f7a163e91
Transpilation MD5: aa38553a8feee7a5647fb0012f592e42
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-30 20:00:03
Source code size: 4818 bytes / 145 lines
Pitched / IR pitched: No / No
Views / Downloads: 121 / 168
Referenced in: [show references]