Download the PHP package vergil-lai/wecom-aibot-php-sdk without Composer

On this page you can find all versions of the php package vergil-lai/wecom-aibot-php-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package wecom-aibot-php-sdk

wecom-aibot-php-sdk

企业微信智能机器人 PHP SDK —— 基于 ReactPHP WebSocket 长连接通道,提供消息收发、流式回复、模板卡片、事件回调、文件下载解密、媒体素材上传等核心能力。

本 SDK 从官方 Node.js SDK 移植而来,API 设计保持一致。

特性

安装

系统要求

快速开始


API 文档

WSClient

核心客户端类,继承自 Evenement\EventEmitter,提供连接管理、消息收发等功能。

构造函数

方法一览

方法 说明 返回值
connect() 建立 WebSocket 连接,连接后自动认证 $this(支持链式调用)
disconnect() 主动断开连接 void
reply(frame, body, cmd?) 通过 WebSocket 通道发送回复消息(通用方法) WsFrame
replyStream(frame, streamId, content, finish?, msgItem?, feedback?) 发送流式文本回复(支持 Markdown) WsFrame
replyWelcome(frame, body) 发送欢迎语回复(文本或模板卡片),需 5s 内调用 WsFrame
replyTemplateCard(frame, templateCard, feedback?) 回复模板卡片消息 WsFrame
replyStreamWithCard(frame, streamId, content, finish?, options?) 流式消息 + 模板卡片组合回复 WsFrame
updateTemplateCard(frame, templateCard, userIds?) 更新模板卡片(响应 template_card_event),需 5s 内调用 WsFrame
sendMessage(chatId, body) 主动发送消息(Markdown / 模板卡片 / 媒体),无需回调帧 WsFrame
uploadMedia(filePath, options) 上传临时素材(三步分片上传),返回 media_id PromiseInterface<UploadMediaFinishResult>
replyMedia(frame, mediaType, mediaId, videoOptions?) 被动回复媒体消息(file/image/voice/video) WsFrame
sendMediaMessage(chatId, mediaType, mediaId, videoOptions?) 主动发送媒体消息 WsFrame
downloadFile(url, aesKey?) 下载文件并 AES 解密 DownloadFileResult
on(event, listener) 注册事件监听器(继承自 EventEmitter) $this
once(event, listener) 注册一次性事件监听器 $this
off(event, listener?) 移除事件监听器 $this

属性

属性 说明 类型
isConnected() 当前 WebSocket 连接状态 bool
getApiClient() 内部 API 客户端实例(高级用途) WeComApiClient
getConnectionManager() 连接管理器(高级用途) WsConnectionManager

replyStream 详细说明

发送流式文本回复(便捷方法,支持 Markdown)。

使用示例:


replyWelcome 详细说明

发送欢迎语回复,需在收到 event.enter_chat 事件 5 秒内调用,超时将无法发送。


replyStreamWithCard 详细说明

发送流式消息 + 模板卡片组合回复。

$options 支持以下键:

键 类型 说明
msgItem ReplyMsgItem[] 图文混排项(仅 finish=true 时有效)
streamFeedback ReplyFeedback 流式消息反馈信息(首次回复时设置)
templateCard TemplateCard 模板卡片内容(同一消息只能回复一次)
cardFeedback ReplyFeedback 模板卡片反馈信息

sendMessage 详细说明

主动向指定会话推送消息,无需依赖收到的回调帧。


uploadMedia 详细说明

通过 WebSocket 长连接执行三步分片上传:init -> chunk x N -> finish。


downloadFile 详细说明

下载文件并 AES-256-CBC 解密(同步方法,内部使用 ReactPHP 事件循环)。


配置选项

WsClientOptions 完整配置:

参数 类型 必填 默认值 说明
botId string 是 — 机器人 ID(企业微信后台获取)
secret string 是 — 机器人 Secret(企业微信后台获取)
scene ?int — null 场景值(可选)
plugVersion ?string — null 插件版本号(可选)
reconnectInterval int — 1000 重连基础延迟(毫秒),实际延迟按指数退避递增
maxReconnectAttempts int — 10 最大重连次数(-1 表示无限重连)
maxAuthFailureAttempts int — 5 认证失败最大重试次数(-1 表示无限重试)
heartbeatInterval int — 30000 心跳间隔(毫秒)
requestTimeout int — 10000 HTTP 请求超时时间(毫秒)
wsUrl string — wss://openws.work.weixin.qq.com 自定义 WebSocket 连接地址
maxReplyQueueSize int — 500 单个 req_id 回复队列最大长度
logger ?LoggerInterface — DefaultLogger 自定义日志实例

