maknz / slack

A simple PHP package for sending messages to Slack, with a focus on ease of use and elegant syntax.
BSD 2-Clause "Simplified" License
1.17k stars 204 forks source link

Support multiple teams #60

Open maknz opened 8 years ago

maknz commented 8 years ago

We need to support multiple teams. As part of the 2.0 release, we'll be moving away from simply an 'endpoint' and have a first-class Team object. A client can be instantiated with one or more teams. Each team will have a default channel, and a name which can be used to refer to that team, e.g. 'mycompany'.

Example usage:

$team = new Team(['name' => 'mycompany', 'webhook' => 'https://hooks.slack.com/...', 'default_channel' => '#general']);
$client = new Client($team);
$client->send('Hello'); // sends to the one team configured, to the default channel of #general
$teams = [
  new Team(['name' => 'company1', 'webhook' => 'https://hooks.slack.com/...', 'default_channel' => '#engineering']),
  new Team(['name' => 'company2', 'webhook' => 'https://hooks.slack.com/...', 'default_channel' => '#devops'])
];

$client = new Client($teams);
$client->team('company1')->send('hello company1'); // #engineering in company1
$client->team('company2')->send('hello company2'); // #devops in company2

The full lead up discussion to the feature is over at #44.

I plan to work on this next weekend.

Gummibeer commented 8 years ago

Wouldn't it be cool to be able to instantiate a Client just by Team name!? Something like: new Client('mycompany'); this will make it easier to use the default settings. Same would be possible with an array new Client(['company1', 'company2']);.

maknz commented 8 years ago

You'd still need to provide the endpoints as well, so wrapping all that up in a Team seems to solve that while still keeping the client easy to interact with.

Ideally the client would generally only be instantiated in the Laravel service provider, or some other IoC type deal. But, even if it was in a a controller, it should still be a one-off thing.

We could also have an addTeam method to Client so you could just do new Client and then $instance->addTeam($team)->addTeam($anotherTeam).

Gummibeer commented 8 years ago

Ok, so the teams are not in the config file? It feels a bit away from the quick and simple usage yet. My idea is something like the different DB connections - everything is in the config file and you have a default one - so the usage of \Slack::send('Hello world!'); is possible but it's also possible to use it like \Slack::team('myteam')->send('hello world') or with an array of teams \Slack::team(['myteam1', 'myteam2'])->send('hello world') and with the Team object \Slack::team(new Team(...))->send('hello world') or with an array of team objects or mix it up.

Edit: Ok, you have moved the laravel parts in another repo - sorry. So this is everything thought for laravel.^^

maknz commented 8 years ago

Ah yes, sorry for the confusion. Yeah, for Laravel you'd have your teams in a config file, and then just simply Slack::team('company1')->send('message') :+1:. The teams would be required for instantiating the client for uses outside Laravel.