Deplayed messages in Slack with Botkit.
When developing a bot for Slack (and most likely others) we have 3 seconds to respond until our request times out. What do we do if we are making a request in our bot that will take longer then 3 seconds?
This error tyically looks like this:
failed with the error "operation_timeout"
We are using Botkit to develop a small bot that can create development enviornments for teams. Our bot makes calls to an API endpoint that may take longer then 3 seconds.
This took us a while to figure out, and the documentation we found was not super clear. So we want to make a note here for anyone that may be going through this.
Botkit v4 seems to have gone through a few large changes. In previous versions the bot object had methods suchs as bot.replyPrivateDelayed
and bot.replyPublicDelayed
. In version 4, we do not have that functionality.
It took me far to long to figure this out... Doh!
Long story short the following code is working. This is a basic slash command that calls a function which in turn makes an API call. By using the bot.replyPrivate
and passing to it the message we are able to respond when the promise resolves and all is happy in the world:
controller.on('slash_command', async function(bot, message) {
// Lets call a function that will take > 3 seconds to resolve.
longHTTPCall(message.text).then(async (response) => {
// Use the bot.replyPrivate to shoot the user a DM with their info.
await bot.replyPrivate(message, "I have completed your request!" ).catch(e => console.log(e))
}).catch(e => console.log(e))
})
If you find this helpful or have any questions, please feel free to reach out!