钉钉机器人-简单处理类
钉钉机器人消息推送类,实现有用户反馈内容实时推送, 目前只封装了文本类消息, 正式使用构造方法options需改造一下,传入参数

$options = [
    'ding_webhook'=>'',//机器人地址
];

<?php

namespace app\common\library;

use GuzzleHttp\Client;
use think\Config;

/**
 * 钉钉机器人
 * https://open.dingtalk.com/document/robots/custom-robot-access
 */
class DingTalk
{
    
    protected $msgtype = 'text';


    protected $message = [];

    /**
     * 错误内容
     */
    protected $error = '';

    /**
     * 默认配置
     */
    public $options = [
        'charset' => 'utf-8', //编码格式
        'debug' => false, //调式模式
        'mail_type' => 0, //状态
    ];

    /**
     * 构造函数
     * @param array $options
     */
    public function __construct($options = [])
    {
        if ($config = Config::get('site')) {
            $this->options = array_merge($this->options, $config);
        }
        $this->options = array_merge($this->options, $options);
    }

    /**
     * 获取最后产生的错误
     * @return string
     */
    public function getError()
    {
        return $this->error;
    }

    /**
     * 设置错误
     * @param string $error 信息信息
     */
    protected function setError($error)
    {
        $this->error = $error;
    }

    public function setMsgtype($msgtype = 'text')
    {
        $this->msgtype = $msgtype;
        return $this;
    }

    public function setMessage(array $data)
    {
        $this->message['msgtype'] = $this->msgtype;
        $this->message = array_merge($this->message, $data);
        return $this;
    }

    /**
     * 发送文本消息
     * @param string $content
     * @param array $atMobiles
     * @param bool $isAtAll
     * @return bool
     */
    public function sendText(string $content, array $atMobiles = [], bool $isAtAll = false)
    {
        $data = ['msgtype' => 'text', 'text' => ['content' => $content]];
        if(!empty($atMobiles)){
            $data['at'] = ['atMobiles' => $atMobiles, 'isAtAll' => $isAtAll];
        }elseif(!empty($this->options['ding_at_mobile'])){
            $data['at'] = ['atMobiles' => $this->options['ding_at_mobile'], 'isAtAll' => false];
        }
        return $this->setMessage($data)->send();
    }


    /**
     * 发送消息
     * @return boolean
     */
    public function send()
    {
        $url = $this->options['ding_webhook'] ?? '';
        $client = new Client();
        $response = $client->post($url, [
            'headers' => ['Content-Type' => 'application/json'],
            'json' => $this->message,
        ]);
        $result = json_decode($response->getBody(), true);
        if ($result['errcode'] == 0) {
            return true;
        } else {
            $this->setError($result['errmsg']);
            return false;
        }
    }
}

使用方法:
$dingTalk = new DingTalk();
$res = $dingTalk->sendText('你有5条新的意见反馈需要处理');
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