Zig
Zig
Clickatell Send SMS Text Message
See more Clickatell Examples
Demonstrate how to send a Clickatell SMS text message.Chilkat Zig Downloads
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();
// Implements the following CURL command:
// curl -i \
// -X POST \
// -H "Content-Type: application/json" \
// -H "Accept: application/json" \
// -H "Authorization: API_KEY" \
// -d '{"content": "Test Message Text", "to": ["PHONE_NUMBER"], "from": "FROM_PHONE_NUMBER" }' \
// -s https://platform.clickatell.com/messages
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "content": "Test Message Text",
// "to": [
// "PHONE_NUMBER"
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("content", "Test Message Text") catch {};
json.updateString("to[0]", "PHONE_NUMBER") catch {};
json.updateString("from", "FROM_PHONE_NUMBER") catch {};
http.setRequestHeader("Authorization", "API_KEY");
http.setRequestHeader("Accept", "application/json");
http.setRequestHeader("Content-Type", "application/json");
// If the following URL does not work, then try "https://platform.clickatell.com/v1/message"
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://platform.clickatell.com/messages", json, "application/json", 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 j_resp = try chilkat.JsonObject.init();
defer j_resp.deinit();
j_resp.loadSb(sb_response_body) catch {};
j_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
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;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "messages": [
// {
// "apiMessageId": "66c06ac68e5c4afb953ea9328babfde6",
// "accepted": true,
// "to": "16302581871",
// "errorCode": null,
// "error": null,
// "errorDescription": null
// }
// ],
// "errorCode": null,
// "error": null,
// "errorDescription": null
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var error_code: [:0]const u8 = "";
var error_: [:0]const u8 = "";
var error_description: [:0]const u8 = "";
var i: i32 = 0;
var api_message_id: [:0]const u8 = "";
var accepted: bool = false;
var v_to: [:0]const u8 = "";
error_code = try j_resp.stringOf(alloc, "errorCode");
error_ = try j_resp.stringOf(alloc, "error");
error_description = try j_resp.stringOf(alloc, "errorDescription");
i = 0;
const count_i = j_resp.sizeOfArray("messages");
while (i < count_i) {
j_resp.setI(i);
api_message_id = try j_resp.stringOf(alloc, "messages[i].apiMessageId");
accepted = j_resp.boolOf("messages[i].accepted");
v_to = try j_resp.stringOf(alloc, "messages[i].to");
error_code = try j_resp.stringOf(alloc, "messages[i].errorCode");
error_ = try j_resp.stringOf(alloc, "messages[i].error");
error_description = try j_resp.stringOf(alloc, "messages[i].errorDescription");
i = i + 1;
}
}