Mattermostの内向きWebhookにAttachmentsを追加して叩く
目次
本エントリの概要
- 前回に引き続き、Slackクローンとして頑張っているMattermostの統機能「内向きウェブフック(Incoming WebHooks)」へPHPから送信するのをやってみたメモです。
- PHPの環境の制約によりcURLが使えなかったので代わりにfile_get_contentsを使っています。
- 本エントリでは「Message Attachments」を使う場合について記録しています。
- 最終的な出力はこれです。
コード
// サーバーで作った内向きウェブフックのURL $url = "http://mattermost.server.path/hooks/xxxxxxxxxxxxxxxxxxxxxxxxxxx"; $pretext = "変なBlogに投稿がありました。"; $title = "No.20182 Mattermostの内向きWebhookにAttachmentsを追加して叩く"; $body = '+ 前回に引き続き、Slackクローンとして頑張っているMattermostの統機能「内向きウェブフック」へPHPから送信するのをやってみたメモです。 + PHPの環境の制約によりcURLが使えなかったので代わりにfile_get_contentsを使っています。 + 本エントリでは「Message Attachments」を使う場合について記録しています。 '; $attachments = array( array( "fallback"=> "NoticeFromWebApplication", "color"=> "#FFA500", "pretext"=> $pretext, "text"=> $body, "author_name"=> "untitled.engineer", "author_icon"=> "/path/to/icon.png", "author_link"=> "https://path/to/blog/top", "title"=> $title, "title_link"=> "https://path/to/target/blog/entry", ) ); $content = array( "icon_url" => "/path/to/icon.png", "username" => "NoticeFromWebApplication", "attachments" => $attachments, // "text" => $body, ); $context = stream_context_create(array( "http" => array( "ignore_errors" => true, "method" => "POST", "header" => "Content-Type: application/json", "content" => json_encode($content), ) )); $response = file_get_contents($url, false, $context); // 以下省略
ポイント
- 'attachments' の中は2重の配列になっていることに注意
まとめ
- おー、なんかこっちの方がBOTっぽい