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

161
LINES

< > BotCompany Repo | #1027691 // Cruddie [adding awesomplete, old]

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

Transpiled version (16843L) is out of date.

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

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: #1027691
Snippet name: Cruddie [adding awesomplete, old]
Eternal ID of this version: #1027691/4
Text MD5: e86879a3e67af2cbc9cdc39896450fba
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-05 13:45:20
Source code size: 5489 bytes / 161 lines
Pitched / IR pitched: No / No
Views / Downloads: 124 / 7894
Version history: 3 change(s)
Referenced in: [show references]