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

68
LINES

< > BotCompany Repo | #1001493 // Bot Ref Bot

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

Libraryless. Click here for Pure Java version (1511L/10K/36K).

// Bot Ref Bot knows which bots are online on some machine.

!747

m {
  static int interval = 4000;
  
  static L<ProgramScan.Program> bots = new ArrayList<ProgramScan.Program>();
  
  p {
    makeAndroid3("Bot Ref Bot.");
    
    while (true) {
      scan();
      sleep(interval);
    }
  }
  
  static void scan() {
    setBots(quickBotScan());
  }
  
  static synchronized void setBots(L<ProgramScan.Program> l) {
    bots = l;
  }
 
  // returned list is thread-safe (immutable even)
  static synchronized L<ProgramScan.Program> getBots() {
    ret bots;
  }
  
  static synchronized S answer(S s, L<S> history) {
    new Matches m;
    
    if (match3("please forward to bot *: *", s, m)) {
      S bot = m.unq(0);
      S line = m.unq(1);
      
      DialogIO io = findBotFromList(bot);
      if (io != null) {
        S hello = io.readLine();
        if (startsWithIgnoreCase(hello, bot)) {
          io.sendLine(line);
          S answer = io.readLine();
          io.close();
          ret answer;
        }
      }
        
      // default case
      print("not in list, fallback");
      ret sendToLocalBot(bot, line); // TODO: keep connections
    }
    
    ret useQuestionBot(s, history);
  }
  
  static S useQuestionBot(S s, L<S> history) {
    ret sendToLocalBot("Questions Bot", "how can question * to bot * be answered?", s, getProgramID());
  }
  
  static DialogIO findBotFromList(S botName) {
    for (ProgramScan.Program p : getBots())
      if (startsWithIgnoreCase(p.helloString, botName))
        ret talkTo(p.port);
    ret null;
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001493
Snippet name: Bot Ref Bot
Eternal ID of this version: #1001493/1
Text MD5: 61da5d3dcfcdb3711468b41f0edba539
Transpilation MD5: 9beca85b20143d07991631b3cb7dd845
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-21 17:33:18
Source code size: 1634 bytes / 68 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 674 / 715
Referenced in: #1001494 - Answer Questions Bot (developing)
#3000190 - Answer for stefanreich(>> t 20 questions)
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)