Skip to content

Instantly share code, notes, and snippets.

@girliemac
Last active March 9, 2021 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save girliemac/e0fde6ea1a5af1affe82270750cf3a99 to your computer and use it in GitHub Desktop.
Save girliemac/e0fde6ea1a5af1affe82270750cf3a99 to your computer and use it in GitHub Desktop.
Examples of calling Slack Web API method via HTTP with axios
const axios = require('axios');
const qs = require('qs');
const apiUrl = 'https://slack.com/api';
const greet = () => {
let messageArgs = {
token: process.env.SLACK_ACCESS_TOKEN,
channel: '#cats',
text: ':wave: Hello, welcome to the channel!',
};
post(messageArgs);
};
const post = async(args) => {
const result = await axios.post(`${apiUrl}/chat.postMessage`, qs.stringify(args));
try {
console.log(result.data);
} catch(e) {
console.log(e);
}
};
// pseudo code. you should be using the events API, but I omit the part for now
// when(a new user joined)
greet();
const axios = require('axios');
const qs = require('qs');
const apiUrl = 'https://slack.com/api';
/*
* Get a list of authorized channels
*/
const findAuthedChannels = async(id, cursor) => {
const args = {
token: process.env.SLACK_ACCESS_TOKEN,
exclude_archived: true,
user: id
};
if (cursor) args.cursor = cursor;
const result = await axios.post(`${apiUrl}/users.conversations`, qs.stringify(args));
const { channels, response_metadata } = result.data;
if (response_metadata.next_cursor !== '') {
return channels.concat(await findAuthedChannels(id, response_metadata.next_cursor));
} else {
return channels;
}
};
const chList = await channels.findAuthedChannels(bot);
const axios = require('axios');
const qs = require('qs');
const apiUrl = 'https://slack.com/api';
/*
* Get a list of authorized channels
*/
const findAuthedChannels = (id, cursor) => new Promise((resolve, reject) => {
const args = {
token: process.env.SLACK_ACCESS_TOKEN,
exclude_archived: true,
user: id
};
if (cursor) args.cursor = cursor;
axios.post(`${apiUrl}/users.conversations`, qs.stringify(args))
.then((result => {
const { channels, response_metadata } = result.data;
if (response_metadata.next_cursor !== '') {
resolve(findAuthedChannels(id, response_metadata.next_cursor)
.then(nextResult => channel.concat(nextResult)))
} else {
resolve(channels);
}
}))
.catch((err) => {
console.log(err);
});
});
findAuthedChannels(cursor).then((result) => {
const chList = result;
});
const axios = require('axios');
const qs = require('qs');
const apiUrl = 'https://slack.com/api';
const greet = () => {
let messageArgs = {
token: process.env.SLACK_ACCESS_TOKEN,
channel: '#cats',
text: ':wave: Hello, welcome to the channel!',
};
post(messageArgs);
};
const post = (args) => {
axios.post(`${apiUrl}/chat.postMessage`, qs.stringify(args))
.then((result => {
console.log(result.data);
}))
.catch((err) => {
console.log(err);
});
};
// pseudo code. you should be using the events API, but I omit the part for now
// when(a new user joined)
greet();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment