Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Slack conversations.open method #54518

Open
ksn135 opened this issue Apr 7, 2024 · 0 comments · May be fixed by #54519
Open

Add support for Slack conversations.open method #54518

ksn135 opened this issue Apr 7, 2024 · 0 comments · May be fixed by #54519

Comments

@ksn135
Copy link

ksn135 commented Apr 7, 2024

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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants