jclg / php-slack-bot

Slack bot user written in PHP
MIT License
169 stars 61 forks source link

Bot loses connection to Slack #30

Open gugglegum opened 6 years ago

gugglegum commented 6 years ago

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";
}
jclg commented 6 years ago

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 😃

djdevin commented 6 years ago

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 :(