事件列表

所有事件均通过 $wsClient->on(event, handler) 监听(继承自 Evenement\EventEmitter):

事件 回调参数 说明
connected — WebSocket 连接建立
authenticated — 认证成功
disconnected string $reason 连接断开
reconnecting int $attempt 正在重连(第 N 次)
error \Throwable $error 发生错误
message WsFrame $frame 收到消息(所有类型)
message.text WsFrame $frame 收到文本消息
message.image WsFrame $frame 收到图片消息
message.mixed WsFrame $frame 收到图文混排消息
message.voice WsFrame $frame 收到语音消息
message.file WsFrame $frame 收到文件消息
message.video WsFrame $frame 收到视频消息
event WsFrame $frame 收到事件回调(所有事件类型)
event.enter_chat WsFrame $frame 收到进入会话事件(用户当天首次进入单聊会话)
event.template_card_event WsFrame $frame 收到模板卡片事件(用户点击卡片按钮)
event.feedback_event WsFrame $frame 收到用户反馈事件

消息类型

SDK 支持以下消息类型(MessageType 枚举):

类型 值 说明
Text 'text' 文本消息
Image 'image' 图片消息(URL 已加密,使用消息中的 image.aeskey 解密)
Mixed 'mixed' 图文混排消息(包含 text / image 子项)
Voice 'voice' 语音消息(已转文本)
File 'file' 文件消息(URL 已加密,使用消息中的 file.aeskey 解密)
Video 'video' 视频消息(URL 已加密,使用消息中的 video.aeskey 解密)

SDK 支持以下事件类型(EventType 枚举):

类型 值 说明
EnterChat 'enter_chat' 用户当天首次进入机器人单聊会话
TemplateCardEvent 'template_card_event' 用户点击模板卡片按钮
FeedbackEvent 'feedback_event' 用户对机器人回复进行反馈

SDK 支持以下媒体类型(WeComMediaType 枚举):

类型 值
File 'file'
Image 'image'
Voice 'voice'
Video 'video'

模板卡片类型

SDK 支持以下模板卡片类型(TemplateCardType 枚举):

类型 值 说明
TextNotice 'text_notice' 文本通知模版卡片
NewsNotice 'news_notice' 图文展示模版卡片
ButtonInteraction 'button_interaction' 按钮交互模版卡片
VoteInteraction 'vote_interaction' 投票选择模版卡片
MultipleInteraction 'multiple_interaction' 多项选择模版卡片

消息帧结构

WsFrame


自定义日志

实现 \Psr\Log\LoggerInterface 即可自定义日志输出:

使用示例:


WebSocket 命令协议

方向 常量 值 说明
开发者 -> 企微 SUBSCRIBE aibot_subscribe 认证订阅
开发者 -> 企微 HEARTBEAT ping 心跳
开发者 -> 企微 RESPONSE aibot_respond_msg 回复消息
开发者 -> 企微 RESPONSE_WELCOME aibot_respond_welcome_msg 回复欢迎语
开发者 -> 企微 RESPONSE_UPDATE aibot_respond_update_msg 更新模板卡片
开发者 -> 企微 SEND_MSG aibot_send_msg 主动发送消息
开发者 -> 企微 UPLOAD_MEDIA_INIT aibot_upload_media_init 上传素材 - 初始化
开发者 -> 企微 UPLOAD_MEDIA_CHUNK aibot_upload_media_chunk 上传素材 - 分片
开发者 -> 企微 UPLOAD_MEDIA_FINISH aibot_upload_media_finish 上传素材 - 完成
企微 -> 开发者 CALLBACK aibot_msg_callback 消息推送回调
企微 -> 开发者 EVENT_CALLBACK aibot_event_callback 事件推送回调

项目结构


完整使用示例

流式回复

上传素材 + 回复媒体消息

主动推送消息

模板卡片交互

文件下载解密


License

MIT


All versions of wecom-aibot-php-sdk with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
psr/log Version ^2.0 || ^3.0
react/event-loop Version ^1.6
react/promise Version ^3.0
react/socket Version ^1.16
react/http Version ^1.10
ratchet/pawl Version ^0.4.3
evenement/evenement Version ^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package vergil-lai/wecom-aibot-php-sdk contains the following files

Loading the files please wait ...