Last active
March 9, 2021 08:04
-
-
Save girliemac/e0fde6ea1a5af1affe82270750cf3a99 to your computer and use it in GitHub Desktop.
Examples of calling Slack Web API method via HTTP with axios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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