1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Phpからシンプルにslackにアラートを飛ばす(2022新方式版)

Posted at

はじめに

フレームワークはCodeIgniterを使っていますが、どんなフレームワークでもサービスとしてクラスを作って、どっかから呼べばできるはずです

下準備

slack側の準備はこれを見てください

source

app/Libraries/Slack.php
<?php

namespace App\Libraries;

class Slack
{
    private const TODAY = 0;
    private const TOKYO = 130010;
    
    /**
     * slackにメッセージを送ったあと、成功したら ok を返す
     * @param string $message
     * @return string
     */
    public function sendMessage(string $message): string
    {
        $url = env('slack.url');
    
        $post_fields = [
            "channel" => urlencode(env('slack.channel')),
            "text" => $message,
        ];
        
        $options = [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true, // 実行結果を文字列で返す
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS =>
                http_build_query(
                    ['payload' => json_encode($post_fields)]
                )
        ];
        $conn = curl_init();
        curl_setopt_array($conn, $options);
        $res = curl_exec($conn);
        curl_close($conn);
        
        return $res;
    }
    
    /**
     * メッセージのネタは「天気」にしましょう!
     * @return string
     */
    public function sampleMessage(): string
    {
        // PHPのcurlでSSL certificate problemが出る場合
        // https://tm23forest.com/contents/php-curl-ssl-certificate-problem
        $url = 'https://weather.tsukumijima.net/api/forecast/city/' . self::TOKYO;
        $conn = curl_init();
        $options = [
            CURLOPT_URL => $url,
            CURLOPT_HEADER => false,
            CURLOPT_RETURNTRANSFER => true, // 実行結果を文字列で返す
        ];
        curl_setopt_array($conn, $options);
        $res = json_decode(curl_exec($conn), true)["forecasts"][self::TODAY];
        curl_close($conn);
    
        return $res["date"] . PHP_EOL . $res["telop"];
    }
}
tests/Tests/Libraries/SlackTest.php
<?php

namespace Tests\Libraries;

use App\Libraries\Slack;
use PHPUnit\Framework\TestCase;

class SlackTest extends TestCase
{
    
    public function testSampleMessage()
    {
        $service = new Slack();
        $expected = '2022-02-11' . PHP_EOL . '晴時々曇' . PHP_EOL . 'From PHP script';
        $this->assertEquals($expected, $service->sampleMessage() . PHP_EOL . 'From PHP script');
    }
    
    public function testSendMessage()
    {
        $service = new Slack();
        $this->assertEquals('ok', $service->sendMessage('あああ'));
    }
}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?