Zig
Zig
MedTunnel: Send Message with File Attachment
See more MedTunnel Examples
Demonstrates the MedTunnel SendMessage method to send a message with a file attachment to one or more recipients.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 https://server.medtunnel.com/MedTunnelMsg/api/Message/SendMessage -X POST -k
// -F "ApplicationId=yourApplicationId" -F "LocationId=yourLocationId"
// -F "MedTunnelId=yourMedTunnelId" -F "MedTunnelPassword=yourMedTunnelPassword"
// -F "To=recipientsMedTunnelId"
// -F "Body=Test of SendMessage"
// -F "file1=@qa_data/jpg/starfish.jpg"
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
const req = try chilkat.HttpRequest.init();
defer req.deinit();
req.setHttpVerb("POST");
req.setPath("/MedTunnelMsg/api/Message/SendMessage");
req.setContentType("multipart/form-data");
req.addParam("ApplicationId", "yourApplicationId");
req.addParam("LocationId", "yourLocationId");
req.addParam("MedTunnelId", "yourMedTunnelId");
req.addParam("MedTunnelPassword", "yourMedTunnelPassword");
req.addParam("To", "recipientsMedTunnelId");
req.addParam("Body", "Test");
try req.addFileForUpload2("file1", "qa_data/jpg/starfish.jpg", "application/octet-stream");
req.addHeader("Expect", "100-continue");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpSReq("server.medtunnel.com", 443, true, 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 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)
// {
// "ReturnCode": 1,
// "ReturnCodeText": "Success",
// "Data": "{\"Id\":989432,\"FromUserId\":36990,\"FromMailBoxId\":36965, ... \"SendGlobalNotifications\":false}"
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
_ = j_resp.intOf("ReturnCode");
_ = try j_resp.stringOf(alloc, "ReturnCodeText");
const data = try j_resp.stringOf(alloc, "Data");
// Load the Data into another JSON object and parse..
const json_data = try chilkat.JsonObject.init();
defer json_data.deinit();
json_data.load(data) catch {};
json_data.setEmitCompact(false);
std.debug.print("{s}\n", .{try json_data.emit(alloc)});
// {
// "Id": 989448,
// "FromUserId": 36990,
// "FromMailBoxId": 36965,
// "FromUserType": 0,
// "FromUserName": "...",
// "FromUserFullName": "...",
// "FromUserAccountName": "...",
// "FromUserAccountTitle": "...",
// "ToUserId": 36990,
// "ToUserType": 1,
// "ToUserMailboxId": "36965",
// "ToUserName": "...",
// "ToUserFullName": "...",
// "EmailAddress": "",
// "Password": "",
// "Subject": "",
// "PatientMedTunnelId": "",
// "Body": "Test",
// "DateReceived": "4/29/2021 2:48:22 PM",
// "DisplayDateReceived": "04/29/2021 2:48 PM",
// "ViewCount": 0,
// "ViewedOn": "",
// "AttachmentCount": 1,
// "AttachmentNames": [
// {
// "MessageId": 989448,
// "Id": 424857,
// "Name": "starfish.jpg.35910fe9-4118-414c-a845-4d092ca6e784",
// "DisplayName": "starfish.jpg",
// "Size": 6229,
// "WasViewed": false,
// "ViewedOn": "",
// "ViewCount": 0,
// "Location": "Default"
// }
// ],
// "AllRecipients": [
// {
// "Id": 989448,
// "UserName": "...",
// "AccountId": 0,
// "AccountName": "...",
// "AccountTitle": "",
// "FirstName": "...",
// "LastName": "...",
// "EmailAddress": "",
// "LastSentOn": "",
// "SendCount": 0,
// "IsFavorite": false
// }
// ],
// "Status": 0,
// "ParentMessageId": 989448,
// "DistributionListId": 0,
// "DistributionListName": "",
// "BodyHistory": "",
// "ReadReceiptCallbackUrl": null,
// "SendGlobalNotifications": false
// }
var message_id: i32 = 0;
var name: [:0]const u8 = "";
var display_name: [:0]const u8 = "";
var size: i32 = 0;
var was_viewed: bool = false;
var location: [:0]const u8 = "";
var user_name: [:0]const u8 = "";
var account_id: i32 = 0;
var account_name: [:0]const u8 = "";
var account_title: [:0]const u8 = "";
var first_name: [:0]const u8 = "";
var last_name: [:0]const u8 = "";
var last_sent_on: [:0]const u8 = "";
var send_count: i32 = 0;
var is_favorite: bool = false;
var id: i32 = json_data.intOf("Id");
_ = json_data.intOf("FromUserId");
_ = json_data.intOf("FromMailBoxId");
_ = json_data.intOf("FromUserType");
_ = try json_data.stringOf(alloc, "FromUserName");
_ = try json_data.stringOf(alloc, "FromUserFullName");
_ = try json_data.stringOf(alloc, "FromUserAccountName");
_ = try json_data.stringOf(alloc, "FromUserAccountTitle");
_ = json_data.intOf("ToUserId");
_ = json_data.intOf("ToUserType");
_ = try json_data.stringOf(alloc, "ToUserMailboxId");
_ = try json_data.stringOf(alloc, "ToUserName");
_ = try json_data.stringOf(alloc, "ToUserFullName");
var email_address: [:0]const u8 = try json_data.stringOf(alloc, "EmailAddress");
_ = try json_data.stringOf(alloc, "Password");
_ = try json_data.stringOf(alloc, "Subject");
_ = try json_data.stringOf(alloc, "PatientMedTunnelId");
_ = try json_data.stringOf(alloc, "Body");
_ = try json_data.stringOf(alloc, "DateReceived");
_ = try json_data.stringOf(alloc, "DisplayDateReceived");
var view_count: i32 = json_data.intOf("ViewCount");
var viewed_on: [:0]const u8 = try json_data.stringOf(alloc, "ViewedOn");
_ = json_data.intOf("AttachmentCount");
_ = json_data.intOf("Status");
_ = json_data.intOf("ParentMessageId");
_ = json_data.intOf("DistributionListId");
_ = try json_data.stringOf(alloc, "DistributionListName");
_ = try json_data.stringOf(alloc, "BodyHistory");
_ = try json_data.stringOf(alloc, "ReadReceiptCallbackUrl");
_ = json_data.boolOf("SendGlobalNotifications");
var i: i32 = 0;
var count_i: i32 = json_data.sizeOfArray("AttachmentNames");
while (i < count_i) {
json_data.setI(i);
message_id = json_data.intOf("AttachmentNames[i].MessageId");
id = json_data.intOf("AttachmentNames[i].Id");
name = try json_data.stringOf(alloc, "AttachmentNames[i].Name");
display_name = try json_data.stringOf(alloc, "AttachmentNames[i].DisplayName");
size = json_data.intOf("AttachmentNames[i].Size");
was_viewed = json_data.boolOf("AttachmentNames[i].WasViewed");
viewed_on = try json_data.stringOf(alloc, "AttachmentNames[i].ViewedOn");
view_count = json_data.intOf("AttachmentNames[i].ViewCount");
location = try json_data.stringOf(alloc, "AttachmentNames[i].Location");
i = i + 1;
}
i = 0;
count_i = json_data.sizeOfArray("AllRecipients");
while (i < count_i) {
json_data.setI(i);
id = json_data.intOf("AllRecipients[i].Id");
user_name = try json_data.stringOf(alloc, "AllRecipients[i].UserName");
account_id = json_data.intOf("AllRecipients[i].AccountId");
account_name = try json_data.stringOf(alloc, "AllRecipients[i].AccountName");
account_title = try json_data.stringOf(alloc, "AllRecipients[i].AccountTitle");
first_name = try json_data.stringOf(alloc, "AllRecipients[i].FirstName");
last_name = try json_data.stringOf(alloc, "AllRecipients[i].LastName");
email_address = try json_data.stringOf(alloc, "AllRecipients[i].EmailAddress");
last_sent_on = try json_data.stringOf(alloc, "AllRecipients[i].LastSentOn");
send_count = json_data.intOf("AllRecipients[i].SendCount");
is_favorite = json_data.boolOf("AllRecipients[i].IsFavorite");
i = i + 1;
}
}