symfony icon indicating copy to clipboard operation
symfony copied to clipboard

[Notifier] [Slack] Add support for `conversations.open` method

Open ksn135 opened this issue 1 year ago • 5 comments

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #54518
License MIT

Description

Suppose you have slackIDs of your users in your database and you need to send a message to several of them BUT simultaneously to one channel (no matter if it is created or not), instead of sending a direct message to each of them separately. We can implement a special conversations method that will return the name of an existing channel using the Slack API or create it automatically with the set of users passed as a parameter to this method. Inside the SlackTransport::doSend method, if this option was set earlier, an additional request to the Slack API will be executed in the response, which will contain the name of the target channel, which will be stored in the array of options by the recipient_id key.

API Details: https://api.slack.com/methods/conversations.open

Request:

curl --location 'https://slack.com/api/conversations.open' \ 
   --header 'Content-Type: application/json; charset=utf-8' \
   --header 'Authorization: Bearer xoxb-XXXXXXXXXXXXX-YYYYYYYYYYYYY-ZZZZZZZZZZZZZZZZZZZZZZZZ' \
   --data '{
      "users": "U0XXXXXXXX1,U0XXXXXXXX2",
   }'

Response:

{"ok":true,"no_op":true,"already_open":true,"channel":{"id":"D0XXXXXXXX1"}}

Example

use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransport;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;

$chatMessage = new ChatMessage('New Symfony Feature');

$options = (new SlackOptions())
// use string parameter directly in conversation.open API call
    ->conversations('U0XXXXXXXX1,U0XXXXXXXX2') 
// OR allow to specify array
// !!! Implementor MUST perform array_filter and array_unique calls !!!
    ->conversations(['U0XXXXXXXX1', 'U0XXXXXXXX2', null, false, 'U0XXXXXXXX1'])
// OR even allow to specify recipients dynamically
    ->conversations(function(
        SlackTransport $transport,
        MessageInterface $message
    ): array {
        return ['U0XXXXXXXX1', 'U0XXXXXXXX2'];
    })
;
$chatMessage->options($options);

$chatter->send($chatMessage);

ksn135 avatar Apr 07 '24 21:04 ksn135

For option 2, can we do the array_unique in the setter?

OskarStark avatar Apr 08 '24 04:04 OskarStark

@OskarStark fixed

ksn135 avatar Apr 08 '24 08:04 ksn135

I don't know if i love the "conversations" name.

I mean "conversation" is the thing we are opening, but setting conversations(...$userIds) seems a bit off : we are not passing or creating conversations but more grouping users into one.

Maybe over-thinking things there.. but that intrigued me at first reading

smnandre avatar Apr 12 '24 21:04 smnandre

Maybe just call this method group(array $uids)?

ksn135 avatar Apr 13 '24 20:04 ksn135

Please add a test case for this new feature, thanks

OskarStark avatar Apr 15 '24 06:04 OskarStark

@ksn135 Would you like to finish this PR? I agree that we should rename the conversations method to something else and we need documentation in the README file as well. Having some tests would be great as well.

fabpot avatar Jan 05 '25 10:01 fabpot

Hi @fabpot ! Yes, I would like to make edits and tests to this PR, however I am right now on vacation. Will it be okay if I update this PR near the end of January 2025? Maybe we should call this method group(array $uids) as I suggested earlier?

ksn135 avatar Jan 07 '25 10:01 ksn135

friendly ping @ksn135

OskarStark avatar Feb 28 '25 12:02 OskarStark

Closing due to lack of activity, feel free to reopen when you find time. Thanks 🙏

OskarStark avatar Apr 01 '25 09:04 OskarStark