Sample code for 30+ languages & platforms
Node.js

MedTunnel: Send Message Text Only

See more MedTunnel Examples

Demonstrates the MedTunnel SendMessage method to send a message to one or more recipients.

Chilkat Node.js Downloads

Node.js
NODEJS_PRELUDE

function chilkatExample() {

    var success = false;

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

    var http = new chilkat.Http();

    //  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

    var req = new chilkat.HttpRequest();
    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");

    var resp = new chilkat.HttpResponse();
    success = http.HttpSReq("server.medtunnel.com",443,true,req,resp);
    if (success == false) {
        console.log(http.LastErrorText);
        return;
    }

    var sbResponseBody = new chilkat.StringBuilder();
    resp.GetBodySb(sbResponseBody);
    var jResp = new chilkat.JsonObject();
    jResp.LoadSb(sbResponseBody);
    jResp.EmitCompact = false;

    console.log("Response Body:");
    console.log(jResp.Emit());

    var respStatusCode = resp.StatusCode;
    console.log("Response Status Code = " + respStatusCode);
    if (respStatusCode >= 400) {
        console.log("Response Header:");
        console.log(resp.Header);
        console.log("Failed.");
        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

    var ReturnCode = jResp.IntOf("ReturnCode");
    var ReturnCodeText = jResp.StringOf("ReturnCodeText");
    var Data = jResp.StringOf("Data");

    //  Load the Data into another JSON object and parse..
    var jsonData = new chilkat.JsonObject();
    jsonData.Load(Data);
    jsonData.EmitCompact = false;
    console.log(jsonData.Emit());

    //  {
    //    "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
    //  }
    //  

    var UserName;
    var AccountId;
    var AccountName;
    var AccountTitle;
    var FirstName;
    var LastName;
    var LastSentOn;
    var SendCount;
    var IsFavorite;

    var Id = jsonData.IntOf("Id");
    var FromUserId = jsonData.IntOf("FromUserId");
    var FromMailBoxId = jsonData.IntOf("FromMailBoxId");
    var FromUserType = jsonData.IntOf("FromUserType");
    var FromUserName = jsonData.StringOf("FromUserName");
    var FromUserFullName = jsonData.StringOf("FromUserFullName");
    var FromUserAccountName = jsonData.StringOf("FromUserAccountName");
    var FromUserAccountTitle = jsonData.StringOf("FromUserAccountTitle");
    var ToUserId = jsonData.IntOf("ToUserId");
    var ToUserType = jsonData.IntOf("ToUserType");
    var ToUserMailboxId = jsonData.StringOf("ToUserMailboxId");
    var ToUserName = jsonData.StringOf("ToUserName");
    var ToUserFullName = jsonData.StringOf("ToUserFullName");
    var EmailAddress = jsonData.StringOf("EmailAddress");
    var Password = jsonData.StringOf("Password");
    var Subject = jsonData.StringOf("Subject");
    var PatientMedTunnelId = jsonData.StringOf("PatientMedTunnelId");
    var Body = jsonData.StringOf("Body");
    var DateReceived = jsonData.StringOf("DateReceived");
    var DisplayDateReceived = jsonData.StringOf("DisplayDateReceived");
    var ViewCount = jsonData.IntOf("ViewCount");
    var ViewedOn = jsonData.StringOf("ViewedOn");
    var AttachmentCount = jsonData.IntOf("AttachmentCount");
    var Status = jsonData.IntOf("Status");
    var ParentMessageId = jsonData.IntOf("ParentMessageId");
    var DistributionListId = jsonData.IntOf("DistributionListId");
    var DistributionListName = jsonData.StringOf("DistributionListName");
    var BodyHistory = jsonData.StringOf("BodyHistory");
    var ReadReceiptCallbackUrl = jsonData.StringOf("ReadReceiptCallbackUrl");
    var SendGlobalNotifications = jsonData.BoolOf("SendGlobalNotifications");
    var i = 0;
    var 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;
    }


}

chilkatExample();