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: The resource methods
| Method | Sends |
|---|---|
text($to, $text) | a text message |
image($to, $file, $text) | an image with optional caption |
document($to, $file, $text) | a file as a document |
audio($to, $file) / voice($to, $file) | audio / a voice note |
location($to, $lat, $lon, $text) | a location pin |
poll($to, $name, $options, $multi) | a poll |
reaction($msg, $emoji) | an emoji reaction |
read($chat) | marks a chat read |
status() / health() / qr() | gateway state and the login QR |
Media methods take a Phlo file object. An uploaded file arrives in the payload as a file already, so you can pass it straight through:
route both POST send image {
$wa = %WhatsApp($url, $secret)
$wa->image($this->contact(%payload->to), %payload->image, (string)%payload->caption)
}2.2: 发送可能会失败;请做好准备。
每个调用都会通过网络传输到网关,而网关可能处于离线状态或未连接。使用 Wrap 发送,以便在失败时向用户呈现一个干净的消息,而不是错误页面。演示在失败时返回一个包含网关错误的 toast,而在成功时则返回一个成功的 toast。
最近更新于 2026年11月5日