企业微信机器人sdk
composer require huo-zi/work-wechat-robot
php < 8 || laravel < 9
composer require huo-zi/work-wechat-robot:~2.0
$robot = new WorkWechatRobot($robotKey);
$robot->text($content); // 文本消息
$robot->markdown($content); // markdown消息
$robot->image($filename); // 图片消息 支持本地图片和网络图片
$robot->news($title, $url, $desc, $picurl); // 图文消息
$robot->file($filename); // 发送上传文件
$messsage = new Text(); // new Markdwon(); new Image()...
$messsage->content('文本消息');
$messsage->send($robotKey); // 或使用 $robot->message($messsage);
- laravel框架
在配置文件
logging.php
的channels
数组中增加:
'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
'stack' => [
'driver' => 'stack',
'channels' => ['single', ... , 'wxwork_robot'],
'ignore_exceptions' => false,
],
- 其他框架
$logger = new \Monolog\Logger($name);
$logger->pushHandler(new RobotHandler($robotKey));
提供了TextFormatter
和MarkdownFormatter
格式化原始日志,使日志内容方便阅读
- laravel框架,修改
logging.php
, 增加formatter
:
'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
都提供了默认的格式化结构,如果需要自定义可以:
'formatter' => \Huozi\WorkWechat\Monolog\Formatter\TextFormatter::class,
'formatter_with' => [
'messageFormat' => '{level_name}:{message} \n {extra.file}:{extra.line}'</b>
]
- 其他框架
$messageFormat = '{level_name}:{message} \n {extra.file}:{extra.line}';
$formatter = new TextFormatter($messageFormat);
$logger->pushHandler((new RobotHandler($robotKey))->setFormatter($formatter));
Licensed under The MIT License (MIT).