Sample code for 30+ languages & platforms
PureBasic

MedTunnel: Send Message Text Only

See more MedTunnel Examples

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

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpRequest.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; 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

    req.i = CkHttpRequest::ckCreate()
    If req.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpRequest::setCkHttpVerb(req, "POST")
    CkHttpRequest::setCkPath(req, "/MedTunnelMsg/api/Message/SendMessage")
    CkHttpRequest::setCkContentType(req, "multipart/form-data")
    CkHttpRequest::ckAddParam(req,"ApplicationId","yourApplicationId")
    CkHttpRequest::ckAddParam(req,"LocationId","yourLocationId")
    CkHttpRequest::ckAddParam(req,"MedTunnelId","yourMedTunnelId")
    CkHttpRequest::ckAddParam(req,"MedTunnelPassword","yourMedTunnelPassword")
    CkHttpRequest::ckAddParam(req,"To","recipientsMedTunnelId")
    CkHttpRequest::ckAddParam(req,"Body","Test")

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpSReq(http,"server.medtunnel.com",443,1,req,resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)
    jResp.i = CkJsonObject::ckCreate()
    If jResp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkHttpRequest::ckDispose(req)
        CkHttpResponse::ckDispose(resp)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    ; 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

    ReturnCode.i = CkJsonObject::ckIntOf(jResp,"ReturnCode")
    ReturnCodeText.s = CkJsonObject::ckStringOf(jResp,"ReturnCodeText")
    Data.s = CkJsonObject::ckStringOf(jResp,"Data")

    ; Load the Data into another JSON object and parse..
    jsonData.i = CkJsonObject::ckCreate()
    If jsonData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(jsonData,Data)
    CkJsonObject::setCkEmitCompact(jsonData, 0)
    Debug CkJsonObject::ckEmit(jsonData)

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

    UserName.s
    AccountId.i
    AccountName.s
    AccountTitle.s
    FirstName.s
    LastName.s
    LastSentOn.s
    SendCount.i
    IsFavorite.i

    Id.i = CkJsonObject::ckIntOf(jsonData,"Id")
    FromUserId.i = CkJsonObject::ckIntOf(jsonData,"FromUserId")
    FromMailBoxId.i = CkJsonObject::ckIntOf(jsonData,"FromMailBoxId")
    FromUserType.i = CkJsonObject::ckIntOf(jsonData,"FromUserType")
    FromUserName.s = CkJsonObject::ckStringOf(jsonData,"FromUserName")
    FromUserFullName.s = CkJsonObject::ckStringOf(jsonData,"FromUserFullName")
    FromUserAccountName.s = CkJsonObject::ckStringOf(jsonData,"FromUserAccountName")
    FromUserAccountTitle.s = CkJsonObject::ckStringOf(jsonData,"FromUserAccountTitle")
    ToUserId.i = CkJsonObject::ckIntOf(jsonData,"ToUserId")
    ToUserType.i = CkJsonObject::ckIntOf(jsonData,"ToUserType")
    ToUserMailboxId.s = CkJsonObject::ckStringOf(jsonData,"ToUserMailboxId")
    ToUserName.s = CkJsonObject::ckStringOf(jsonData,"ToUserName")
    ToUserFullName.s = CkJsonObject::ckStringOf(jsonData,"ToUserFullName")
    EmailAddress.s = CkJsonObject::ckStringOf(jsonData,"EmailAddress")
    Password.s = CkJsonObject::ckStringOf(jsonData,"Password")
    Subject.s = CkJsonObject::ckStringOf(jsonData,"Subject")
    PatientMedTunnelId.s = CkJsonObject::ckStringOf(jsonData,"PatientMedTunnelId")
    Body.s = CkJsonObject::ckStringOf(jsonData,"Body")
    DateReceived.s = CkJsonObject::ckStringOf(jsonData,"DateReceived")
    DisplayDateReceived.s = CkJsonObject::ckStringOf(jsonData,"DisplayDateReceived")
    ViewCount.i = CkJsonObject::ckIntOf(jsonData,"ViewCount")
    ViewedOn.s = CkJsonObject::ckStringOf(jsonData,"ViewedOn")
    AttachmentCount.i = CkJsonObject::ckIntOf(jsonData,"AttachmentCount")
    Status.i = CkJsonObject::ckIntOf(jsonData,"Status")
    ParentMessageId.i = CkJsonObject::ckIntOf(jsonData,"ParentMessageId")
    DistributionListId.i = CkJsonObject::ckIntOf(jsonData,"DistributionListId")
    DistributionListName.s = CkJsonObject::ckStringOf(jsonData,"DistributionListName")
    BodyHistory.s = CkJsonObject::ckStringOf(jsonData,"BodyHistory")
    ReadReceiptCallbackUrl.s = CkJsonObject::ckStringOf(jsonData,"ReadReceiptCallbackUrl")
    SendGlobalNotifications.i = CkJsonObject::ckBoolOf(jsonData,"SendGlobalNotifications")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jsonData,"AttachmentNames")
    While i < count_i
        CkJsonObject::setCkI(jsonData, i)
        i = i + 1
    Wend
    i = 0
    count_i = CkJsonObject::ckSizeOfArray(jsonData,"AllRecipients")
    While i < count_i
        CkJsonObject::setCkI(jsonData, i)
        Id = CkJsonObject::ckIntOf(jsonData,"AllRecipients[i].Id")
        UserName = CkJsonObject::ckStringOf(jsonData,"AllRecipients[i].UserName")
        AccountId = CkJsonObject::ckIntOf(jsonData,"AllRecipients[i].AccountId")
        AccountName = CkJsonObject::ckStringOf(jsonData,"AllRecipients[i].AccountName")
        AccountTitle = CkJsonObject::ckStringOf(jsonData,"AllRecipients[i].AccountTitle")
        FirstName = CkJsonObject::ckStringOf(jsonData,"AllRecipients[i].FirstName")
        LastName = CkJsonObject::ckStringOf(jsonData,"AllRecipients[i].LastName")
        EmailAddress = CkJsonObject::ckStringOf(jsonData,"AllRecipients[i].EmailAddress")
        LastSentOn = CkJsonObject::ckStringOf(jsonData,"AllRecipients[i].LastSentOn")
        SendCount = CkJsonObject::ckIntOf(jsonData,"AllRecipients[i].SendCount")
        IsFavorite = CkJsonObject::ckBoolOf(jsonData,"AllRecipients[i].IsFavorite")
        i = i + 1
    Wend


    CkHttp::ckDispose(http)
    CkHttpRequest::ckDispose(req)
    CkHttpResponse::ckDispose(resp)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)
    CkJsonObject::ckDispose(jsonData)


    ProcedureReturn
EndProcedure