LoginSignup
2
1

More than 5 years have passed since last update.

PHPでMattermostにattachments本文をPostするサンプルプログラム

Last updated at Posted at 2018-11-09

とりあえず今はただただ自分へのメモとして。。。

SlackクローンのMattermostへ、内向きのウェブフック(Incoming Webhooks)を利用してメッセージをPostするPHPスクリプトのサンプルです。
Postする本文としては、表示を装飾したattachments本文を書き込みます。

タイトルのこの前置きだけでなんかお腹いっぱいですがとりあえず忘れないように。。。

サンプルスクリプト

とりあえずめちゃくちゃ単純に本文のみをPostしてます。

<?php

// Webhook URL
$url = "取得したWebhook URL";

// メッセージ本文(Attachmentsで装飾する)
$message = array(
        "attachments" => array(
                array(
                        "color" => "warning",
                        "title" => "たいとる",
                        "text"  => "本文"
                )
        )
);

// メッセージをJSON化
$message_json = json_encode($message);

// URLエンコードしてpayload指定
$message_post = 'payload=' . urlencode($message_json);

// cURLで送信
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
//echo $result;

// cURLチャンネルを閉じる
curl_close($ch);

?>

とりあえず単純にこれだけ。
実際に投稿された結果はこんな感じになります。

image.png

ちなみに動作確認は PHP 5.4.16 で行いました。(古い)

$ php -v
PHP 5.4.16 (cli) (built: Apr 12 2018 19:02:01)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
2
1
0

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
2
1