大家好,分享一个我刚开源的 PHP 扩展包:WeComAiBot —— 企业微信 AI 机器人 PHP SDK。
GitHub: https://github.com/bangbangda/wecomaibot
企业微信官方提供了 AI 机器人的 WebSocket 长连接通道,Node.js 有官方 SDK(@wecom/aibot-node-sdk),但 PHP 生态一片空白。
传统的 HTTP 回调方式,需要公网 IP、域名、SSL 证书,还要处理 5 秒超时——光是环境准备就劝退一大半人。
所以我基于 Workerman 做了这个 SDK,通过 WebSocket 长连接与企微服务器通信,彻底省掉回调地址配置。内网、本地、开发机上都能直接跑。
composer require bangbangda/wecomaibot
没有第二步。
$bot = new WeComBot([
'bot_id' => 'your-bot-id',
'secret' => 'your-secret',
]);
$bot->onMessage(function (Message $message, Reply $reply) {
$reply->text("你好!你说的是:{$message->text}");
});
$bot->start();
php your-bot.php start
没了。机器人就活了。
php artisan wecom:serve 一键启动像 ChatGPT 一样,先显示加载动画,再逐步输出回复内容:
$bot->onMessage(function (Message $message, Reply $reply) {
// 立即显示"思考中"加载动画
$reply->stream('<think></think>', finish: false);
// 模拟 AI 处理(实际场景替换成你的 AI 接口调用)
Timer::add(2, function () use ($reply, $message) {
$reply->stream("关于「{$message->text}」,我的回答是...", finish: true);
}, [], false);
});
不用等用户说话,机器人主动找人。支持单聊和群聊:
$bot->onAuthenticated(function () use ($bot) {
// 推送给指定用户
$bot->pushToUser('zhangsan', '你好,提醒你下午 3 点有个会议');
// 推送到群聊
$bot->pushToGroup('group-chat-id', '各位注意,系统将在 5 分钟后维护');
});
推送交互式卡片,监听按钮点击,实时更新卡片状态:
// 推送卡片
$bot->pushTemplateCardToUser('zhangsan', [
'card_type' => 'button_interaction',
'main_title' => ['title' => '服务器告警', 'desc' => 'CPU 超过 90%'],
'button_list' => [
['text' => '确认', 'style' => 1, 'key' => 'confirm'],
['text' => '误报', 'style' => 2, 'key' => 'false_alarm'],
],
'task_id' => 'ALERT_001',
]);
// 监听点击 + 更新卡片
$bot->onTemplateCardEvent(function (Event $event) use ($bot) {
$bot->updateTemplateCard($event->reqId, [
'card_type' => 'button_interaction',
'main_title' => ['title' => '服务器告警', 'desc' => '已处理'],
'button_list' => [['text' => '已确认', 'style' => 1, 'key' => 'confirm']],
'task_id' => $event->eventData['task_id'] ?? '',
]);
});
1. 发布配置 + 设置环境变量:
php artisan vendor:publish --tag=wecomaibot-config
WECOM_BOT_ID=your-bot-id
WECOM_BOT_SECRET=your-bot-secret
2. 写一个 Handler:
// app/Services/WeComHandler.php
class WeComHandler
{
public function onMessage(Message $message, Reply $reply): void
{
$reply->text("Echo: {$message->text}");
}
}
3. 启动:
php artisan wecom:serve
完事。
一个进程同时跑多个机器人,销售部、财务部、客服部各一个,互不干扰:
$manager = new BotManager();
$salesBot = $manager->addBot('sales', ['bot_id' => '...', 'secret' => '...']);
$salesBot->onMessage(fn(Message $msg, Reply $reply) => $reply->text("【销售助手】{$msg->text}"));
$financeBot = $manager->addBot('finance', ['bot_id' => '...', 'secret' => '...']);
$financeBot->onMessage(fn(Message $msg, Reply $reply) => $reply->text("【财务助手】{$msg->text}"));
$manager->start();
| 方案 | 问题 |
|---|---|
| HTTP 回调 | 需要公网 IP + 域名 + SSL 证书 + 处理 5 秒超时 |
| Swoole | 需要安装 C 扩展,部署门槛高 |
| ratchet/pawl | 没有内置重连和心跳,要自己造轮子 |
| Node.js SDK | 很好,但我们是 PHP 开发者(PHP 是最好的语言.jpg) |
推荐用 Supervisor 守护常驻进程:
[program:wecom-bot]
command=php /path/to/your-bot.php start
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/wecom-bot.log
也支持 Workerman 自带的守护模式:
php your-bot.php start -d # 后台运行
php your-bot.php status # 查看状态
php your-bot.php stop # 停止
composer require bangbangda/wecomaibot欢迎 Star、Issue、PR。有问题直接在 GitHub 提 Issue 就好。
PHP 是世界上最好的语言,这件事不接受反驳。