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

135
LINES

< > BotCompany Repo | #1002660 // Join Slack Bot (dev.)

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

Libraryless. Click here for Pure Java version (2138L/13K/46K).

1  
!752
2  
3  
static S team; // null if not on slack
4  
static S botName;
5  
6  
p {
7  
  load("team");
8  
  load("botName");
9  
}
10  
11  
synchronized answer {
12  
  if (matchAny(litlist("are we on slack", "are you on slack"), s))
13  
    if (joined())
14  
      ret "Yes, in team " + domain() + (hasToken() ? ", with token" : ", but need a token in " + getTokenFile().getAbsolutePath());
15  
    else
16  
      ret "No, type 'please join slack team nlbots' or similar to join a team.";
17  
      
18  
  if "please join slack team *" {
19  
    if (!master()) ret "You're not my master";
20  
    if (joined()) ret "Joined already (team " + domain() + "), type 'please leave slack' to leave.";
21  
    S teamName = dropSuffix(".slack.com", m.unq(0));
22  
    if (!checkTeamName(teamName)) ret "Bad team name: " + teamName;
23  
    team = teamName;
24  
    save("team");
25  
    if (hasToken())
26  
      ret "OK.";
27  
    else
28  
      ret "OK. Now I need a token for " + domain() + " in " + getTokenFile().getAbsolutePath();
29  
  }
30  
  
31  
  if "please leave slack" {
32  
    if (!master()) ret "You're not my master";
33  
    if (!joined()) ret "Not there anyway, type 'please join slack' to join.";
34  
    team = null;
35  
    save("team");
36  
    ret "OK, left slack team " + domain();
37  
  }
38  
  
39  
  if "slack bot name" {
40  
    if (!joined()) ret "I'm not on slack.com, type 'please join slack' to join it.";
41  
    ret botName == null ? botName : "No bot name - posting as user as defined by token";
42  
  }
43  
  
44  
  if "set slack bot name to *" {
45  
    S name = m.unq(0);
46  
    if (!isIdentifier(name)) ret "Oops, not an identifier.";
47  
    if (eq(name, "none")) name = null;
48  
    botName = name;
49  
    save("botName");
50  
    ret "OK, bot name saved.";
51  
  }
52  
  
53  
  if "list slack channels" exceptionToUser {
54  
    if (!joined()) ret "I'm not on slack.com, type 'please join slack' to join it.";
55  
    Map map = slack("channels.list", "exclude_archived", "1");
56  
    new L<S> l;
57  
    l.add("Channels list for " + domain());
58  
    l.add("");
59  
    int numJoined = 0;
60  
    L<Map> channels = cast map.get("channels");
61  
    for (Map c : channels) {
62  
      S name = getString(c, "name");
63  
      L members = cast c.get("members");
64  
      boolean in = isTrue(c.get("isMember"));
65  
      if (in) ++numJoined;
66  
      l.add("#" + name + ", members: " + l(members) + (in ? " (JOINED)" : ""));
67  
    }
68  
    l.add("");
69  
    l.add(numJoined == 0 ? "I have not joined any channels, please type 'join slack channel *' to do it." : "I am in " + n(numJoined, "channel") + ".");
70  
    ret slackSnippet(joinLines(l));
71  
  }
72  
  
73  
  if "join slack channel *" {
74  
    S name = dropPrefix("#", m.unq(0));
75  
    if (!isIdentifier(name)) ret "Bad channel name: " + name;
76  
    Map map = slack("channels.join", "name", name);
77  
    boolean ok = isTrue(map.get("ok"));
78  
    boolean alreadyIn = isTrue(map.get("already_in_channel"));
79  
    ret !ok ? "Error... " + structure(map) : (alreadyIn ? "OK (already in)" : "OK, joined " + quote(name) + "!");
80  
  }
81  
  
82  
  if "leave slack channel *" {
83  
    //TODO
84  
  }
85  
  
86  
  if "slack test post in *" exceptionToUser {
87  
    S name = dropPrefix("#", m.unq(0));
88  
    if (!isIdentifier(name)) ret "Bad channel name: " + name;
89  
    S channelID = slackGetChannelID(getToken(), name);
90  
    if (empty(channelID)) ret "Channel " + quote(name) + "not found";
91  
    Map map = slack("chat.postMessage",
92  
      "channel", channelID,
93  
      "text", "Test post from Eleutheria",
94  
      "username", botName,
95  
      "as_user", botName == null? "true" : null);
96  
    boolean ok = isTrue(map.get("ok"));
97  
    ret ok ? "OK!" : "Error... " + structure(map);
98  
  }
99  
}
100  
101  
static S domain() {
102  
  ret team == null ? null : team + ".slack.com";
103  
}
104  
105  
static boolean joined() {
106  
  ret team != null;
107  
}
108  
109  
static boolean checkTeamName(S teamName) {
110  
  ret isIdentifier(teamName);
111  
}
112  
113  
static File getTokenFile() {
114  
  ret secretProgramFile(team);
115  
}
116  
117  
static boolean hasToken() {
118  
  File file = getTokenFile();
119  
  if (!file.exists()) saveTextFile(file, "");
120  
  ret nempty(getToken());
121  
}
122  
123  
static S getToken() {
124  
  ret trim(loadTextFile(getTokenFile()));
125  
}
126  
127  
static Map slack(S cmd, S... params) {
128  
  ret slack(cmd, litmap(params));
129  
}
130  
131  
static Map slack(S cmd, Map<S, S> params) {
132  
  Map postData = new TreeMap(params);
133  
  postData.put("token", getToken());
134  
  ret jsonDecodeMap(doPost(postData, "https://slack.com/api/" + cmd));
135  
}

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: #1002660
Snippet name: Join Slack Bot (dev.)
Eternal ID of this version: #1002660/1
Text MD5: 95ff178f0025949f390c845be182ef61
Transpilation MD5: 38210fa16945e72488556d0e59d61494
Author: stefan
Category: eleu / javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-10 20:21:45
Source code size: 4304 bytes / 135 lines
Pitched / IR pitched: No / No
Views / Downloads: 730 / 1067
Referenced in: [show references]