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

48
LINES

< > BotCompany Repo | #1002355 // User Name Bot (LIVE)

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

Libraryless. Click here for Pure Java version (2356L/16K/52K).

1  
!759
2  
3  
static class PerUser {
4  
  S dialogID;
5  
  S name;
6  
}
7  
8  
static new Map<S, PerUser> perUserMap; // key = dialogID
9  
static new ThreadLocal<PerUser> puCurrent;
10  
11  
p {
12  
  load("perUserMap");
13  
  makeBot("User Name Bot");
14  
}
15  
16  
synchronized answer {
17  
  if (!attn ()) null;
18  
  
19  
  // init per-user stuff
20  
  
21  
  S dialogID = getDialogID();
22  
  if (dialogID == null) ret null; // need a dialog id to proceed
23  
  PerUser pu = perUserMap.get(dialogID);
24  
  if (pu == null) {
25  
    pu = new PerUser;
26  
    pu.dialogID = dialogID;
27  
    printFormat("Created new dialog: *", dialogID);
28  
    perUserMap.put(dialogID, pu);
29  
  }
30  
  puCurrent.set(pu);
31  
  
32  
  if (matchStart("my name is", s, m) || matchStart("i am", s, m) || matchStart("you can call me", s, m) || matchStart("call me", s, m) || matchStart("they call me", s, m) || matchStart("my friends call me", s, m)) {
33  
    pu.name = unquote(m.rest().trim());
34  
    save("perUserMap");
35  
    ret "Hello " + pu.name + "!";
36  
  }
37  
  
38  
  if (match("who am i?", s) || match("eleu what's my name", s)) {
39  
    if (nempty(pu.name))
40  
      ret "You are " + pu.name + ".";
41  
    ret "I don't know, stranger. Who are you?";
42  
  }
43  
}
44  
45  
static synchronized S getUserName() {
46  
  PerUser pu = perUserMap.get(getDialogID());
47  
  ret pu == null ? null : pu.name;
48  
}

Author comment

Began life as a copy of #1002211

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002355
Snippet name: User Name Bot (LIVE)
Eternal ID of this version: #1002355/1
Text MD5: 0358f6b81da3e0924b0b654b386cce37
Transpilation MD5: 04e26d79d58b8ae7571d854f8a630b3b
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-20 00:01:27
Source code size: 1281 bytes / 48 lines
Pitched / IR pitched: No / No
Views / Downloads: 692 / 1501
Referenced in: [show references]