配置企业微信机器人
laravel配置
创建企业微信通知类
php artisan make:notification WxWorkRobotNotification
生成文件路径
app/Notifications/WxWorkRobotNotification.php
文件内容
<?php
namespace App\Notifications;
use App\Channels\WxWorkRobotChannel;
use App\Service\WxWorkRobotService;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
/*
* 使用示例
* Notification::send(['这是一个智函日志']), new WxWorkRobotNotification()); //单条日志
* Notification::send(['这是一个智函日志', '第二条日志']), new WxWorkRobotNotification()); //多条日志
* Notification::send(json_encode(['name' => '这是一个智函日志','info' => '其他信息']), new WxWorkRobotNotification()); //1次发送大量数据
* */
class WxWorkRobotNotification extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [WxWorkRobotChannel::class];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return mixed
*/
public function toWxWorkRobot($notifiable)
{
app(WxWorkRobotService::class)->log($notifiable);
}
}
创建企业微信通知频道类
app/Channels/WxWorkRobotChannel.php
在该目录下创建WxWorkRobotChannel.php类
文件内容
<?php
namespace App\Channels;
use Illuminate\Notifications\Notification;
class WxWorkRobotChannel
{
/**
* 发送指定的通知.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$notification->toWxWorkRobot($notifiable);
}
}
创建企业微信机器人服务类
app/Service/WxWorkRobotService.php
文件内容
<?php
/**
* Created by PhpStorm.
* User: hp
* Date: 2020/8/17
* Time: 9:36
*/
namespace App\Service;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
/*
* 参考文档
* https://work.weixin.qq.com/help?person_id=1&doc_id=13376
* */
class WxWorkRobotService
{
/**
* @param mixed $notifiable
*/
public function log($notifiable)
{
$url = config('admin.log_wxwork_webhook_url');
$body = $this->getTextBody($notifiable);
$client = new Client();
$res = $client->post($url, [
'json' => $body,
'verify' => false,
]);
$resContent = $res->getBody()->getContents();
$resContentArr = json_decode($resContent, true);
if ($resContentArr['errcode'] !== 0) {
Log::error($resContent);
}
}
public function getTextBody($notifiable)
{
return [
'msgtype' => 'text',
'text' => [
'content' => $this->format($notifiable),
],
];
}
public function getMarkdownBody($notifiable)
{
return [
'msgtype' => 'markdown',
'markdown' => [
'content' => $this->format($notifiable),
],
];
}
private function format($notifiable)
{
return $notifiable;
}
}
使用方式
可以通过通知类门面在任意位置使用使用,示例如下
Notification::send(['这是一个智函日志']), new WxWorkRobotNotification()); //单条日志
Notification::send(['这是一个智函日志', '第二条日志']), new WxWorkRobotNotification()); //多条日志
Notification::send(json_encode(['name' => '这是一个智函日志','info' => '其他信息']), new WxWorkRobotNotification()); //1次发送大量数据
也可以直接使用服务类实例化后使用
app(WxWorkRobotService::class)->log($notifiable);