Uses 911K of libraries. Click here for Pure Java version (16683L/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 | |
26 | S speechUI = |
27 | hspan( // hdiv |
28 | "< Talk to me >", |
29 | id := 'speechResults, |
30 | display := "inline") + " " |
31 | + tag("button", "...", onclick := "startOrStop()", type := 'button, id := 'speechOnBtn, disabled := 'disabled, display := 'inline); |
32 | |
33 | chatBot = new WebChatBot; |
34 | chatBot.baseLink = botLink(); |
35 | chatBot.thoughtBot = new ThoughtBot; |
36 | |
37 | chatBot.jsOnMsgHTML = "window.processNewStuff(src);"; |
38 | chatBot.afterHeading = |
39 | //noWebKit := "[" + targetBlank("http://www.google.com/chrome", "Use Chrome") + " if you want speech recognition]", |
40 | speechUI; |
41 | |
42 | chatBot.templateHTML = () -> |
43 | chatBot.templateHTML_base().replace("//MORESTUFF//", "window.startSpeechRecog();"); |
44 | |
45 | chatBot.start(); |
46 | |
47 | set redirectHttpToHttps; |
48 | start_webServers(javaxSecretDir("cruddie.site/keystore.p12"), "botcompany"); |
49 | } |
50 | |
51 | O webServe(S uri, SS params) { |
52 | printVars("webServe", +uri); |
53 | //S cookie = serveHttp_cookieHandling(); |
54 | new Matches m; |
55 | if (startsWith(appendSlash(uri), "/bot/", m)) |
56 | ret chatBot.html("/" + m.rest(), params); |
57 | |
58 | S jsOnSpeech = "if (transcript != 'stop listening') window.submitAMsg(transcript);"; |
59 | |
60 | S sayScript = [[ |
61 | window.processNewStuff = function(src) { |
62 | if ($("#speechResults") == null) return; // no speech |
63 | // we assume that webkit speech synthesis is present |
64 | // when there is webkit speech recognition |
65 | if (!bigOn) return; // not enabled |
66 | console.log("Got speech"); |
67 | var match = src.match(/\d+/); |
68 | if (match == null) return; |
69 | if (src.match(/NEW DIALOG -->/)) return; |
70 | console.log("Got incremental"); |
71 | var re = /bot-utterance">(.*?)</g; |
72 | var match = re.exec(src); |
73 | var lastUtterance = null; |
74 | while (match != null) { |
75 | lastUtterance = match[1]; |
76 | match = re.exec(src); |
77 | } |
78 | // TODO: properly drop HTML tags/HTML-decode |
79 | if (lastUtterance) |
80 | say(lastUtterance); |
81 | }; |
82 | ]]; |
83 | |
84 | ret hhtml(hmobilefix() + hhead( |
85 | htitle("CRUDDIE - I manage your anything") |
86 | + hLoadJQuery2() |
87 | + hJsMakeCookie()) |
88 | + hbody(hfullcenter( |
89 | p(hsnippetimage(#1102905)) |
90 | + p("Chat bot is doing basic stuff. Documentation coming up.") |
91 | + p("Server temperature is " + dm_cpuTemperature()) |
92 | + hWebKitSayFunction() |
93 | + hjavascript(sayScript) |
94 | + hSpeechRecognition(jsOnSpeech, false, "en-US", false, |
95 | noWebKit := p("Use Chrome if you want speech recognition"), |
96 | /*+speechUI*/) |
97 | + hscript_src(botLink()) |
98 | ))); |
99 | } |
100 | |
101 | S cookieToCaseID(S cookie) { |
102 | ret md5(cookie + salt); |
103 | } |
104 | |
105 | class Request { |
106 | S cookie, caseID; |
107 | S frontend, backend; // module IDs |
108 | |
109 | *(S *cookie) { |
110 | caseID = cookieToCaseID(cookie); |
111 | frontend = dm_makeModuleWithParams_systemQ(frontendModuleLibID, +caseID); |
112 | backend = dm_makeModuleWithParams_systemQ(backendModuleLibID, +caseID); |
113 | dm_call(frontend, 'connectToBackend, backend); |
114 | dm_call(frontend, 'importCmdsFromSnippetIfEmpty, cmdsSnippetID); |
115 | printVars(+caseID, +backend); |
116 | } |
117 | } |
118 | |
119 | class ThoughtBot { |
120 | new ThreadLocal<Request> request; |
121 | |
122 | void setSession(S cookie, SS params) { |
123 | //session.set(uniq_sync(Session, +cookie)); |
124 | request.set(new Request(cookie)); |
125 | } |
126 | |
127 | S initialMessage() { |
128 | //ret "Hello from module " + request->backend; |
129 | ret (S) dm_call(request->backend, 'answer, "stats"); |
130 | } |
131 | |
132 | S answer(S s) { |
133 | ret (S) dm_call(request->frontend, 'answer, s); |
134 | } |
135 | } |
136 | } |
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: | #1027648 |
Snippet name: | Cruddie v2 [moving speech recognition button in chat popup, not really working] |
Eternal ID of this version: | #1027648/10 |
Text MD5: | f8a8fef642686f4042f8820241a00bcb |
Transpilation MD5: | bded49c5c99a2a09a9c4dc4c26b12540 |
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:58:23 |
Source code size: | 4456 bytes / 136 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 188 / 355 |
Version history: | 9 change(s) |
Referenced in: | [show references] |