Zig
Zig
MessageMedia - Send Messages
See more MessageMedia Examples
Submit one or more (up to 100 per request) SMS, MMS, or text to voice messages for delivery.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 --request POST \
// --header "Content-Type: application/json" \
// --header "Accept: application/json" \
// --data-binary "{
// \"messages\": [
// {
// \"callback_url\": \"https://my.callback.url.com\",
// \"content\": \"My first message\",
// \"destination_number\": \"+61491570156\",
// \"delivery_report\": true,
// \"format\": \"SMS\",
// \"message_expiry_timestamp\": \"2016-11-03T11:49:02.807Z\",
// \"metadata\": {
// \"myKey\": \"myValue\",
// \"anotherKey\": \"anotherValue\"
// },
// \"scheduled\": \"2016-11-03T11:49:02.807Z\",
// \"source_number\": \"+61491570157\",
// \"source_number_type\": \"INTERNATIONAL\"
// },
// {
// \"callback_url\": \"https://my.callback.url.com\",
// \"content\": \"My second message\",
// \"destination_number\": \"+61491570158\",
// \"delivery_report\": true,
// \"format\": \"MMS\",
// \"subject\": \"This is an MMS message\",
// \"media\": [ \"https://images.pexels.com/photos/1018350/pexels-photo-1018350.jpeg?cs=srgb&dl=architecture-buildings-city-1018350.jpg\" ],
// \"message_expiry_timestamp\": \"2016-11-03T11:49:02.807Z\",
// \"metadata\": {
// \"myKey\": \"myValue\",
// \"anotherKey\": \"anotherValue\"
// },
// \"scheduled\": \"2016-11-03T11:49:02.807Z\",
// \"source_number\": \"+61491570159\",
// \"source_number_type\": \"INTERNATIONAL\"
// }
// ]
// }" \
// https://private-anon-ecbaffbc28-messages32.apiary-mock.com/v1/messages
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "messages": [
// {
// "callback_url": "https://my.callback.url.com",
// "content": "My first message",
// "destination_number": "+61491570156",
// "delivery_report": true,
// "format": "SMS",
// "message_expiry_timestamp": "2016-11-03T11:49:02.807Z",
// "metadata": {
// "myKey": "myValue",
// "anotherKey": "anotherValue"
// },
// "scheduled": "2016-11-03T11:49:02.807Z",
// "source_number": "+61491570157",
// "source_number_type": "INTERNATIONAL"
// },
// {
// "callback_url": "https://my.callback.url.com",
// "content": "My second message",
// "destination_number": "+61491570158",
// "delivery_report": true,
// "format": "MMS",
// "subject": "This is an MMS message",
// "media": [
// "https://images.pexels.com/photos/1018350/pexels-photo-1018350.jpeg?cs=srgb&dl=architecture-buildings-city-1018350.jpg"
// ],
// "message_expiry_timestamp": "2016-11-03T11:49:02.807Z",
// "metadata": {
// "myKey": "myValue",
// "anotherKey": "anotherValue"
// },
// "scheduled": "2016-11-03T11:49:02.807Z",
// "source_number": "+61491570159",
// "source_number_type": "INTERNATIONAL"
// }
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("messages[0].callback_url", "https://my.callback.url.com") catch {};
json.updateString("messages[0].content", "My first message") catch {};
json.updateString("messages[0].destination_number", "+61491570156") catch {};
json.updateBool("messages[0].delivery_report", true) catch {};
json.updateString("messages[0].format", "SMS") catch {};
json.updateString("messages[0].message_expiry_timestamp", "2016-11-03T11:49:02.807Z") catch {};
json.updateString("messages[0].metadata.myKey", "myValue") catch {};
json.updateString("messages[0].metadata.anotherKey", "anotherValue") catch {};
json.updateString("messages[0].scheduled", "2016-11-03T11:49:02.807Z") catch {};
json.updateString("messages[0].source_number", "+61491570157") catch {};
json.updateString("messages[0].source_number_type", "INTERNATIONAL") catch {};
json.updateString("messages[1].callback_url", "https://my.callback.url.com") catch {};
json.updateString("messages[1].content", "My second message") catch {};
json.updateString("messages[1].destination_number", "+61491570158") catch {};
json.updateBool("messages[1].delivery_report", true) catch {};
json.updateString("messages[1].format", "MMS") catch {};
json.updateString("messages[1].subject", "This is an MMS message") catch {};
json.updateString("messages[1].media[0]", "https://images.pexels.com/photos/1018350/pexels-photo-1018350.jpeg?cs=srgb&dl=architecture-buildings-city-1018350.jpg") catch {};
json.updateString("messages[1].message_expiry_timestamp", "2016-11-03T11:49:02.807Z") catch {};
json.updateString("messages[1].metadata.myKey", "myValue") catch {};
json.updateString("messages[1].metadata.anotherKey", "anotherValue") catch {};
json.updateString("messages[1].scheduled", "2016-11-03T11:49:02.807Z") catch {};
json.updateString("messages[1].source_number", "+61491570159") catch {};
json.updateString("messages[1].source_number_type", "INTERNATIONAL") catch {};
http.setRequestHeader("Content-Type", "application/json");
http.setRequestHeader("Accept", "application/json");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://private-anon-ecbaffbc28-messages32.apiary-mock.com/v1/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": [
// {
// "message_id": "04fe9a97-a579-43c5-bb1a-58ed29bf0a6a",
// "callback_url": "https://my.url.com",
// "status": "delivered",
// "content": "My first message",
// "destination_number": "+61491570156",
// "delivery_report": true,
// "format": "SMS",
// "message_expiry_timestamp": "2016-11-03T11:49:02.807Z",
// "metadata": {
// "myKey": "myValue",
// "anotherKey": "anotherValue"
// },
// "scheduled": "2016-11-03T11:49:02.807Z",
// "source_number": "+61491570157",
// "source_number_type": "INTERNATIONAL"
// }
// ]
// }
// 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 message_id: [:0]const u8 = "";
var callback_url: [:0]const u8 = "";
var status: [:0]const u8 = "";
var content: [:0]const u8 = "";
var destination_number: [:0]const u8 = "";
var delivery_report: bool = false;
var format: [:0]const u8 = "";
var message_expiry_timestamp: [:0]const u8 = "";
var my_key: [:0]const u8 = "";
var another_key: [:0]const u8 = "";
var scheduled: [:0]const u8 = "";
var source_number: [:0]const u8 = "";
var source_number_type: [:0]const u8 = "";
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("messages");
while (i < count_i) {
j_resp.setI(i);
message_id = try j_resp.stringOf(alloc, "messages[i].message_id");
callback_url = try j_resp.stringOf(alloc, "messages[i].callback_url");
status = try j_resp.stringOf(alloc, "messages[i].status");
content = try j_resp.stringOf(alloc, "messages[i].content");
destination_number = try j_resp.stringOf(alloc, "messages[i].destination_number");
delivery_report = j_resp.boolOf("messages[i].delivery_report");
format = try j_resp.stringOf(alloc, "messages[i].format");
message_expiry_timestamp = try j_resp.stringOf(alloc, "messages[i].message_expiry_timestamp");
my_key = try j_resp.stringOf(alloc, "messages[i].metadata.myKey");
another_key = try j_resp.stringOf(alloc, "messages[i].metadata.anotherKey");
scheduled = try j_resp.stringOf(alloc, "messages[i].scheduled");
source_number = try j_resp.stringOf(alloc, "messages[i].source_number");
source_number_type = try j_resp.stringOf(alloc, "messages[i].source_number_type");
i = i + 1;
}
}