php-slack-bot icon indicating copy to clipboard operation
php-slack-bot copied to clipboard

Bot loses connection to Slack

Open gugglegum opened this issue 6 years ago • 2 comments

The bot loses connection sometimes and does nothing with it. In slack chat (web-)application slackbot user just become with gray circle (offline). But the script continues to run, need to be killed and restarted. Isn't there a way to force the bot to check is connection alive and throw an exception if not?

The code (excepting code of commands and config):

<?php

require __DIR__ . '/vendor/autoload.php';

use PhpSlackBot\Bot;
use SlackBot\Commands\AlphaLockCommand;
use SlackBot\Commands\AlphaShipmentCommand;
use SlackBot\Commands\GetItemQtyCommand;
use SlackBot\Commands\StatusCommand;

$config = require __DIR__ . '/config.php';

$bot = new Bot();
$bot->setToken($config['slackbot']['token']);
try {
    $bot->loadCommand(new StatusCommand());
    $bot->loadCommand(new GetItemQtyCommand());
    $bot->loadCommand(new AlphaLockCommand());
    $bot->loadCommand(new AlphaShipmentCommand());
    $bot->run();
} catch (\Exception $e) {
    echo $e->getMessage(), "\n";
}

gugglegum avatar Mar 09 '18 13:03 gugglegum

Instead of throwing an exception, I think it would be better to check periodically (using a react loop timer) if the bot is still online and re-connect automatically. To detect disconnections, we can use ping/pong messages specified in the Slack RTM protocol. https://api.slack.com/rtm Section "Ping and Pong"

I am busy at the moment so any help will be very welcome 😃

jclg avatar Mar 11 '18 11:03 jclg

This is likely due to this change, maybe it was acting as a keepalive.

RTM API Presence is now only available via subscription. As of January 2018, presence_change events are not dispatched without presence subscriptions established with presence_sub. Relatedly, current user presence status is no longer communicated in rtm.start.

https://api.slack.com/docs/presence-and-status#presence

I've used this bot for a while and only recently would it disconnect frequently :(

djdevin avatar Aug 20 '18 18:08 djdevin