public function register(Application $app) { $default_options = ['endpoint' => 'slack.com/api/', 'token' => null, 'limit_retries' => 5, 'identities' => ['Silex' => []], 'logging' => ['enabled' => false, 'channel' => '', 'identity' => '']]; if (isset($app['dz.slack.options'])) { $app['dz.slack.options'] = array_merge($default_options, $app['dz.slack.options']); } else { $app['dz.slack.options'] = $default_options; } $app['dz.slack.identity_bag'] = $app->share(function ($app) { $identityBag = new Messaging\IdentityBag(); $identityBag->createIdentities($app['dz.slack.options']['identities']); return $identityBag; }); $app['dz.slack.connection'] = $app->share(function ($app) { $connection = new Client\Connection(); $connection->setEndpoint($app['dz.slack.options']['endpoint']); $connection->setLimitRetries($app['dz.slack.options']['limit_retries']); $connection->setToken($app['dz.slack.options']['token']); return $connection; }); $app['dz.slack.client'] = $app->share(function ($app) { return new Client($app['dz.slack.connection']); }); $app['dz.slack.messaging'] = $app->share(function ($app) { return new Messaging($app['dz.slack.client'], $app['dz.slack.identity_bag']); }); if ($app['dz.slack.options']['logging']['enabled']) { $app['monolog.handler.slack'] = $app->share(function ($app) { $level = MonologServiceProvider::translateLevel($app['monolog.level']); return new SlackHandler($app['dz.slack.messaging'], $app['dz.slack.options']['logging']['channel'], $app['dz.slack.options']['logging']['identity'], $level); }); $app['monolog']->pushHandler($app['monolog.handler.slack']); } }
/** * @param string $channel * @param string $message * @param string $identity * @param Attachment[] $attachments * @return Client\Response|bool * @throws \InvalidArgumentException */ public function message($channel, $message, $identity, array $attachments = []) { if (!$this->identityBag->has($identity)) { throw new \InvalidArgumentException('identiy "' . $identity . '" is not registered'); } return $this->client->send(Actions::ACTION_POST_MESSAGE, ['identity' => $this->identityBag->get($identity), 'channel' => $channel, 'text' => $message, 'attachments' => $attachments], $identity); }