Sample code for 30+ languages & platforms
Zig

WaTrend Send WhatsApp Text

See more WaTrend Examples

Send a WhatsApp text.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Use your actual access token instead of 555555555555555555555555555555
    const req = try chilkat.HttpRequest.init();
    defer req.deinit();
    req.addParam("number", "84933313xxx");
    req.addParam("type", "text");
    req.addParam("message", "This is a test message");
    req.addParam("instance_id", "609ACF283XXXX");
    req.addParam("access_token", "555555555555555555555555555555");

    // Note: The WaTrend online documentation indicate a POST should be used.
    // However, it seems you might actually need to send a GET request.
    // It is unclear.
    // If a GET is neeed, you would just send to the URL w/ query params like this:
    const sb_url = try chilkat.StringBuilder.init();
    defer sb_url.deinit();
    sb_url.append("https://app.watrend.com/api/send.php?") catch {};
    sb_url.append(try req.getUrlEncodedParams(alloc)) catch {};
    _ = try http.quickGetStr(alloc, try sb_url.getAsString(alloc));
    // The responseBodyStr contains the JSON response from the server..

    req.setHttpVerb("POST");
    req.setContentType("application/x-www-form-urlencoded");

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpReq("https://app.watrend.com/api/send.php", req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};

    const resp_status_code = resp.getStatusCode();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});

    // Both success and failed responses use 200 status code.

    // A success response contains this JSON in the response body:
    // {"status":"success", ... }

    // A failed response will contain something like this:
    // {"status":"error","message":"License Invalidated"}

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.loadSb(sb_response_body) catch {};

    const status = try j_resp.stringOf(alloc, "status");
    std.debug.print("status: {s}\n", .{status});
}