Feishu.php 2.5 KB
<?php
namespace Jiaoyin;

class Feishu{
    private static $users = [
        'liquid'  => 'gd1d6b34',
        'jiaoyin' => 'efdgdecb',
        'linghu'  => '6ac9fb35',
        'tanli'   => 'f16c2bd7',
        'small'   => 'db9d9g4d',
        '007'    => '9de6cgf3',
    ];
    //发送预警通知
    static public function send($content,$users=[])
    {
        return self::notice($content,'follow',$users);

    }
    static public function sendNoError($content,$users=[])
    {
        return self::notice($content,'error',$users);
    }

    static public function notice($content,$channel="warning",$users=[])
    {
        $urls = [
            'follow' => 'https://open.feishu.cn/open-apis/bot/v2/hook/f30bbb72-091c-499d-a577-43c45dceb158',//跟单
            'warning' => 'https://open.feishu.cn/open-apis/bot/v2/hook/d48c2e4a-0ef9-4684-88d4-e62d878fdaca',//盈迪苟异常报警
            'error' => 'https://open.feishu.cn/open-apis/bot/v2/hook/a344e313-4d5d-4aa4-912b-dda37b2e3ee8',//零容忍
            'strategy'=>'https://open.feishu.cn/open-apis/bot/v2/hook/53444f37-cfec-420c-a4e6-2f415b908dee',//A01-交易策略研发沟通
        ];
        $url  = $urls[$channel] ?? $urls['warning'];
        $userXml = '';
        foreach ($users as $user) {
            if(!empty(self::$users[$user])){
                $userXml .= "<at user_id='" . self::$users[$user] . "'></at>";
            }else{
                $userXml .= "<at user_id='" . $user. "'></at>";//直接用传入的。
            }
        }
        $content = [
            'msg_type'=>"text",
            'content'=>[
                'text'=>$content.$userXml
            ]
        ];
        $con = json_encode($content);
        return self::send_post_json($url,$con);
    }


    static public function send_post_json($url, $jsonStr)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr)
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        return array($httpCode, $response);
    }
}