PHPackages                             calchen/laravel-dingtalk-robot-notification - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. calchen/laravel-dingtalk-robot-notification

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

calchen/laravel-dingtalk-robot-notification
===========================================

钉钉智能群助手 Laravel/Lumen 消息通知扩展包（Dingtalk robot message notifications for Laravel/Lumen）

2.3.1(4y ago)2326.4k↑400%11[1 issues](https://github.com/calchen/laravel-dingtalk-robot-notification/issues)MITPHPPHP ^7.0

Since Jan 17Pushed 4y ago1 watchersCompare

[ Source](https://github.com/calchen/laravel-dingtalk-robot-notification)[ Packagist](https://packagist.org/packages/calchen/laravel-dingtalk-robot-notification)[ RSS](/packages/calchen-laravel-dingtalk-robot-notification/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (18)Used By (0)

 laravel-dingtalk-robot-notification
=====================================

[](#-laravel-dingtalk-robot-notification-)

 钉钉群机器人 Laravel/Lumen 扩展包

 [ ![Style CI](https://camo.githubusercontent.com/745fabc6dde99f1a255d143ede1dc3ad5855e63c21b475d97c93e687d6f5f342/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3136363139363232312f736869656c643f7374796c653d666c6174) ](https://github.styleci.io/repos/166196221) [ ![Travis CI](https://camo.githubusercontent.com/7a8f1037deadd5d86d421cd82c424cb22004a4e37a65119e5e30518981c026c7/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f63616c6368656e2f6c61726176656c2d64696e6774616c6b2d726f626f742d6e6f74696669636174696f6e2e737667) ](https://travis-ci.com/calchen/laravel-dingtalk-robot-notification) [ ![Coverage Status](https://camo.githubusercontent.com/d9325d3c198997a9cb9f34854868134a7e8d93e16e1d87929a916e529369f103/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f63616c6368656e2f6c61726176656c2d64696e6774616c6b2d726f626f742d6e6f74696669636174696f6e2f62616467652e7376673f6272616e63683d6d6173746572) ](https://coveralls.io/github/calchen/laravel-dingtalk-robot-notification?branch=master) [ ![Latest Stable Version](https://camo.githubusercontent.com/e470daa2a118b0a050d54ca11f42b0509c35d5ba8b6ec192539b75fac98e8c9c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63616c6368656e2f6c61726176656c2d64696e6774616c6b2d726f626f742d6e6f74696669636174696f6e2e737667) ](https://packagist.org/packages/calchen/laravel-dingtalk-robot-notification) [ ![Total Downloads](https://camo.githubusercontent.com/25b0be1dd60b11b3d6651d59bf03aa69be3f9c687c645e6a00bc58ba64250d39/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63616c6368656e2f6c61726176656c2d64696e6774616c6b2d726f626f742d6e6f74696669636174696f6e2e737667) ](https://packagist.org/packages/calchen/laravel-dingtalk-robot-notification) [ ![License](https://camo.githubusercontent.com/5c25449fb568a8072e6f4ab9fa639e876579428c84718ede3c40aa2cd4fc4c4a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f63616c6368656e2f6c61726176656c2d64696e6774616c6b2d726f626f742d6e6f74696669636174696f6e2e737667) ](https://github.com/calchen/laravel-dingtalk-robot-notification/blob/master/LICENSE)

> [English](https://github.com/calchen/laravel-dingtalk-robot-notification/blob/master/README_en.md)

这是一个[钉钉群机器人](https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq)的 Laravel/Lumen 消息通知（Notification）扩展包

安装
--

[](#安装)

推荐使用 composer 进行安装：

```
$ composer require calchen/laravel-dingtalk-robot-notification:^2.0
```

### Laravel

[](#laravel)

Laravel 5.5+ 已经实现了扩展包发现机制，您不需要进行额外的加载操作，但是依然需要将配置文件发布出来：

```
php artisan vendor:publish --provider="Calchen\LaravelDingtalkRobot\DingtalkRobotNoticeServiceProvider"
```

### Lumen

[](#lumen)

Lumen 并未移植扩展包自动发现机制，所以需要手动加载扩展包并复制配置文件。

打开配置文件 `bootstrap/app.php` 并在大约 81 行左右添加如下内容：

```
$app->register(Calchen\LaravelDingtalkRobot\DingtalkRobotNoticeServiceProvider::class);
```

将文件系统配置文件从 `vendor/calchen/laravel-dingtalk-robot-notification/config/dingtalk_robot.php` 复制到 `config/dingtalk_robot.php`

### 非 Laravel/Lumen 框架

[](#非-laravellumen-框架)

无需考虑加载问题，请使用全局函数 `\dingtalk_robot()` 或直接创建 \\Calchen\\LaravelDingtalkRobot\\DingtalkRobot 实例，以发送消息。

配置
--

[](#配置)

打开配置文件 `config/dingtalk_robot.php` 并按照如下格式添加或修改配置：

```
'robotName' => [
    'access_token' => 'xxxx',
    'timeout' => 2.0,
    'security_types' => [
        'signature',
    ],
    'security_signature' => 'SECxxxx',
],
```

请注意，如果需要配置多个机器人，请重复以上操作，并为不同机器人给予不同的 robotName

### 配置说明

[](#配置说明)

配置项必须数据类型说明备注http\_client\_name否string/nullGuzzle 实例的名称默认值：null，注入在 Laravel 中的 Guzzle 实例的名称，以便替换 HTTP 客户端robotName是string机器人名称这个名称为了区别不同的机器人access\_token是string创建机器人后 Webhook URL 中 access\_token 的值timeout否int/float超时时间默认值：2.0秒，具体见 [Guzzle 文档](http://docs.guzzlephp.org/en/stable/request-options.html#timeout)security\_types是array安全设置旧机器人是不存在该项配置的传 null 或不设置该配置项；新机器人可以组合选择：自定义关键字、加签、IP地址（段）security\_types.\*是string/null设置项枚举值：null、keywords、signature、ipsecurity\_signature否string安全模式包含加签时需要的密钥字符串应当以 SEC 开头### 获取 access\_token 并设置安全设置

[](#获取-access_token-并设置安全设置)

首先在钉钉群选择添加一个群机器人（智能群助手），如果您不知道如何设置请查看 [钉钉文档](https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq)，请注意这里需要设置的是"自定义"类型的机器人。

根据您的需要设置机器人名字，并选择安全设置。在完成后您将获得一个 webhook 地址，该地址中 access\_token 的值即配置中使用到的 access\_token 的值， 请妥善保存该 access\_token。选择的安全设置就是配置中 security\_types 的值，如果您选择了“加签”的安全设置，您还需要妥善保存密钥，该密钥即配置中 security\_signature 的值

### HTTP 客户端注入

[](#http-客户端注入)

为了方便在某些情况下需要统一管理扩展包使用的 HTTP 客户端，提供了 `http_client_name` 配置项，以实现从 Laravel 容器中获取已经注入的 Guzzle 实例。如果您不需要手动管理 HTTP 客户端，您可以不设置该配置项或将该配置项值设置成 null。

使用方法
----

[](#使用方法)

Tips：为了方便快速调试功能，内置了一个使用了 Notifiable Trait 的类：Calchen\\LaravelDingtalkRobot\\Robot ，以下均以此对象为例，实际开发中请务必根据您项目情况进行对应处理。

首先需要先创建一个 TestDingtalkNotification ，如果是 Laravel 可通过 artisan 命令创建

```
php artisan make:notification TestDingtalkNotification
```

如果是 Lumen 那么可能需要您手动创建 app/Notifications 文件夹并创建 TestDingtalkNotification.php 文件

```
