LoginSignup
10

More than 3 years have passed since last update.

PHPからSlackに通知するシンプルな方法

Posted at

流れ

1.Slack App を作成する
2.PHPにコードを記述する

Slack App の作成

Slack App の新規作成

https://api.slack.com/
上記URLにアクセスし、画面真ん中の「Start Building」をクリックする。

「App Name」:アプリの名前を入力
「Development Slack Workspace」:アプリを導入するワークスペースを選択

Slack App の設定

「Basic Information」→「Display Information」でアプリのアイコンや色を設定できる。

「OAuth & Permissions」→「Scopes」で「Add an OAuth Scope」をクリックする。
「Add permission ...」の欄をクリックして、「chat:write:bot」を選択して有効にする。
「Install App to Workspace」をクリックし、「許可する」を選択する。
「OAuth Access Token」をコピーしておく。

PHPの記述

$token = {OAuth_Access_Token};//上記でコピーした「OAuth Access Token」
$channel = {Channel_Name};//投稿するチャンネル名(もしくはチャンネルID)
$text = {Post_Message};//投稿するメッセージ

$channel = urlencode($channel);//文字列をURLエンコードする(例えば日本語はそのままURLとして使用できない)
$text = urlencode($text);

$url = "https://slack.com/api/chat.postMessage?token=${token}&channel=%23${channel}&text=${text}";
$response = file_get_contents($url);
return $response;//特に意味はない。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10