Skip to content

Instantly share code, notes, and snippets.

@tajuszk
Last active March 13, 2020 01:37
Show Gist options
  • Save tajuszk/2603b82226bee8aecdd3d36877ed92f3 to your computer and use it in GitHub Desktop.
Save tajuszk/2603b82226bee8aecdd3d36877ed92f3 to your computer and use it in GitHub Desktop.
namespace App\Http\Controllers;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
class SlackController extends Controller
{
const BOT_TOKEN = 'YOUR_TOKEN';
/**
* /yukyu のSlashコマンドを実行すると呼ばれる場所
*
* @param Request $request
* @return Response
*/
function yukyuDialog (Request $request) {
$url = 'https://slack.com/api/dialog.open';
$token = self::BOT_TOKEN;
$dialog = $this->getYukyuDialog();
$trigger_id = $request->input('trigger_id');
$params = [
'token' => $token,
'dialog' => \GuzzleHttp\json_encode($dialog),
'trigger_id' => $trigger_id
];
$client = new Client();
$response = $client->request(
'POST',
$url, // URLを設定
['query' => $params] // パラメーターがあれば設定
);
Log::info(print_r($response, true));
return response('',200);
}
/**
* ダイアログから送られてくるリクエストを受け取る
* 他のリクエストからの受け口になるので callback_id などで場合分けする必要がある
*
* @param Request $request
* @return Response
*/
function interactiveMessage (Request $request) {
$payload = $request->input('payload');
$postData = \GuzzleHttp\json_decode($payload, true);
$text = '以下の内容で受け付けました!';
$channel = $postData['channel']['id'];
$user = $postData['user']['id'];
$submission = $postData['submission'];
$dialog = $this->getYukyuDialog();
$attachment = $this->makeAttachment($dialog, $submission);
$this->postEphemeral($channel, $user, $text, $attachment);
return response('',200);
}
/**
* 「あなただけに表示されています」のメッセージを送る
*
* @param $channel
* @param $user
* @param $text
* @param $attachment
*/
function postEphemeral ($channel, $user, $text, $attachment) {
$url = 'https://slack.com/api/chat.postEphemeral';
$token = self::BOT_TOKEN;
$params = [
'token' => $token,
'attachments' => $attachment,
'channel' => $channel,
'text' => $text,
'user' => $user,
];
$client = new Client();
$response = $client->request(
'POST',
$url,
['query' => $params]
);
Log::info(print_r($response, true));
}
/**
* 回答を受け取った結果をまとめる
*
* @param $dialog
* @param $submission
* @return string
*/
function makeAttachment ($dialog, $submission) {
$elements = $dialog['elements'];
$attachment = [];
$text = "";
foreach ($submission as $key => $value) {
foreach ($elements as $element) {
if ($element['name'] === $key) {
$label = $element['label'];
$answer = '';
if (isset($element['options'])) {
$options = $element['options'];
foreach ($options as $option) {
if ($option['value'] === $value) {
$answer = $option['label'];
}
}
} else {
$answer = $value;
}
$text .= $label . ' : ' . $answer. "\n";
}
}
}
$attachment[] = [
'text' => $text,
];
return \GuzzleHttp\json_encode($attachment);
}
/**
* ダイアログのテンプレートを作る
*
* @return array
*/
function getYukyuDialog () {
return [
"callback_id" => "yukyu",
"title" => "休暇申請ダイアログ",
"submit_label" => "送信",
"state" => "none",
"elements" => [
[
"type" => "select",
"label" => "休暇種類",
"name" => "holiday_type",
"options" => [
[
"label" => "有給休暇",
"value" => "type-full"
],
[
"label" => "午前休暇",
"value" => "type-am"
],
[
"label" => "午後休暇",
"value" => "type-pm"
]
],
],
[
"type" => "text",
"label" => "休暇予定日",
"name" => "date",
"placeholder" => "2020-01-01",
],
[
"type" => "textarea",
"label" => "休暇理由",
"name" => "reason",
"hint" => "理由を記入してください",
]
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment