Dingding robot message push composer expands laravel-dingbot

DingTalk robot message sending

introduce

There are many systems within the enterprise that support the company's core business processes, such as CRM systems, transaction systems, monitoring and alarm systems, and so on. Through DingTalk's custom robot, these system events can be synchronized to DingTalk's chat group. laravel-dingbot is a Laravel extension for DingTalk robot message push. Through this extension, you can simply and efficiently push message notifications to DingTalk for monitoring and reminder operations.

DingTalk official document

Install

composer require dreamcoders/laravel-dingbot

Use in laravel project

Execute after successful installation

php artisan vendor:publish --provider="DreamCoders\DingBot\DingBotServiceProvider"

Will be automatically dingbot.phpadded to your project's config configuration file

related configuration

return [
    'webhookUrl' => env('DING_ROBOT_URL', 'https://oapi.dingtalk.com/robot/send'),

    'default' => [
        // 机器人的access_token default
        'access_token' => env('DING_TOKEN','4c8ed2083bcd5cc9d38343211900ea29ef7bbef9156ad0892'),
        // 加签 (不开启加签则为空)
        'secret' => env('DING_SECRET',''),
    ],
    'bot1' => [
        // 机器人的access_token  bot1
        'access_token' => env('DING_TOKEN1',''),
        // 加签 (不开启加签则为空)
        'secret' => env('DING_SECRET1',''),
    ],
    'bot2' => [
        // 机器人的access_token  bot2
        'access_token' => env('DING_TOKEN2',''),
        // 加签 (不开启加签则为空)
        'secret' => env('DING_SECRET2',''),
    ],
];

DingTalk push access_token

.env configuration

DING_TOKEN=you-push-token

Multi-robot configuration

If you want to add multiple robots, just dingbot.phpadd the robot name and the corresponding token configuration in it

    'default' => [
        // 机器人的access_token default
        'access_token' => env('DING_TOKEN','4c8ed2083bcd5cef9156ad0892'),
        // 加签 (不开启加签则留空)
        'secret' => env('DING_SECRET',''),
    ],
    'bot1' => [
        // 机器人的access_token  bot1
        'access_token' => env('DING_TOKEN1',''),
        // 加签 (不开启加签则留空)
        'secret' => env('DING_SECRET1',''),
    ],
    'bot2' => [
        // 机器人的access_token  bot2
        'access_token' => env('DING_TOKEN2',''),
        // 加签 (不开启加签则留空)
        'secret' => env('DING_SECRET2',''),
    ]

.env configuration

DING_TOKEN=you-push-token
DING_TOKEN1=you-push-token

security configuration

custom keywords

Up to 10 keywords can be set, and the message can only be sent successfully if at least one of them is included in the message. For example, if a custom keyword is added: monitoring alarm, the message sent by this robot must contain the word monitoring alarm in order to be sent successfully.

Signature

When DingTalk creates a robot, choose to add a signature, and secretcopy it to the corresponding configuration file. If you enable the signature addition method, you must fill it in secret, otherwise you can leave it blank

DING_SECRET=you-bot-secret
DING_SECRET1=you-bot-secret
DING_SECRET2=you-bot-secret

IP address (segment)

After setting, only requests from within the IP address range will be processed normally. Supports two setting methods: IP address and IP address segment, does not support IPv6 address whitelist

Example of use

Send a plain text message

Ding()->text("服务器通知报警,请紧急处理")->send();

or

(new DingBot())->text('服务器通知报警,请紧急处理')->send();

Send a message @designated person

Ding()->text("服务器通知报警,请紧急处理")
->setAtMobiles(['13218899188'])
->send();

Send a message @everyone

Ding()->text("服务器通知报警,请紧急处理")
->setAtAll()
->send();

Send a markdown type message

$title = '杭州天气';
$markdown = "#### 杭州天气  \n ".
            "> 9度,@1825718XXXX 西北风1级,空气良89,相对温度73%\n\n ".
            "> ![screenshot](http://i01.lw.aliimg.com/media/lALPBbCc1ZhJGIvNAkzNBLA_1200_588.png)\n".
            "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) ";
            
Ding()->markdown($title,$markdown)->send();

Send a message @designated person/all ditto

Send a link type message

$title = "自定义机器人协议";
$text = "群机器人是钉钉群的高级扩展功能。群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步。例如:通过聚合GitHub,GitLab等源码管理服务,实现源码更新同步;通过聚合Trello,JIRA等项目协调服务,实现项目信息同步。不仅如此,群机器人支持Webhook协议的自定义接入,支持更多可能性,例如:你可将运维报警提醒通过自定义机器人聚合到钉钉群。";
$picUrl = "";
$messageUrl = "https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Rqyvqo&treeId=257&articleId=105735&docType=1";

Ding()->link($title,$text,$messageUrl,$picUrl)->send();

Send a message of type ActionCard

no jump link

$title = "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身";
$text = "![screenshot](@lADOpwk3K80C0M0FoA) \n".
    " #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n".
    " Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划";

Ding()->actionCard($title,$text,1)->send();

Overall jump ActionCard type (single)

$title = "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身";
$text = "![screenshot](@lADOpwk3K80C0M0FoA) \n".
    " #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n".
    " Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划";

Ding()->actionCard($title,$text,1)
    ->single("阅读全文","https://www.dingtalk.com/")
    ->send();

Independent jump ActionCard type (btns)

$title = "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身";
$text = "![screenshot](@lADOpwk3K80C0M0FoA) \n".
    " #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n".
    " Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划";

Ding()->actionCard($title,$text,1)
    ->addButtons("内容不错","https://www.dingtalk.com/")
    ->addButtons("不感兴趣","https://www.dingtalk.com/")
    ->send();

Send a message of type FeedCard

   $linksArray = [
            [
                "title"=> "时代的火车向前开1通知",
                "messageURL"=> "https://www.dingtalk.com/",
                "picURL"=> "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
            ],
            [
                "title"=> "时代的火车向前开2",
                "messageURL"=> "https://www.dingtalk.com/",
                "picURL"=> "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
            ],
            [
                "title"=> "时代的火车向前开3",
                "messageURL"=> "https://www.dingtalk.com/",
                "picURL"=> "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
            ],
            [
                "title"=> "时代的火车向前开4",
                "messageURL"=> "https://www.dingtalk.com/",
                "picURL"=> "https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png"
            ]
        ];
        (new DingBot())->feedCard($linksArray)->send();
        Ding()->feedCard($linksArray)->send();

Multi-bot messaging

Ding('bot1')->text("服务器通知报警,请紧急处理")->send();

or

(new DingBot('bot1'))->text('服务器通知报警,请紧急处理')->send();

Same as above for other message types

Effect example

Guess you like

Origin blog.csdn.net/qq_31766533/article/details/132634390