You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
6.4 KiB
228 lines
6.4 KiB
|
2 weeks ago
|
// This is a JS File that was renamed to TS so it won't lose its git history when converted to TS
|
||
|
|
// TODO: Remove the following lint/ts instructions when the file gets properly converted
|
||
|
|
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
|
||
|
|
// @ts-nocheck
|
||
|
3 years ago
|
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
|
||
|
7 years ago
|
|
||
|
|
export class SlackAPI {
|
||
|
2 years ago
|
constructor(apiOrBotToken) {
|
||
|
|
this.token = apiOrBotToken;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async getChannels(cursor = null) {
|
||
|
6 years ago
|
let channels = [];
|
||
|
3 years ago
|
const request = await fetch('https://slack.com/api/conversations.list', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
7 years ago
|
params: {
|
||
|
|
types: 'public_channel',
|
||
|
6 years ago
|
exclude_archived: true,
|
||
|
|
limit: 1000,
|
||
|
2 years ago
|
...(cursor && { cursor }),
|
||
|
7 years ago
|
},
|
||
|
|
});
|
||
|
3 years ago
|
const response = await request.json();
|
||
|
6 years ago
|
|
||
|
3 years ago
|
if (response && response && Array.isArray(response.channels) && response.channels.length > 0) {
|
||
|
|
channels = channels.concat(response.channels);
|
||
|
|
if (response.response_metadata && response.response_metadata.next_cursor) {
|
||
|
|
const nextChannels = await this.getChannels(response.response_metadata.next_cursor);
|
||
|
6 years ago
|
channels = channels.concat(nextChannels);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return channels;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async getGroups(cursor = null) {
|
||
|
6 years ago
|
let groups = [];
|
||
|
3 years ago
|
const request = await fetch('https://slack.com/api/conversations.list', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
7 years ago
|
params: {
|
||
|
|
types: 'private_channel',
|
||
|
6 years ago
|
exclude_archived: true,
|
||
|
|
limit: 1000,
|
||
|
2 years ago
|
...(cursor && { cursor }),
|
||
|
7 years ago
|
},
|
||
|
|
});
|
||
|
3 years ago
|
const response = await request.json();
|
||
|
6 years ago
|
|
||
|
3 years ago
|
if (response && response && Array.isArray(response.channels) && response.channels.length > 0) {
|
||
|
|
groups = groups.concat(response.channels);
|
||
|
|
if (response.response_metadata && response.response_metadata.next_cursor) {
|
||
|
|
const nextGroups = await this.getGroups(response.response_metadata.next_cursor);
|
||
|
6 years ago
|
groups = groups.concat(nextGroups);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return groups;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async getRoomInfo(roomId) {
|
||
|
|
const request = await fetch(`https://slack.com/api/conversations.info`, {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
7 years ago
|
params: {
|
||
|
|
channel: roomId,
|
||
|
|
include_num_members: true,
|
||
|
|
},
|
||
|
|
});
|
||
|
3 years ago
|
const response = await request.json();
|
||
|
|
return response && response && request.status === 200 && request.ok && response.channel;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async getMembers(channelId) {
|
||
|
7 years ago
|
const { num_members } = this.getRoomInfo(channelId);
|
||
|
|
const MAX_MEMBERS_PER_CALL = 100;
|
||
|
|
let members = [];
|
||
|
|
let currentCursor = '';
|
||
|
|
for (let index = 0; index < num_members; index += MAX_MEMBERS_PER_CALL) {
|
||
|
3 years ago
|
// eslint-disable-next-line no-await-in-loop
|
||
|
|
const request = await fetch('https://slack.com/api/conversations.members', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
7 years ago
|
params: {
|
||
|
|
channel: channelId,
|
||
|
|
limit: MAX_MEMBERS_PER_CALL,
|
||
|
2 years ago
|
...(currentCursor && { cursor: currentCursor }),
|
||
|
7 years ago
|
},
|
||
|
|
});
|
||
|
3 years ago
|
// eslint-disable-next-line no-await-in-loop
|
||
|
|
const response = await request.json();
|
||
|
|
if (response && response && request.status === 200 && request.ok && Array.isArray(response.members)) {
|
||
|
|
members = members.concat(response.members);
|
||
|
|
const hasMoreItems = response.response_metadata && response.response_metadata.next_cursor;
|
||
|
7 years ago
|
if (hasMoreItems) {
|
||
|
3 years ago
|
currentCursor = response.response_metadata.next_cursor;
|
||
|
7 years ago
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return members;
|
||
|
|
}
|
||
|
|
|
||
|
3 years ago
|
async react(data) {
|
||
|
|
const request = await fetch('https://slack.com/api/reactions.add', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
3 years ago
|
method: 'POST',
|
||
|
2 years ago
|
params: data,
|
||
|
3 years ago
|
});
|
||
|
|
const response = await request.json();
|
||
|
|
return response && request.status === 200 && response && request.ok;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async removeReaction(data) {
|
||
|
|
const request = await fetch('https://slack.com/api/reactions.remove', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
3 years ago
|
method: 'POST',
|
||
|
2 years ago
|
params: data,
|
||
|
3 years ago
|
});
|
||
|
|
const response = await request.json();
|
||
|
|
return response && request.status === 200 && response && request.ok;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async removeMessage(data) {
|
||
|
|
const request = await fetch('https://slack.com/api/chat.delete', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
3 years ago
|
method: 'POST',
|
||
|
2 years ago
|
params: data,
|
||
|
3 years ago
|
});
|
||
|
|
const response = await request.json();
|
||
|
|
return response && request.status === 200 && response && request.ok;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async sendMessage(data) {
|
||
|
|
const request = await fetch('https://slack.com/api/chat.postMessage', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
3 years ago
|
method: 'POST',
|
||
|
2 years ago
|
params: data,
|
||
|
3 years ago
|
});
|
||
|
|
return request.json();
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async updateMessage(data) {
|
||
|
|
const request = await fetch('https://slack.com/api/chat.update', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
3 years ago
|
method: 'POST',
|
||
|
2 years ago
|
params: data,
|
||
|
3 years ago
|
});
|
||
|
|
const response = await request.json();
|
||
|
|
return response && request.status === 200 && response && request.ok;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
2 years ago
|
async getHistory(options) {
|
||
|
|
const request = await fetch(`https://slack.com/api/conversations.history`, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
3 years ago
|
},
|
||
|
2 years ago
|
params: options,
|
||
|
4 years ago
|
});
|
||
|
3 years ago
|
const response = await request.json();
|
||
|
|
return response;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async getPins(channelId) {
|
||
|
|
const request = await fetch('https://slack.com/api/pins.list', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
7 years ago
|
params: {
|
||
|
|
channel: channelId,
|
||
|
|
},
|
||
|
|
});
|
||
|
3 years ago
|
const response = await request.json();
|
||
|
|
return response && response && request.status === 200 && request.ok && response.items;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
3 years ago
|
async getUser(userId) {
|
||
|
|
const request = await fetch('https://slack.com/api/users.info', {
|
||
|
2 years ago
|
headers: {
|
||
|
|
Authorization: `Bearer ${this.token}`,
|
||
|
|
},
|
||
|
7 years ago
|
params: {
|
||
|
|
user: userId,
|
||
|
|
},
|
||
|
|
});
|
||
|
3 years ago
|
const response = await request.json();
|
||
|
|
return response && response && request.status === 200 && request.ok && response.user;
|
||
|
7 years ago
|
}
|
||
|
2 years ago
|
|
||
|
|
static async verifyToken(token) {
|
||
|
|
const request = await fetch('https://slack.com/api/auth.test', {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
},
|
||
|
|
method: 'POST',
|
||
|
|
});
|
||
|
|
const response = await request.json();
|
||
|
|
return response && response && request.status === 200 && request.ok && response.ok;
|
||
|
|
}
|
||
|
|
|
||
|
|
static async verifyAppCredentials({ botToken, appToken }) {
|
||
|
|
const request = await fetch('https://slack.com/api/apps.connections.open', {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${appToken}`,
|
||
|
|
},
|
||
|
|
method: 'POST',
|
||
|
|
});
|
||
|
|
const response = await request.json();
|
||
|
|
const isAppTokenOk = response && response && request.status === 200 && request.ok && response.ok;
|
||
|
|
const isBotTokenOk = await this.verifyToken(botToken);
|
||
|
|
return isAppTokenOk && isBotTokenOk;
|
||
|
|
}
|
||
|
7 years ago
|
}
|