Perl
Perl
MedTunnel: Send Message Text Only
See more MedTunnel Examples
Demonstrates the MedTunnel SendMessage method to send a message to one or more recipients.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
my $http = Chilkat2::Http->new;
# 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"
# Use the following online tool to generate HTTP code from a CURL command
# Convert a cURL Command to HTTP Source Code
my $req = Chilkat2::HttpRequest->new;
$req->HttpVerb('POST');
$req->Path('/MedTunnelMsg/api/Message/SendMessage');
$req->ContentType('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');
my $resp = Chilkat2::HttpResponse->new;
my $success = $http->HttpSReq('server.medtunnel.com', 443, 1, $req, $resp);
if (!$success) {
print $http->LastErrorText, "\n";
exit;
}
my $sbResponseBody = Chilkat2::StringBuilder->new;
$resp->GetBodySb($sbResponseBody);
my $jResp = Chilkat2::JsonObject->new;
$jResp->LoadSb($sbResponseBody);
$jResp->EmitCompact(0);
print "Response Body:\n";
print $jResp->Emit(), "\n";
my $respStatusCode = $resp->StatusCode;
print 'Response Status Code = ', $respStatusCode, "\n";
if ($respStatusCode >= 400) {
print "Response Header:\n";
print $resp->Header, "\n";
print "Failed.\n";
exit;
}
# 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
my $ReturnCode = $jResp->IntOf('ReturnCode');
my $ReturnCodeText = $jResp->StringOf('ReturnCodeText');
my $Data = $jResp->StringOf('Data');
# Load the Data into another JSON object and parse..
my $jsonData = Chilkat2::JsonObject->new;
$jsonData->Load($Data);
$jsonData->EmitCompact(0);
print $jsonData->Emit(), "\n";
# {
# "Id": 989436,
# "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:29:18 PM",
# "DisplayDateReceived": "04/29/2021 2:29 PM",
# "ViewCount": 0,
# "ViewedOn": "",
# "AttachmentCount": 0,
# "AttachmentNames": [
# ],
# "AllRecipients": [
# {
# "Id": 989436,
# "UserName": "...",
# "AccountId": 0,
# "AccountName": "...",
# "AccountTitle": "",
# "FirstName": "...",
# "LastName": "...",
# "EmailAddress": "",
# "LastSentOn": "",
# "SendCount": 0,
# "IsFavorite": false
# }
# ],
# "Status": 1,
# "ParentMessageId": 989436,
# "DistributionListId": 0,
# "DistributionListName": "",
# "BodyHistory": "",
# "ReadReceiptCallbackUrl": null,
# "SendGlobalNotifications": false
# }
#
my $UserName = '';
my $AccountId = 0;
my $AccountName = '';
my $AccountTitle = '';
my $FirstName = '';
my $LastName = '';
my $LastSentOn = '';
my $SendCount = 0;
my $IsFavorite = 0;
my $Id = $jsonData->IntOf('Id');
my $FromUserId = $jsonData->IntOf('FromUserId');
my $FromMailBoxId = $jsonData->IntOf('FromMailBoxId');
my $FromUserType = $jsonData->IntOf('FromUserType');
my $FromUserName = $jsonData->StringOf('FromUserName');
my $FromUserFullName = $jsonData->StringOf('FromUserFullName');
my $FromUserAccountName = $jsonData->StringOf('FromUserAccountName');
my $FromUserAccountTitle = $jsonData->StringOf('FromUserAccountTitle');
my $ToUserId = $jsonData->IntOf('ToUserId');
my $ToUserType = $jsonData->IntOf('ToUserType');
my $ToUserMailboxId = $jsonData->StringOf('ToUserMailboxId');
my $ToUserName = $jsonData->StringOf('ToUserName');
my $ToUserFullName = $jsonData->StringOf('ToUserFullName');
my $EmailAddress = $jsonData->StringOf('EmailAddress');
my $Password = $jsonData->StringOf('Password');
my $Subject = $jsonData->StringOf('Subject');
my $PatientMedTunnelId = $jsonData->StringOf('PatientMedTunnelId');
my $Body = $jsonData->StringOf('Body');
my $DateReceived = $jsonData->StringOf('DateReceived');
my $DisplayDateReceived = $jsonData->StringOf('DisplayDateReceived');
my $ViewCount = $jsonData->IntOf('ViewCount');
my $ViewedOn = $jsonData->StringOf('ViewedOn');
my $AttachmentCount = $jsonData->IntOf('AttachmentCount');
my $Status = $jsonData->IntOf('Status');
my $ParentMessageId = $jsonData->IntOf('ParentMessageId');
my $DistributionListId = $jsonData->IntOf('DistributionListId');
my $DistributionListName = $jsonData->StringOf('DistributionListName');
my $BodyHistory = $jsonData->StringOf('BodyHistory');
my $ReadReceiptCallbackUrl = $jsonData->StringOf('ReadReceiptCallbackUrl');
my $SendGlobalNotifications = $jsonData->BoolOf('SendGlobalNotifications');
my $i = 0;
my $count_i = $jsonData->SizeOfArray('AttachmentNames');
while ($i < $count_i) {
$jsonData->I($i);
$i = $i + 1;
}
$i = 0;
$count_i = $jsonData->SizeOfArray('AllRecipients');
while ($i < $count_i) {
$jsonData->I($i);
$Id = $jsonData->IntOf('AllRecipients[i].Id');
$UserName = $jsonData->StringOf('AllRecipients[i].UserName');
$AccountId = $jsonData->IntOf('AllRecipients[i].AccountId');
$AccountName = $jsonData->StringOf('AllRecipients[i].AccountName');
$AccountTitle = $jsonData->StringOf('AllRecipients[i].AccountTitle');
$FirstName = $jsonData->StringOf('AllRecipients[i].FirstName');
$LastName = $jsonData->StringOf('AllRecipients[i].LastName');
$EmailAddress = $jsonData->StringOf('AllRecipients[i].EmailAddress');
$LastSentOn = $jsonData->StringOf('AllRecipients[i].LastSentOn');
$SendCount = $jsonData->IntOf('AllRecipients[i].SendCount');
$IsFavorite = $jsonData->BoolOf('AllRecipients[i].IsFavorite');
$i = $i + 1;
}