Permalink
Browse files

Add middleware for sending message to slack via response url

See screenshots here: http://imgur.com/a/HRm6L
  • Loading branch information...
stringerbell committed May 29, 2016
1 parent 7397576 commit 4c8ceb644d804bf6ff246a6bb1133d05943923b6
@@ -4,6 +4,8 @@
use App\Pipeline\SlackJiraPipeline;
use App\Service\Jira\JiraShowIssueCommand;
use App\Service\Jira\JiraShowIssueCommandFactory;
use App\Service\Slack\SendMessageToSlackUserViaResponseUrl;
use App\Service\Slack\SendMessageToSlackUserViaResponseUrlFactory;
use App\Validator\Slack\ParseSlackJiraInput;
use App\Validator\Slack\ParseSlackJiraInputFactory;
use App\Validator\Slack\ValidateSlackToken;
@@ -28,14 +30,15 @@
],
// Use 'factories' for services provided by callbacks/factory classes.
'factories' => [
Application::class => ApplicationFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
SlackJiraPipeline::class => SlackJiraPipeline::class,
ValidateSlackToken::class => ValidateSlackTokenFactory::class,
ParseSlackJiraInput::class => ParseSlackJiraInputFactory::class,
SlackClient::class => SlackClient::class,
JiraClient::class => JiraClient::class,
JiraShowIssueCommand::class => JiraShowIssueCommandFactory::class,
Application::class => ApplicationFactory::class,
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
SlackJiraPipeline::class => SlackJiraPipeline::class,
ValidateSlackToken::class => ValidateSlackTokenFactory::class,
ParseSlackJiraInput::class => ParseSlackJiraInputFactory::class,
SlackClient::class => SlackClient::class,
JiraClient::class => JiraClient::class,
JiraShowIssueCommand::class => JiraShowIssueCommandFactory::class,
SendMessageToSlackUserViaResponseUrl::class => SendMessageToSlackUserViaResponseUrlFactory::class,
],
],
];
@@ -3,6 +3,7 @@
namespace App\Pipeline;
use App\Service\Jira\JiraShowIssueCommand;
use App\Service\Slack\SendMessageToSlackUserViaResponseUrl;
use App\Validator\Slack\ParseSlackJiraInput;
use App\Validator\Slack\ValidateSlackToken;
use App\Validator\ValidateBody;
@@ -18,6 +19,9 @@ public function __invoke(ContainerInterface $container)
$pipeline->pipe($container->get(ValidateSlackToken::class));
$pipeline->pipe($container->get(ParseSlackJiraInput::class));
$pipeline->pipe($container->get(JiraShowIssueCommand::class));
$pipeline->pipe(
$container->build(SendMessageToSlackUserViaResponseUrl::class, ['message' => 'Processing Complete'])
);
return $pipeline;
}
@@ -0,0 +1,37 @@
<?php
namespace App\Service\Slack;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class SendMessageToSlackUserViaResponseUrl
{
/**
* @var Client
*/
private $slackClient;
/**
* @var string
*/
private $message;
public function __construct(Client $slackClient, string $message)
{
$this->slackClient = $slackClient;
$this->message = $message;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
{
$body = $request->getParsedBody();
$responseBody = [
'text' => $this->message,
'response_type' => $body['response_type'] ?? 'ephemeral',
];
$this->slackClient->post($body['response_url'], ['body' => json_encode($responseBody)]);
return $next($request, $response, $error ?? null);
}
}
@@ -0,0 +1,21 @@
<?php
namespace App\Service\Slack;
use App\Client\SlackClient;
use Interop\Container\ContainerInterface;
class SendMessageToSlackUserViaResponseUrlFactory
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = [])
{
$defaults = [
'message' => 'You sent a message, but forgot to replace the default! Go you!
You should probably use `build`',
];
$options += $defaults;
$slackClient = $container->get(SlackClient::class);
return new SendMessageToSlackUserViaResponseUrl($slackClient, $options['message']);
}
}

0 comments on commit 4c8ceb6

Please sign in to comment.