[Notifier] [Slack] Add support for `conversations.open` method
| 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);
For option 2, can we do the array_unique in the setter?
@OskarStark fixed
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
Maybe just call this method group(array $uids)?
Please add a test case for this new feature, thanks
@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.
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?
friendly ping @ksn135
Closing due to lack of activity, feel free to reopen when you find time. Thanks 🙏