in packages/slack/post.js [31:91]
function main(params) {
let errorMsg = checkParams(params);
if (errorMsg) {
return { error: errorMsg };
}
var body = {
channel: params.channel,
username: params.username || 'Simple Message Bot'
};
if (params.icon_emoji) {
// guard against sending icon_emoji: undefined
body.icon_emoji = params.icon_emoji;
}
if (params.as_user === true) {
body.as_user = true;
}
if (params.text) {
body.text = params.text;
}
if (params.attachments) {
body.attachments = JSON.stringify(params.attachments);
}
if (params.token) {
//
// this allows us to support /api/chat.postMessage
// e.g. users can pass params.url = https://slack.com/api/chat.postMessage
// and params.token = <their auth token>
//
body.token = params.token;
} else {
//
// the webhook api expects a nested payload
//
// notice that we need to stringify; this is due to limitations
// of the formData npm: it does not handle nested objects
//
body = {
payload: JSON.stringify(body)
};
}
var promise = new Promise(function (resolve, reject) {
needle.post(params.url, body, function (err, res, body) {
if (err) {
console.log('error: ', err, body);
reject(err);
} else {
console.log('success: ', params.text, 'successfully sent');
resolve();
}
});
});
return promise;
}