这是一个企业微信群机器人的sdk
0.安装
composer require huo-zi/work-wechat-robot
php < 8 || laravel < 9
composer require huo-zi/work-wechat-robot:~2.0
1.使用
1.1 直接使用
1 2 3 4 5 6
| $robot = new WorkWechatRobot($robotKey); $robot->text($content); $robot->markdown($content); $robot->image($filename); $robot->news($title, $url, $desc, $picurl); $robot->file($filename);
|
1.2 v2.1.0之后支持创建消息对象发送消息:
1 2 3
| $messsage = new Text(); $messsage->content('文本消息'); $messsage->send($robotKey);
|
2.作为Monolog的通道使用
2.1 配置通道
- laravel框架
在配置文件logging.php
的channels
数组中增加:
1 2 3 4 5 6 7 8
| 'wxwork_robot' => [ 'driver' => 'monolog', 'level' => 'notice', 'handler' => \Huozi\WorkWechat\Monolog\Handler\RobotHandler::class, 'handler_with' => [ 'robotKey' => 'your_wxwork_robot_key', ], ],
|
然后修改channels
节点stack
,在channels
中增加wxwork_robot
1 2 3 4 5
| 'stack' => [ 'driver' => 'stack', 'channels' => ['single', ... , 'wxwork_robot'], 'ignore_exceptions' => false, ],
|
详见laravel高度自定义Monolog通道
1 2
| $logger = new \Monolog\Logger($name); $logger->pushHandler(new RobotHandler($robotKey));
|
2.2 日志格式化
提供了TextFormatter
和MarkdownFormatter
格式化原始日志,使日志内容方便阅读
- laravel框架,修改
logging.php
, 增加formatter
:
1 2 3 4 5 6 7 8 9
| 'wxwork_robot' => [ 'driver' => 'monolog', 'level' => 'notice', 'handler' => \Huozi\WorkWechat\Monolog\Handler\RobotHandler::class, 'handler_with' => [ 'robotKey' => 'your_wxwork_robot_key', ], 'formatter' => \Huozi\WorkWechat\Monolog\Formatter\MarkdownFormatter::class,``` ],
|
TextFormatter
和MarkdownFormatter
都提供了默认的格式化结构,如果需要自定义可以:
1 2 3 4
| 'formatter' => \Huozi\WorkWechat\Monolog\Formatter\TextFormatter::class, 'formatter_with' => [ 'messageFormat' => '{level_name}:{message} \n {extra.file}:{extra.line}'</b> ]
|
1 2 3
| $messageFormat = '{level_name}:{message} \n {extra.file}:{extra.line}'; $formatter = new TextFormatter($messageFormat); $logger->pushHandler((new RobotHandler($robotKey))->setFormatter($formatter));
|
License
Licensed under The MIT License (MIT).
GIT地址:work-wechat-robot