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

329
LINES

< > BotCompany Repo | #1018442 // New Woody Web Interface [dev.]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Compilation Failed (14599L/107K).

1  
!7
2  
3  
set flag NoAI.
4  
5  
sS imagesPath = "http://web-woody-lab.de/Botbilder/";
6  
sS botURL = "http://www.web-woody-lab.de/bot.php";
7  
sS defaultEmotion = "01", loadingEmotion = "14";
8  
static long conversationTimeOutMS = 1000*60*30;
9  
sbool enableDynamic = false, alwaysIframe = false;
10  
11  
static NewWoody bot;
12  
static volatile bool loaded;
13  
static new ThreadLocal<Conversation> conversation;
14  
static new LS imageNames;
15  
16  
static LS suggestions = tlft([[
17  
  Fragen Sie zum Beispiel, wie ein Chatbot installiert wird.
18  
  Fragen Sie zum Beispiel, ob Ihr Logo als Chatbot taugt.
19  
  Fragen Sie zum Beispiel, nach welchem Prinzip ein Chatbot agiert.
20  
  Fragen Sie zum Beispiel, wie ein Bot auf hre Website kommt.
21  
  Fragen Sie zum Beispiel, welche Ausbildung ein Chatbot braucht.
22  
  Fragen Sie zum Beispiel, was das hier soll.
23  
  Fragen Sie zum Beispiel, welchen Nutzen ein Chatbot bringt.
24  
  Fragen Sie zum Beispiel, ob ein Chatbot mehr Erfolg verspricht.
25  
  Fragen Sie zum Beispiel, wie wir es mit der Moral halten.
26  
  Fragen Sie zum Beispiel, was ein virtueller Mitarbeiter bringt.
27  
  Fragen Sie zum Beispiel, ob wir überhaupt Ahnung haben.
28  
  Fragen Sie zum Beispiel, wer hier die Chefs sind.
29  
]]);
30  
31  
sclass Msg semiauto {
32  
  long time
33  
  bool fromUser
34  
  S text
35  
  S newURL
36  
  
37  
  *() {}
38  
  *(bool *fromUser, S *text) { time = now(); }
39  
}
40  
41  
concept Conversation semiauto {
42  
  S cookie
43  
  new LL<Msg> oldDialogs
44  
  new L<Msg> msgs
45  
46  
  void add(Msg m) { msgs.add(m); change(); }
47  
48  
  //SS vars = new HashMap;
49  
  
50  
  NewWoody woody;
51  
}
52  
53  
p {
54  
  db();
55  
  thread { loadMe(); }
56  
  loadImages();
57  
}
58  
59  
svoid loadImages {
60  
  imageNames.clear();
61  
  for (S file : toLinesFullTrim(loadPage(imagesPath + "list.php")))
62  
    if (ewic(file, ".jpg")) {
63  
      print("Found image: " + file);
64  
      imageNames.add(file);
65  
    }
66  
}
67  
68  
static CtxContext newContext(S cookie) {
69  
  setConversation(cookie);
70  
  Msg m = last(conversation->msgs);
71  
  if (m != null && now() > m.time+conversationTimeOutMS) {
72  
    NEU();
73  
    ret ctxContext();
74  
  }
75  
  CtxContext ctx = woodyNewContext(bot);
76  
  copyFields(conversation!, ctx, 'activeRule, 'activeAnalyzer, 'vars);
77  
  ctx.conversationLength = l(conversation->msgs);
78  
  ret ctx;
79  
}
80  
81  
svoid doneContext {
82  
  copyFields(ctxContext(), conversation!, 'activeRule, 'activeAnalyzer);
83  
  //conversation->vars = dropKeysStartingWith("MATCH", ctxContext().vars);
84  
  ctxContext(null);
85  
}
86  
87  
sbool newUser;
88  
sS answer(S s) {
89  
  if (!isMainProgram()) null;
90  
  time {
91  
    newContext('default);
92  
    
93  
    if (eq(trim(s), "NEU")) {
94  
      NEU();
95  
      s = "";
96  
      newUser = true;
97  
    }
98  
    
99  
    if (newUser) {
100  
      ctxNewUser();
101  
      newUser = false;
102  
    } else {
103  
      print("Input: " + s);
104  
      ctxProcessInput(s);
105  
    }
106  
    
107  
    ctxDefaultOnEmptyOutput();
108  
    
109  
    for (S url : unnull(ctxContext().urlsToOpen)) print(">> " + url);
110  
    S out = ctxGetOutput();
111  
    print("Emotion: " + ctxGetLastEmotion(out));
112  
    doneContext();
113  
    ret out;
114  
  }
115  
}
116  
117  
svoid selfTest {}
118  
119  
svoid loadMe autosemi {
120  
  bot = new NewWoody;
121  
122  
  // Bot is loaded now.
123  
  if (isMainProgram()) selfTest()
124  
  
125  
  // Done loading. Make console bot if main program
126  
  
127  
  loaded = true
128  
  if (isMainProgram()) makeBot(
129  
}
130  
131  
svoid setConversation(S cookie) {
132  
  conversation.set(uniq_sync(Conversation, +cookie));
133  
}
134  
135  
svoid clearConversation(S cookie) {
136  
  deleteConcepts(Conversation, +cookie);
137  
}
138  
 
139  
svoid processActiveRule() {
140  
  ctxProcessRule(ctxContext().activeRule);
141  
}
142  
143  
// for webChatBotLogsHTML
144  
static S formatDateAndTime(long time) {
145  
  ret formatGermanDateAndTime(time);
146  
}
147  
148  
html {
149  
  registerVisitor();
150  
  
151  
  lock dbLock();
152  
  
153  
  if (eq(uri, "/logs"))
154  
    ret webChatBotLogsHTML2(relativeRawBotLink(programID(), "logs"), params);
155  
  
156  
  S referer = subBot_referer();
157  
  S message = trim(params.get('message));
158  
  S text = "Programm lädt,\nbin gleich wieder da.";
159  
  S page = params.get("uri");
160  
  bool _loaded = loaded;
161  
  Msg msg = null, msgMade = null;
162  
  if (_loaded) {
163  
    S cookie = params.get("session");
164  
    if (empty(cookie)) cookie = cookieSent();
165  
    if (empty(page)) page = referer;
166  
    newContext(cookie);
167  
    Conversation conv = conversation!;
168  
    
169  
    print("Conv ID: " + conv.id + ", cookie: " + conv.cookie + ", msgs: " + structForUser(conv.msgs));
170  
    
171  
    if (eq(message, "NEU")) {
172  
      NEU();
173  
      message = null;
174  
    }
175  
176  
    if (empty(conv.msgs)) {
177  
      ctxNewUser();
178  
      conv.add(msgMade = botAnswerMsg());
179  
    }
180  
    
181  
    // empty user input
182  
    if (eq(message, "")) message = "-";
183  
      
184  
    if (nempty(message) && !lastUserMessageWas(conv, message))
185  
      conv.add(new Msg(true, message));
186  
  
187  
    if (nempty(conv.msgs) && last(conv.msgs).fromUser) {
188  
      L<S> buttons = null;
189  
      pcall {
190  
        ctxProcessInput(last(conv.msgs).text);
191  
      }
192  
      conv.add(msgMade = botAnswerMsg());
193  
    }
194  
    
195  
    doneContext();
196  
    
197  
    msg = last(conv.msgs);
198  
    text = msg.text;
199  
  }
200  
  
201  
  S ping = params.get('ping);
202  
  bool dynamic = eq(ping, 'dynamic);
203  
  if (!dynamic && nempty(ping)) ret "";
204  
  
205  
  S textHtml = htmlEncode_nlToBr(dropAllTags(ctxDropEmotions(text)));
206  
  bool include = nempty(params.get('include));
207  
  S scrollCmd = "setTimeout(function() { document.body.scrollTop = 167; }, 200);";
208  
  // "document.getElementById('theinput').scrollIntoView(false);"
209  
  //S inner = himg("http://www.web-woody-lab.de/IMG_1460%20herz.jpg", width := 180, onload := scrollCmd);
210  
  S emo;
211  
  if (!_loaded) emo = loadingEmotion;
212  
  else emo = or2(ctxGetLastEmotion(text), defaultEmotion);
213  
  S imageName = firstStartingWith(imageNames, emo);
214  
  //print("emo=" + emo + ", imageName=" + imageName);
215  
  S inner = "";
216  
  if (imageName != null) {
217  
    S img = imagesPath + imageName;
218  
    inner = himg(img, width := 180, height := 225/*, onload := scrollCmd*/);
219  
  }
220  
  
221  
  if (!_loaded)
222  
    inner += p(textHtml, style := "text-align: center");
223  
  else {
224  
    // loaded
225  
    S suggestion = "";
226  
    if (l(conversation->msgs) <= 1)
227  
      suggestion = random(suggestions);
228  
    /*else
229  
      suggestion = sfu(collect(conversation->msgs, text));*/
230  
      
231  
    bool redirect = msg != null && nempty(msg.newURL) && msg == msgMade;
232  
    inner += p(textHtml);
233  
    inner +=
234  
      redirect
235  
        ? hjs_redirectWindowOrParent(msg.newURL.replaceAll("\\.html$", ".php"))
236  
        // no redirect
237  
        : (!enableDynamic ? "" : loadJQuery2()
238  
        + jsJoinHack()
239  
        + hjavascript([[
240  
          function submitDynamic() {
241  
            var url = #BOTURL# + "?ping=dynamic&message="
242  
              + encodeURIComponent(document.getElementById('theinput').value);
243  
            alert(url);
244  
            /*$.get(url, function(src) {
245  
              alert(src);
246  
              $("#botdiv").html(src);
247  
            }).fail(function() {
248  
              alert("error");
249  
            });*/
250  
  $.ajax({
251  
    type: "GET",
252  
    url: url,
253  
    success: function (response) {
254  
        $('#botdiv').html(response.responseText);
255  
    },
256  
    error: function (jqXHR, exception, error) {
257  
        var msg = '';
258  
        if (jqXHR.status === 0) {
259  
            msg = '0 ' + jqXHR.join();
260  
        } else if (jqXHR.status == 404) {
261  
            msg = 'Requested page not found. [404]';
262  
        } else if (jqXHR.status == 500) {
263  
            msg = 'Internal Server Error [500].';
264  
        } else if (exception === 'parsererror') {
265  
            msg = 'Requested JSON parse failed.';
266  
        } else if (exception === 'timeout') {
267  
            msg = 'Time out error.';
268  
        } else if (exception === 'abort') {
269  
            msg = 'Ajax request aborted.';
270  
        } else {
271  
            msg = 'Uncaught Error.\n' + jqXHR.responseText;
272  
        }
273  
        $('#botdiv').html(msg);
274  
    }
275  
  });
276  
}
277  
        ]].replace("#BOTURL#", jsQuote(botURL))))
278  
        + hformPOST(
279  
          htextinput("message", style := "width: 180px", autofocus := 1, id := "theinput", onkeydown := enableDynamic ? "if (event.keyCode != 13) return true; submitDynamic(); return false;" : null)
280  
        + (!_loaded ? "" : p(nempty(suggestion)
281  
          ? htmlencode(suggestion)
282  
          : ahref("#", htmlencode("Neues Gespräch ") + "&nbsp;" + b("X"), style := "font-size: 14px; color: #299397", onClick := "theinput.value = 'NEU'; document.getElementById('theform').submit(); return false;")
283  
          , style := "color: #299397; font-style: italic" + (nempty(suggestion) ? "" : "; text-align: right")))
284  
          , "accept-charset" := "UTF-8", id := "theform");
285  
  }
286  
      
287  
  S loadingRefresher = _loaded ? "" :
288  
    //hrefresh(5, include ? "" : fullSelfLink(params));
289  
    hjs_reloadWindowIn(5);
290  
      
291  
  if (dynamic) ret loadingRefresher + inner;
292  
  if (include)
293  
    if (alwaysIframe)
294  
      ret tag("iframe", "", src := rawBotLink_newServer(), width := 180, height := 600, frameBorder := 0);
295  
    else
296  
      ret loadingRefresher + div(inner, id := 'botdiv);
297  
  
298  
  // deliver iframe contents
299  
  ret hhtml(hhead(
300  
    loadingRefresher
301  
    + hcss([[
302  
      body { background-color:white; }
303  
      p { font:normal 14px Arial, Helvetica; color:black; line-height:18px; }
304  
    ]]))
305  
    + hbody(inner, style := "margin: 0px", onload := "document.getElementById('theinput').focus();");
306  
}
307  
308  
static bool lastUserMessageWas(Conversation conv, S message) {
309  
  Msg m = last(conv.msgs);
310  
  ret m != null && m.fromUser && eq(m.text, message);
311  
}
312  
313  
static Msg botAnswerMsg() {
314  
  // S reply = "Ich habe Sie leider nicht verstanden";
315  
  ctxDefaultOnEmptyOutput();
316  
  S output = ctxGetOutput();
317  
  S reply = output;
318  
  Msg _msg = new Msg(false, reply);
319  
  _msg.newURL = last(ctxContext().urlsToOpen);
320  
  ret _msg;
321  
}
322  
323  
svoid NEU {
324  
  Conversation conv = conversation!;
325  
  conv.oldDialogs.add(conv.msgs);
326  
  cset(conv, msgs := new L);
327  
  cset(conv, woody := new NewWoody);
328  
  newContext(conv.cookie);
329  
}

Author comment

Began life as a copy of #1008855

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1018442
Snippet name: New Woody Web Interface [dev.]
Eternal ID of this version: #1018442/6
Text MD5: 74a167f09542de7b09ebb111c8e0602a
Transpilation MD5: c8cb7de788d37a654e20e310bd49ec28
Author: stefan
Category: javax / a.i.
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-09-30 23:27:15
Source code size: 9844 bytes / 329 lines
Pitched / IR pitched: No / No
Views / Downloads: 411 / 711
Version history: 5 change(s)
Referenced in: [show references]