配置企业微信机器人

https://work.weixin.qq.com/help?person_id=1&doc_id=13376

laravel配置

创建企业微信通知类

  1. php artisan make:notification WxWorkRobotNotification

生成文件路径

app/Notifications/WxWorkRobotNotification.php

文件内容

  1. <?php
  2. namespace App\Notifications;
  3. use App\Channels\WxWorkRobotChannel;
  4. use App\Service\WxWorkRobotService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Notifications\Notification;
  7. /*
  8. * 使用示例
  9. * Notification::send(['这是一个智函日志']), new WxWorkRobotNotification()); //单条日志
  10. * Notification::send(['这是一个智函日志', '第二条日志']), new WxWorkRobotNotification()); //多条日志
  11. * Notification::send(json_encode(['name' => '这是一个智函日志','info' => '其他信息']), new WxWorkRobotNotification()); //1次发送大量数据
  12. * */
  13. class WxWorkRobotNotification extends Notification
  14. {
  15. use Queueable;
  16. /**
  17. * Create a new notification instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct()
  22. {
  23. //
  24. }
  25. /**
  26. * Get the notification's delivery channels.
  27. *
  28. * @param mixed $notifiable
  29. * @return array
  30. */
  31. public function via($notifiable)
  32. {
  33. return [WxWorkRobotChannel::class];
  34. }
  35. /**
  36. * Get the array representation of the notification.
  37. *
  38. * @param mixed $notifiable
  39. * @return mixed
  40. */
  41. public function toWxWorkRobot($notifiable)
  42. {
  43. app(WxWorkRobotService::class)->log($notifiable);
  44. }
  45. }

创建企业微信通知频道类

app/Channels/WxWorkRobotChannel.php

在该目录下创建WxWorkRobotChannel.php类

文件内容

  1. <?php
  2. namespace App\Channels;
  3. use Illuminate\Notifications\Notification;
  4. class WxWorkRobotChannel
  5. {
  6. /**
  7. * 发送指定的通知.
  8. *
  9. * @param mixed $notifiable
  10. * @param \Illuminate\Notifications\Notification $notification
  11. * @return void
  12. */
  13. public function send($notifiable, Notification $notification)
  14. {
  15. $notification->toWxWorkRobot($notifiable);
  16. }
  17. }

创建企业微信机器人服务类

app/Service/WxWorkRobotService.php

文件内容

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hp
  5. * Date: 2020/8/17
  6. * Time: 9:36
  7. */
  8. namespace App\Service;
  9. use GuzzleHttp\Client;
  10. use Illuminate\Support\Facades\Log;
  11. /*
  12. * 参考文档
  13. * https://work.weixin.qq.com/help?person_id=1&doc_id=13376
  14. * */
  15. class WxWorkRobotService
  16. {
  17. /**
  18. * @param mixed $notifiable
  19. */
  20. public function log($notifiable)
  21. {
  22. $url = config('admin.log_wxwork_webhook_url');
  23. $body = $this->getTextBody($notifiable);
  24. $client = new Client();
  25. $res = $client->post($url, [
  26. 'json' => $body,
  27. 'verify' => false,
  28. ]);
  29. $resContent = $res->getBody()->getContents();
  30. $resContentArr = json_decode($resContent, true);
  31. if ($resContentArr['errcode'] !== 0) {
  32. Log::error($resContent);
  33. }
  34. }
  35. public function getTextBody($notifiable)
  36. {
  37. return [
  38. 'msgtype' => 'text',
  39. 'text' => [
  40. 'content' => $this->format($notifiable),
  41. ],
  42. ];
  43. }
  44. public function getMarkdownBody($notifiable)
  45. {
  46. return [
  47. 'msgtype' => 'markdown',
  48. 'markdown' => [
  49. 'content' => $this->format($notifiable),
  50. ],
  51. ];
  52. }
  53. private function format($notifiable)
  54. {
  55. return $notifiable;
  56. }
  57. }

使用方式

  1. 可以通过通知类门面在任意位置使用使用,示例如下

    1. Notification::send(['这是一个智函日志']), new WxWorkRobotNotification()); //单条日志
    2. Notification::send(['这是一个智函日志', '第二条日志']), new WxWorkRobotNotification()); //多条日志
    3. Notification::send(json_encode(['name' => '这是一个智函日志','info' => '其他信息']), new WxWorkRobotNotification()); //1次发送大量数据
  2. 也可以直接使用服务类实例化后使用

    1. app(WxWorkRobotService::class)->log($notifiable);