Skip to content

Instantly share code, notes, and snippets.

@tajuszk
Last active April 5, 2020 10:45
Show Gist options
  • Save tajuszk/c7e75f584930349a6dfc6ee245d49cec to your computer and use it in GitHub Desktop.
Save tajuszk/c7e75f584930349a6dfc6ee245d49cec to your computer and use it in GitHub Desktop.
<?php
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 = 'xoxb-xxxxxxxx';
// 先程のControllerに追記
/**
* ダイアログから送られてくるリクエストを受け取る
* 他のリクエストからの受け口になるので callback_id などで場合分けする必要がある
*
* @param Request $request
* @return Response
*/
function interactiveMessage (Request $request) {
// 早期レスポンス
response('',200)->send();
$payload = $request->input('payload');
$postData = \GuzzleHttp\json_decode($payload, true);
Log::info(print_r($postData, true));
/*
* なんらかの処理
*/
$text = '登録しました!';
$user = $postData['user']['id'];
// メッセージを送る
// modalを使う場合、response_urlを取得できなかったので強制的にメッセージ投稿
$response = $this->postEphemeral($user, $text);
Log::info(print_r($response, true));
return response('',200);
}
/**
* 「あなただけに表示されています」のメッセージを送る
*
* @param $user
* @param $text
* @param $attachment
* @return mixed
*/
function postEphemeral ($user, $text, $attachment = []) {
$url = 'https://slack.com/api/chat.postEphemeral';
$token = self::BOT_TOKEN;
$params = [
'token' => $token,
'attachments' => $attachment,
'channel' => $user,
'text' => $text,
'user' => $user,
];
$client = new Client();
$response = $client->request(
'POST',
$url,
['query' => $params]
);
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment