-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslackLib.js
More file actions
73 lines (61 loc) · 2.2 KB
/
slackLib.js
File metadata and controls
73 lines (61 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Slack Library
// Load the library
var slack = require("@slack/client");
// let us access the settings
var settings = require("./settings.js").module("settings");
// get the API token from the settings
const botTokenId = settings.get("bot-token");
// return it to calling context
exports.ClientEvents = slack.CLIENT_EVENTS;
exports.RtmEvents = slack.RTM_EVENTS;
exports.WebApi = new slack.WebClient(botTokenId);
// Connect to slack and return the RTM object.
exports.connect = function () {
// prepare the connection to slack
var rtm = new slack.RtmClient(botTokenId);
// make sure we log a debug message on a successful connection
rtm.on(exports.ClientEvents.RTM.AUTHENTICATED, function () {
if (settings.debugMode) {
console.info("Successfully Connected.");
}
});
// connect to slack
rtm.start();
return rtm;
};
// get the channel ID for a user's Direct Messages with the bot
exports.getDMChannel = function (userId, callback) {
exports.WebApi.im.open(userId, function (err, info) {
if (err && settings.debugMode)
console.error("Error in GetDMChannel(" + userId + ")\n", err);
else
callback(info.channel.id);
});
};
// get the user's name from their ID
exports.getUserName = function (userId, callback) {
exports.WebApi.users.info(userId, function (err, info) {
if (err && settings.debugMode)
console.error("Error in getUserName(" + userId + ")\n", err);
else
callback(info.user.name);
});
};
// get the channel's name from the ID
exports.getChannelName = function (channelId, callback) {
exports.WebApi.channels.info(channelId, function (err, info) {
if (err && settings.debugMode)
console.error("Error in getChannelName(" + channelId + ")" + err);
else
callback(info.channel.name);
});
};
// get the channel's name from the ID
exports.getGroupName = function (channelId, callback) {
exports.WebApi.groups.info(channelId, function (err, info) {
if (err && settings.debugMode)
console.error("Error in getGroupName(" + channelId + ")" + err);
else
callback(info.group.name);
});
};