I have created a bot using Botkit and wit.ai. It runs perfectly fine. However, after a certain period of time, I start getting multiple messages from the bot. After I restart the node.js service, it starts working normally.
Here's the code I have written,
const _bots = {};
function trackBot(bot) {
_bots[bot.config.token] = bot;
}
const controller = Botkit.slackbot({
json_file_store: './db_slackbutton_bot/',
logger: new winston.Logger({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: './bot.log' })
]
}),
debug: true
})
.configureSlackApp({
clientId: config.slack.clientId,
clientSecret: config.slack.clientSecret,
scopes: ['bot'],
});
// Handle events related to the websocket connection to Slack
controller.on('rtm_open',function(bot) {
console.log('** The RTM api just connected!');
});
controller.on('rtm_close',function(bot) {
// you may want to attempt to re-open
console.log('** The RTM api just closed');
bot.startRTM(function(err) {
if (!err) {
trackBot(bot);
}
});
});
/*
code for starting RTM
*/
controller.storage.teams.all(function(err,teams) {
if (err) {
throw new Error(err);
}
// connect all teams with bots up to slack!
for (var t in teams) {
if (teams[t].bot) {
controller.spawn(teams[t]).startRTM(function(err, bot) {
if (err) {
console.log('Error connecting bot to Slack:',err);
} else {
trackBot(bot);
}
});
}
}
});
controller.on('create_bot',function(bot,config) {
if (_bots[bot.config.token]) {
// already online! do nothing.
} else {
bot.startRTM(function(err) {
if (!err) {
trackBot(bot);
}
bot.startPrivateConversation({user: config.createdBy},function(err,convo) {
if (err) {
console.log(err);
} else {
convo.say('Hey, thanks for adding me to your team! You can say hi to me to get started :)');
}
});
});
}
});
I am not able to figure out what is the issue in the code. These are few errors in the error log,
{"level":"error","message":"** BOT ID: after5 ...reconnect failed after #4 attempts and 8050ms","timestamp":"2016-08-26T10:53:19.175Z"}
{"level":"error","message":"Abnormal websocket close event, attempting to reconnect","timestamp":"2016-08-27T03:08:30.972Z"}
and the screenshot of the messages,

+1, having this issue as well. Only started recently. Glad to know I'm not crazy.
the direct_message event seems to be called multiple times on message sent to bot.
Getting the same issue. Any idea on how to solve this.
One thing I tried was rate limiting, but that doesn鈥檛 always work. 聽This seems to stem from an issue on slack鈥檚 end. 聽My eventual solution was to port my botkit setup to the events API.
I tried using rest api https://slack.com/api/chat.postMessage to send response , this is getting called only once to respond to a message, still I get 2 sometimes 3 similar responses.
@vinodjoseph62 This is likely because the direct_message event is being fired multiple times, not because of your responses. The Events API doesn't seem to have this issue.
Okay will try doing it using the event api.
You can probably set up an events hook e.g. /events/receive and then use the controller.trigger method to use your existing botkit setup with the events API.
Did the above solve your question? Closing for now, if you still need assistance please ask in:
@peterswimm the issue seems to be an RTM api issue, as far as I know, this issue will continue to pop up until botkit is ported to the events API.
@dtesler that's coming soon!
@peterswimm already built my own events API port 馃槣 .
@dtesler PR it!
Having still the issue and couldn't find something to solve it, is botkit supporting the API Events ?
If not yet, any ideas ?
I checked the event API but do I need to set up my botkit all over again so my code supports API Events ?
API Events support is upcoming, possibly with https://github.com/howdyai/botkit/pull/459
Oh great ! So for now if I don't want my bot to answer multiple times, I need to setup Events API on my own ?
@kadaaran @vinodjoseph62 are you also using wit.ai middleware as well?
Can anyone who is experiencing this give some context to what they are listening to as a trigger when multiple responses are happening? @dtesler
@jonchurch back when I was running into this, it was listening on controller.on('direct_message'), and according to slack, this was an issue on their end with RTM, not botkit.
@dtesler I'm currently encountering this error. Please how did you go with the porting at your ends?
We're experiencing this same issue and we're using the Events API. I just posted a test message and received this identical JSON posted 3 times, even though they're not immediate. One was post immediate, the other about a second later and the third arrives several seconds later. Having said that, the timestamp value is identical on all three JSON requests:
{"token":"S...N","team_id":"T4...H","api_app_id":"A4...2","event":{"type":"message","user":"U4...P","text":"Good job!","ts":"1486835278.000054","channel":"C4...L","event_ts":"1486835278.000054"},"type":"event_callback","authed_users":["U4...4"]}
{"token":"S...N","team_id":"T4...H","api_app_id":"A4...2","event":{"type":"message","user":"U4...P","text":"Good job!","ts":"1486835278.000054","channel":"C4...L","event_ts":"1486835278.000054"},"type":"event_callback","authed_users":["U4...4"]}
{"token":"S...N","team_id":"T4...H","api_app_id":"A4...2","event":{"type":"message","user":"U4...P","text":"Good job!","ts":"1486835278.000054","channel":"C4...L","event_ts":"1486835278.000054"},"type":"event_callback","authed_users":["U4...4"]}
I've replaced most characters in all our IDs w/ ellipses, but I assure you the values are identical.
This is a real pain because the only way I can imagine getting around this is to store the message token in a database, causing two extra queries for each message (one to store it and one to check if it exists or not), not to mention the additional database storage.
To anyone else who encounters and reports this!
Please include your controller.on function that the duplicated responses are defined within, the events you have subscribed to on whatever platform you are on, and a section of your logs that contains only items from the single user message that resulted in multiple responses.
I think this happens when people subscribe to more events (like user_typing, message_received, message_echoes, etc) than they are using, and then making functions like controller.on('message_received'), which triggers on lots of kinds of messages sent to the bot. A single user mesage can result in several platform events being sent to you webhook simultaneously.
It appears to me that I get 1 message per event I'm subscribed to even if there is overlap. For instance, I had events app_mention and some other scope that was for when anyone posted to channel. Since I would test with posting to the channel while AT'ing my slackbot, I received 2 messages: mentioning my slackbot and sending a message to the channel. After removing the scope for any post to the channel, I now only receive 1 event for the app_mention. Hope this helps someone.
Most helpful comment
I tried using rest api https://slack.com/api/chat.postMessage to send response , this is getting called only once to respond to a message, still I get 2 sometimes 3 similar responses.