飞书 自定义群机器人 自建简易通知驱动及发送通知 实例
飞书群机器人-简易文本 通知驱动
示例代码要求
- Laravel 框架 5.5以上
- guzzle
开始
- Laravel 项目 建立
Channels
文件夹 建议LarkRobotChannel
驱动
LarkRobotChannel
代码如下:
<?php
namespace App\Channels;
use GuzzleHttp\Client;
use Illuminate\Notifications\Notification;
class LarkRobotChannel
{
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
* @return void
*/
public function send($notifiable, Notification $notification)
{
$message = $notification->toLarkRobot($notifiable);
$key = $notifiable->routes['lark'];
$data = array(
"msg_type" =>"text",
"content"=> [
"text" =>$message
]
);
$client = new Client();
$key = is_array($key) ? $key : array($key);
foreach ($key as $keys) {
$url = 'https://open.feishu.cn/open-apis/bot/v2/hook/' . $keys;
$response = $client->post($url, [\GuzzleHttp\RequestOptions::JSON => $data ]);
}
}
}
- 在
App\Notifications
文件夹下 创建通知文件LarkRobotNotification
代码如下:
<?php
namespace App\Notifications;
use App\Channels\LarkRobotChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
class LarkRobotNotification extends Notification
{
use Queueable;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function via($notifiable)
{
return [LarkRobotChannel::class];
}
public function toLarkRobot($notifiable)
{
return $this->message;
}
}
- 业务代码使用 飞书通知
use Illuminate\Support\Facades\Notification;
use App\Notifications\LarkRobotNotification;
Notification::route('lark', $robotKey)
->notify(new LarkRobotNotification($content));
如需复杂效果 可以参考 飞书文档 扩展:
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: