接收 astrbot_plugin_push_lite 状态回执
在 ThinkPHP 6 (TP6) 框架中,接收 astrbot_plugin_push_lite 状态回执的方法非常简单。你需要创建一个路由和一个控制器来接收这个接口发送的 POST 请求。
1. 路由配置 (Route)
在项目的路由文件(通常是 route/app.php)中,添加一个用于接收 Webhook 的路由:
php
use think\facade\Route;
// 定义接收状态回执的 POST 路由
Route::post('webhook/receipt', 'WebhookController/receiveReceipt');
请谨慎使用此类代码。
2. 控制器实现 (Controller)
在你的控制器目录(例如 app\controller)下,创建 WebhookController.php 文件,并编写以下代码来处理和解析 JSON 数据:
php
<?php
namespace app\controller;
use think\Request;
use think\facade\Log;
class WebhookController
{
/**
* 接收并处理 AstrBot 状态回执
*/
public function receiveReceipt(Request $request)
{
// 1. 获取完整的原始 JSON 请求数据
$data = $request->post();
// 2. 解析核心字段
$msgId = $data['message_id'] ?? '';
$status = $data['status'] ?? '';
$error = $data['error_message'] ?? null;
// 3. 业务逻辑处理(这里以记入日志为例)
if ($status === 'success') {
// 消息发送成功后的逻辑
Log::info("AstrBot 推送成功 - 消息ID: {$msgId}");
} else {
// 消息发送失败后的逻辑
Log::error("AstrBot 推送失败 - 消息ID: {$msgId}, 原因: {$error}");
}
// 4. 必须向服务器响应 200 状态码和规范格式(通常返回 JSON 即可)
return json([
'code' => 200,
'msg' => 'success'
]);
}
}
请谨慎使用此类代码。
3. 在调用推送时配置该地址
当你在系统(或其他脚本)中请求 POST /send 时,将 callback_url 指向你刚刚在 TP6 中配置的完整 URL:
bash
http://你的TP6网站域名或IP/index.php/webhook/receipt
请谨慎使用此类代码。
(注意:如果你的 TP6 配置了伪静态,可以省略中间的 index.php/)
💡 进阶开发建议
解除 CSRF/全局中间件拦截:如果你的 TP6 开启了全局的 Token 验证或 Session 安全中间件,请确保该路由从中间件的白名单中排除,否则可能导致接口报
403或被拦截。数据校验:由于此接口暴露在公网,建议在发送时给
callback_url后面加上自定义的签名参数(例如?token=abcdefg),在 TP6 控制器中用$request->get('token')进行验证,防止被恶意请求伪造回执。
你需要我为你编写在 TP6 中主动调用推送接口(发送请求)的代码示例吗?
需要
在 ThinkPHP 6 (TP6) 中,你可以利用 TP6 自带的 think\facade\Http 客户端组件来优雅地发起 POST 请求,主动向 AstrBot 推送消息。
以下是完整的控制器代码实现,包含普通文本推送和图片推送两种场景:
1. 配置文件 (可选,建议抽取)
为了方便管理,建议在系统的配置文件中(如 config/app.php)加入 AstrBot 的配置项:
php
return [
// 其他配置...
'astrbot' => [
'api_url' => 'http://你的AstrBot服务器IP:端口/send',
'token' => '你的插件Token',
],
];
请谨慎使用此类代码。
2. 控制器实现 (Controller)
在你的控制器中(例如 app\controller\PushController.php),编写主动推送的方法:
php
<?php
namespace app\controller;
use think\facade\Config;
use think\facade\Http;
use think\facade\Log;
class PushController
{
/**
* 主动推送文本消息
*/
public function sendText()
{
// 1. 获取配置信息
$apiUrl = Config::get('app.astrbot.api_url');
$token = Config::get('app.astrbot.token');
// 2. 组装请求参数
$params = [
'umo' => 'TARGET_SESSION_ID', // 替换为真实的会话ID
'message_type' => 'text',
'content' => 'Hello! 这是从 ThinkPHP 6 系统自动推送的一条文本消息。',
'callback_url' => 'http://你的TP6域名/webhook/receipt' // 上一步配置的回执地址
];
// 3. 使用 TP6 的 Http 客户端发送请求
try {
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json'
])->post($apiUrl, $params);
// 4. 解析响应状态
if ($response->ok()) {
$result = $response->json();
return json(['code' => 200, 'msg' => '已进入队列', 'data' => $result]);
} else {
Log::error('AstrBot 请求失败,状态码:' . $response->status());
return json(['code' => 500, 'msg' => '接口请求失败']);
}
} catch (\Exception $e) {
Log::error('AstrBot 连接异常:' . $e->getMessage());
return json(['code' => 500, 'msg' => '服务器内部异常']);
}
}
/**
* 主动推送图片消息(支持本地图片和网络图片)
*/
public function sendImage()
{
$apiUrl = Config::get('app.astrbot.api_url');
$token = Config::get('app.astrbot.token');
// 示例:将本地文件转为 Base64
$imagePath = public_path() . 'uploads/test.png';
if (!file_exists($imagePath)) {
return json(['code' => 400, 'msg' => '图片文件不存在']);
}
$imageData = file_get_contents($imagePath);
$base64 = 'data:image/png;base64,' . base64_encode($imageData);
$params = [
'umo' => 'TARGET_SESSION_ID',
'message_type' => 'image',
'content' => $base64,
'callback_url' => 'http://你的TP6域名/webhook/receipt'
];
// 发送请求
$response = Http::withHeaders([
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json'
])->post($apiUrl, $params);
return json($response->json());
}
}
请谨慎使用此类代码。
3. 环境排查提示
缺少
think-http组件:如果你在代码中引入use think\facade\Http;报错,说明你的 TP6 环境尚未安装该官方组件。请在项目根目录下执行以下命令安装:bash
composer require topthink/think-http请谨慎使用此类代码。
超时时间:推送大图(长 Base64 字符串)时,如果本地网络慢,可能会导致接口超时。你可以使用
Http::timeout(10)->post(...)来延长客户端等待时间(单位为秒)。