Uses 911K of libraries. Click here for Pure Java version (16744L/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 | 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 ? ` " + 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 | "< Talk to me >", |
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 | + p("Server temperature is " + dm_cpuTemperature()) |
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 | } |
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: | #1027650 |
Snippet name: | Cruddie 2.2 [putting everything in one script] |
Eternal ID of this version: | #1027650/7 |
Text MD5: | daae0f1f905fad7014cad4925a4da5a3 |
Transpilation MD5: | 41ce56d92484d25ddb366213ef65bf37 |
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 13:25:46 |
Source code size: | 4510 bytes / 134 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 207 / 353 |
Version history: | 6 change(s) |
Referenced in: | [show references] |