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

38
LINES

< > BotCompany Repo | #1001926 // slackReadChannel

JavaX fragment (include)

1  
static class SlackMsg {
2  
  S user, botName; // one of these is going to be set
3  
  S userName; // looked up if user != null
4  
  S type, text, ts;
5  
  O reactions;
6  
  S channelID;
7  
}
8  
9  
static L<SlackMsg> slackReadChannel(S channelID, S token, int limit) {
10  
  ret slackReadChannel(channelID, token, limit, null);
11  
}
12  
13  
static L<SlackMsg> slackReadChannel(S channelID, S token, int limit, S oldest) {
14  
  S url = "https://slack.com/api/channels.history";
15  
  S postData = "token=" + urlencode(token) + "&channel=" + urlencode(channelID) + "&count=" + limit
16  
    + (oldest != null ? "&oldest=" + urlencode(oldest) : "");
17  
  S data = doPostWithTimeout(postData, url, slackSetTimeout_get());
18  
  Map map = cast jsonDecode(data);
19  
  if (!isTrue(map.get("ok")))
20  
    fail(structure(map));
21  
  List<Map> _msgs = cast map.get("messages");
22  
  new L<SlackMsg> msgs;
23  
  for (Map msg : reversedList(_msgs)) {
24  
    new SlackMsg m;
25  
    m.type = (S) msg.get("type");
26  
    m.user = (S) msg.get("user");
27  
    m.botName = (S) msg.get("username");
28  
    m.text = htmldecode((S) msg.get("text"));
29  
    m.ts = (S) msg.get("ts");
30  
    m.reactions = msg.get("reactions");
31  
    m.channelID = channelID;
32  
    m.userName = slackGetUserName(token, m.user);
33  
    msgs.add(m);
34  
    //print(structure(m));
35  
  }
36  
  //print(l(msgs) + " messages sucked from slack channel.");
37  
  ret msgs;
38  
}

Author comment

Began life as a copy of #1001907

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1001926
Snippet name: slackReadChannel
Eternal ID of this version: #1001926/1
Text MD5: dee1be7fa9d615a915fc135160347d22
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-04-18 22:42:30
Source code size: 1352 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 637 / 1206
Referenced in: [show references]