Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

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

$oHttp = ObjCreate("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"
;         -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

$oReq = ObjCreate("Chilkat.HttpRequest")
$oReq.HttpVerb = "POST"
$oReq.Path = "/MedTunnelMsg/api/Message/SendMessage"
$oReq.ContentType = "multipart/form-data"

$oReq.AddParam "ApplicationId","yourApplicationId"
$oReq.AddParam "LocationId","yourLocationId"
$oReq.AddParam "MedTunnelId","yourMedTunnelId"
$oReq.AddParam "MedTunnelPassword","yourMedTunnelPassword"
$oReq.AddParam "To","recipientsMedTunnelId"
$oReq.AddParam "Body","Test"

$bSuccess = $oReq.AddFileForUpload2("file1","qa_data/jpg/starfish.jpg","application/octet-stream")

$oReq.AddHeader "Expect","100-continue"

$oResp = ObjCreate("Chilkat.HttpResponse")
$bSuccess = $oHttp.HttpSReq("server.medtunnel.com",443,True,$oReq,$oResp)
If ($bSuccess = False) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

$oSbResponseBody = ObjCreate("Chilkat.StringBuilder")
$oResp.GetBodySb($oSbResponseBody)
$oJResp = ObjCreate("Chilkat.JsonObject")
$oJResp.LoadSb($oSbResponseBody)
$oJResp.EmitCompact = False

ConsoleWrite("Response Body:" & @CRLF)
ConsoleWrite($oJResp.Emit() & @CRLF)

Local $iRespStatusCode = $oResp.StatusCode
ConsoleWrite("Response Status Code = " & $iRespStatusCode & @CRLF)
If ($iRespStatusCode >= 400) Then
    ConsoleWrite("Response Header:" & @CRLF)
    ConsoleWrite($oResp.Header & @CRLF)
    ConsoleWrite("Failed." & @CRLF)
    Exit
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

Local $iReturnCode = $oJResp.IntOf("ReturnCode")
Local $sReturnCodeText = $oJResp.StringOf("ReturnCodeText")
Local $sData = $oJResp.StringOf("Data")

; Load the Data into another JSON object and parse..
$oJsonData = ObjCreate("Chilkat.JsonObject")
$oJsonData.Load($sData)
$oJsonData.EmitCompact = False
ConsoleWrite($oJsonData.Emit() & @CRLF)

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

Local $iMessageId
Local $sName
Local $sDisplayName
Local $iSize
Local $bWasViewed
Local $sLocation
Local $sUserName
Local $iAccountId
Local $sAccountName
Local $sAccountTitle
Local $sFirstName
Local $sLastName
Local $sLastSentOn
Local $iSendCount
Local $bIsFavorite

Local $iId = $oJsonData.IntOf("Id")
Local $iFromUserId = $oJsonData.IntOf("FromUserId")
Local $iFromMailBoxId = $oJsonData.IntOf("FromMailBoxId")
Local $iFromUserType = $oJsonData.IntOf("FromUserType")
Local $sFromUserName = $oJsonData.StringOf("FromUserName")
Local $sFromUserFullName = $oJsonData.StringOf("FromUserFullName")
Local $sFromUserAccountName = $oJsonData.StringOf("FromUserAccountName")
Local $sFromUserAccountTitle = $oJsonData.StringOf("FromUserAccountTitle")
Local $iToUserId = $oJsonData.IntOf("ToUserId")
Local $iToUserType = $oJsonData.IntOf("ToUserType")
Local $sToUserMailboxId = $oJsonData.StringOf("ToUserMailboxId")
Local $sToUserName = $oJsonData.StringOf("ToUserName")
Local $sToUserFullName = $oJsonData.StringOf("ToUserFullName")
Local $sEmailAddress = $oJsonData.StringOf("EmailAddress")
Local $sPassword = $oJsonData.StringOf("Password")
Local $sSubject = $oJsonData.StringOf("Subject")
Local $sPatientMedTunnelId = $oJsonData.StringOf("PatientMedTunnelId")
Local $sBody = $oJsonData.StringOf("Body")
Local $sDateReceived = $oJsonData.StringOf("DateReceived")
Local $sDisplayDateReceived = $oJsonData.StringOf("DisplayDateReceived")
Local $iViewCount = $oJsonData.IntOf("ViewCount")
Local $sViewedOn = $oJsonData.StringOf("ViewedOn")
Local $iAttachmentCount = $oJsonData.IntOf("AttachmentCount")
Local $iStatus = $oJsonData.IntOf("Status")
Local $iParentMessageId = $oJsonData.IntOf("ParentMessageId")
Local $iDistributionListId = $oJsonData.IntOf("DistributionListId")
Local $sDistributionListName = $oJsonData.StringOf("DistributionListName")
Local $sBodyHistory = $oJsonData.StringOf("BodyHistory")
Local $sReadReceiptCallbackUrl = $oJsonData.StringOf("ReadReceiptCallbackUrl")
Local $bSendGlobalNotifications = $oJsonData.BoolOf("SendGlobalNotifications")
Local $i = 0
Local $iCount_i = $oJsonData.SizeOfArray("AttachmentNames")
While $i < $iCount_i
    $oJsonData.I = $i
    $iMessageId = $oJsonData.IntOf("AttachmentNames[i].MessageId")
    $iId = $oJsonData.IntOf("AttachmentNames[i].Id")
    $sName = $oJsonData.StringOf("AttachmentNames[i].Name")
    $sDisplayName = $oJsonData.StringOf("AttachmentNames[i].DisplayName")
    $iSize = $oJsonData.IntOf("AttachmentNames[i].Size")
    $bWasViewed = $oJsonData.BoolOf("AttachmentNames[i].WasViewed")
    $sViewedOn = $oJsonData.StringOf("AttachmentNames[i].ViewedOn")
    $iViewCount = $oJsonData.IntOf("AttachmentNames[i].ViewCount")
    $sLocation = $oJsonData.StringOf("AttachmentNames[i].Location")
    $i = $i + 1
Wend
$i = 0
$iCount_i = $oJsonData.SizeOfArray("AllRecipients")
While $i < $iCount_i
    $oJsonData.I = $i
    $iId = $oJsonData.IntOf("AllRecipients[i].Id")
    $sUserName = $oJsonData.StringOf("AllRecipients[i].UserName")
    $iAccountId = $oJsonData.IntOf("AllRecipients[i].AccountId")
    $sAccountName = $oJsonData.StringOf("AllRecipients[i].AccountName")
    $sAccountTitle = $oJsonData.StringOf("AllRecipients[i].AccountTitle")
    $sFirstName = $oJsonData.StringOf("AllRecipients[i].FirstName")
    $sLastName = $oJsonData.StringOf("AllRecipients[i].LastName")
    $sEmailAddress = $oJsonData.StringOf("AllRecipients[i].EmailAddress")
    $sLastSentOn = $oJsonData.StringOf("AllRecipients[i].LastSentOn")
    $iSendCount = $oJsonData.IntOf("AllRecipients[i].SendCount")
    $bIsFavorite = $oJsonData.BoolOf("AllRecipients[i].IsFavorite")
    $i = $i + 1
Wend