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).

!759

static class PerUser {
  S dialogID;
  S name;
}

static new Map<S, PerUser> perUserMap; // key = dialogID
static new ThreadLocal<PerUser> puCurrent;

p {
  load("perUserMap");
  makeBot("User Name Bot");
}

synchronized answer {
  if (!attn ()) null;
  
  // init per-user stuff
  
  S dialogID = getDialogID();
  if (dialogID == null) ret null; // need a dialog id to proceed
  PerUser pu = perUserMap.get(dialogID);
  if (pu == null) {
    pu = new PerUser;
    pu.dialogID = dialogID;
    printFormat("Created new dialog: *", dialogID);
    perUserMap.put(dialogID, pu);
  }
  puCurrent.set(pu);
  
  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)) {
    pu.name = unquote(m.rest().trim());
    save("perUserMap");
    ret "Hello " + pu.name + "!";
  }
  
  if (match("who am i?", s) || match("eleu what's my name", s)) {
    if (nempty(pu.name))
      ret "You are " + pu.name + ".";
    ret "I don't know, stranger. Who are you?";
  }
}

static synchronized S getUserName() {
  PerUser pu = perUserMap.get(getDialogID());
  ret pu == null ? null : pu.name;
}

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: 687 / 1494
Referenced in: [show references]