2: Sending messages

Construct the client from your gateway's URL and secret, then call a method per message type.

$wa = %WhatsApp($url, $secret)
$wa->text('31612345678@c.us', 'Hello from Phlo')

A contact is a WhatsApp id like 31612345678@c.us (individual) or a ...@g.us (group). The demo accepts a plain number and appends @c.us for you.

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(%req->to), %req->image, (string)%req->caption)
}

2.2: Sends can fail; expect it

Every call crosses the network to the gateway, which may be offline or unlinked. Wrap sends so a failure becomes a clean message to the user instead of an error page. The demo returns a toast with the gateway's error on failure and a success toast otherwise.

We use essential cookies to make this site work. With your permission we also use analytics to improve the site.