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

53
LINES

< > BotCompany Repo | #1017582 // AbstractTelegramThinkBot

JavaX fragment (include)

1  
abstract sclass AbstractTelegramThinkBot > DynPrintLog {
2  
  abstract void thinkAbout(S input);
3  
  
4  
  transient int lookback = 3; // old lines to grab
5  
  transient int maxCharsPerMessage = 2000;
6  
  transient int maxLinesPerMessage = 20;
7  
  transient Set<S> toSend;
8  
  transient LatestList<Map> recentHistory;
9  
10  
  // only called when not in test mode
11  
  start {
12  
    loadFunctions_preferCached();
13  
    dm_useLocallyCopiedMechLists();
14  
15  
    recentHistory = new LatestList(lookback);
16  
    recentHistory.addAll(map_pcall unstructureUnquote(dropLast(lastNLines(lookback, telegramLogFile()))));
17  
    watchStructureLog_plusLastLine(500, telegramLogFile(), voidfunc(final Map map) {
18  
      temp tempAfterwards(r { recentHistory.add(map) });
19  
      if (!eqGet(map, 'type, 'heard)) ret;
20  
      printStruct(map);
21  
22  
      try {
23  
        setThreadLocal((ThreadLocal) getOptMC('makeAndCall_initChild), voidfunc(Class c) {
24  
          copyFields(mc(), c, 'telegram_msg_tl, 'telegram_recentHistory_tl)
25  
        });
26  
        temp tempSetThreadLocal(telegram_msg_tl(), map);
27  
        temp tempSetThreadLocal(telegram_recentHistory_tl(), recentHistory);
28  
        
29  
        S s = getString(map, 'text);
30  
        toSend = new LinkedHashSet;
31  
        thinkAbout(s);
32  
        for (S out : takeFirst(3, getAndClearList(toSend))) {
33  
          Long chatID = (Long) _get(map, 'where);
34  
          S globalID = isTrue(_get(map, 'fromVoice))
35  
            ? telegram_postVoiceMessageToSendLog(out, "Andy", chatID)
36  
            : telegram_postToSendLog(out, chatID);
37  
          
38  
          if (globalID != null)
39  
            recentHistory.add(litmap(text := out, globalID));
40  
        }
41  
      } catch e {
42  
        printStackTrace(e);
43  
        postMessage(exceptionToStringShort(e));
44  
      }
45  
    });
46  
  }
47  
  
48  
  void postMessage(S s) {
49  
    s = shortenToMaxLinesAndChars(rtrim(s), maxLinesPerMessage, maxCharsPerMessage);
50  
    print("Sending: " + s);
51  
    toSend.add(s);
52  
  }
53  
}

Author comment

Began life as a copy of #1017568

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1017582
Snippet name: AbstractTelegramThinkBot
Eternal ID of this version: #1017582/30
Text MD5: 486e5f7042620b6031dc79fa7bb96af1
Author: stefan
Category: javax / telegram
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-08-29 12:44:08
Source code size: 1949 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 501 / 1127
Version history: 29 change(s)
Referenced in: [show references]