2: 发送消息
从您的网关的 URL 和密钥构建客户端,然后根据消息类型调用方法。
$wa = %WhatsApp($url, $secret)
$wa->text('31612345678@c.us', 'Hello from Phlo')
联系人是一个 WhatsApp ID,像 31612345678@c.us(个人)或 ...@g.us(群组)。演示接受一个普通号码,并为您附加 @c.us。
2.1: 资源方法
| 方法 | 发送内容 |
|---|---|
text($to, $text) |
一条文本消息 |
image($to, $file, $text) |
一张带可选说明的图片 |
document($to, $file, $text) |
作为文档的文件 |
audio($to, $file) / voice($to, $file) |
音频 / 一条语音消息 |
location($to, $lat, $lon, $text) |
一个位置标记 |
poll($to, $name, $options, $multi) |
一项投票 |
reaction($msg, $emoji) |
一个表情反应 |
read($chat) |
标记聊天为已读 |
status() / health() / qr() |
网关状态和登录二维码 |
媒体方法接受一个 Phlo file 对象。上传的文件在有效载荷中已经作为 file 到达,因此您可以直接传递它:
route both POST send image {
$wa = %WhatsApp($url, $secret)
$wa->image($this->contact(%req->to), %req->image, (string)%req->caption)
}2.2: 发送可能会失败;请做好准备。
每个调用都会通过网络传输到网关,而网关可能处于离线状态或未连接。使用 Wrap 发送,以便在失败时向用户呈现一个干净的消息,而不是错误页面。演示在失败时返回一个包含网关错误的 toast,而在成功时则返回一个成功的 toast。