Skip to content

Slack RTM typing indicator #264

@crocodele

Description

@crocodele

The Slack RTM driver currently has an empty types() method. I've tried to implement the typing indicator using the RTM API (documentation). It works in the sense that the typing indicator is shown, but for some reason the indicator appears only after the reply has been sent. The result is the same regardless of the sleep time (I tried with 10 seconds and 1 second). Do you have any ideas why showing the typing indicator is deferred like this? Is it related to websocket calls vs. HTTP API calls?

slack-client / RealTimeClient.php:

public function setAsTyping(ChannelInterface $channel)
{
	if (!$this->connected) {
		return Promise\reject(new ConnectionException('Client not connected. Did you forget to call `connect()`?'));
	}

	$data = [
		'id' => ++$this->lastMessageId,
		'type' => 'typing',
		'channel' => $channel->data['id'],
	];
	$this->websocket->send(json_encode($data));
}

botman / SlackRTMDriver.php:

public function types(Message $matchingMessage)
{
	$channel = null;

	$this->getChannel($matchingMessage)->then(function ($_channel) use (&$channel) {
		$channel = $_channel;
	});

	if (is_null($channel)) {
		$this->client->getDMById($matchingMessage->getChannel())->then(function ($_channel) use (&$channel) {
		   $channel = $_channel;
		});
	}

	if (! is_null($channel)) {
		$this->client->setAsTyping($channel);
	}
}

test:

$loop = Factory::create();
$config = ['slack_token' => 'SLACK_TOKEN_HERE'];
$botMan = BotManFactory::createForRTM($config, $loop);

$botMan->hears('ping', function(BotMan $bot) {

    // Simulate typing
    $bot->typesAndWaits(1);

    // Respond
    $bot->reply('Pong');
});

$loop->run();

Thanks for an awesome library! 😎

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy