I'm using the Slack API to get a link from a Slack workspace. It works except when a message is posted, it triggers more then once. Everytime. Here is the code I have:
exports.slack = (req , res) => {
var message = req.body;
if (message.challenge) {
res.send(message.challenge);
} else if (message.event.type == 'message_received') {
if (message.event.text.includes('<')) {
// Link
console.log('NEW LINK');
var link = message.event.text.slice(1, -1);
getArticleInfo(link, (error, body) => {
if (error)
console.error(error);
else {
newArticleToDB(body , link, req);
}
});
}
}
}
Everytime a new message is posted I get a 'NEW LINK' log multiple times. Any ideas where to look for this issue?
Thanks.
I added res.sendStatus(200)
and now everything works as expected. Here's the code:
exports.slack = (req , res) => {
var message = req.body;
res.sendStatus(200);
if (message.challenge) {
res.send(message.challenge);
} else if (message.event.type == 'message' && message.event.subtype != 'message_changed') {
if (message.event.text && message.event.text.includes('<')) {
// Link
console.log('NEW LINK');
var link = message.event.text.slice(1, -1);
getArticleInfo(link, (error, body) => {
if (error)
console.error(error);
else {
newArticleToDB(body , link, req);
}
});
}
}
I had the issue, thank for @taylor-singletary suggestion. I had to print
console.log('Request Headers::: ', JSON.stringify(req.headers));
to look for
"x-slack-retry-num":"1"
To get header value, simply refer to this question: How to extract request http headers from a request using NodeJS connect
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With