Lua
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.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"
-- -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
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/MedTunnelMsg/api/Message/SendMessage"
req.content_type = "multipart/form-data"
req:add_param("ApplicationId", "yourApplicationId")
req:add_param("LocationId", "yourLocationId")
req:add_param("MedTunnelId", "yourMedTunnelId")
req:add_param("MedTunnelPassword", "yourMedTunnelPassword")
req:add_param("To", "recipientsMedTunnelId")
req:add_param("Body", "Test")
local success = req:add_file_for_upload2("file1", "qa_data/jpg/starfish.jpg", "application/octet-stream")
req:add_header("Expect", "100-continue")
local resp = chilkat.HttpResponse.new()
success = http:http_sreq("server.medtunnel.com", 443, true, req, resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- 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 ReturnCode = jResp:int_of("ReturnCode")
local ReturnCodeText = jResp:string_of("ReturnCodeText")
local Data = jResp:string_of("Data")
-- Load the Data into another JSON object and parse..
local jsonData = chilkat.JsonObject.new()
jsonData:load(Data)
jsonData.emit_compact = false
print(jsonData:emit())
-- {
-- "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 MessageId = 0
local Name = ""
local DisplayName = ""
local Size = 0
local WasViewed = false
local Location = ""
local UserName = ""
local AccountId = 0
local AccountName = ""
local AccountTitle = ""
local FirstName = ""
local LastName = ""
local LastSentOn = ""
local SendCount = 0
local IsFavorite = false
local Id = jsonData:int_of("Id")
local FromUserId = jsonData:int_of("FromUserId")
local FromMailBoxId = jsonData:int_of("FromMailBoxId")
local FromUserType = jsonData:int_of("FromUserType")
local FromUserName = jsonData:string_of("FromUserName")
local FromUserFullName = jsonData:string_of("FromUserFullName")
local FromUserAccountName = jsonData:string_of("FromUserAccountName")
local FromUserAccountTitle = jsonData:string_of("FromUserAccountTitle")
local ToUserId = jsonData:int_of("ToUserId")
local ToUserType = jsonData:int_of("ToUserType")
local ToUserMailboxId = jsonData:string_of("ToUserMailboxId")
local ToUserName = jsonData:string_of("ToUserName")
local ToUserFullName = jsonData:string_of("ToUserFullName")
local EmailAddress = jsonData:string_of("EmailAddress")
local Password = jsonData:string_of("Password")
local Subject = jsonData:string_of("Subject")
local PatientMedTunnelId = jsonData:string_of("PatientMedTunnelId")
local Body = jsonData:string_of("Body")
local DateReceived = jsonData:string_of("DateReceived")
local DisplayDateReceived = jsonData:string_of("DisplayDateReceived")
local ViewCount = jsonData:int_of("ViewCount")
local ViewedOn = jsonData:string_of("ViewedOn")
local AttachmentCount = jsonData:int_of("AttachmentCount")
local Status = jsonData:int_of("Status")
local ParentMessageId = jsonData:int_of("ParentMessageId")
local DistributionListId = jsonData:int_of("DistributionListId")
local DistributionListName = jsonData:string_of("DistributionListName")
local BodyHistory = jsonData:string_of("BodyHistory")
local ReadReceiptCallbackUrl = jsonData:string_of("ReadReceiptCallbackUrl")
local SendGlobalNotifications = jsonData:bool_of("SendGlobalNotifications")
local i = 0
local count_i = jsonData:size_of_array("AttachmentNames")
while i < count_i do
jsonData.i = i
MessageId = jsonData:int_of("AttachmentNames[i].MessageId")
Id = jsonData:int_of("AttachmentNames[i].Id")
Name = jsonData:string_of("AttachmentNames[i].Name")
DisplayName = jsonData:string_of("AttachmentNames[i].DisplayName")
Size = jsonData:int_of("AttachmentNames[i].Size")
WasViewed = jsonData:bool_of("AttachmentNames[i].WasViewed")
ViewedOn = jsonData:string_of("AttachmentNames[i].ViewedOn")
ViewCount = jsonData:int_of("AttachmentNames[i].ViewCount")
Location = jsonData:string_of("AttachmentNames[i].Location")
i = i + 1
end
i = 0
count_i = jsonData:size_of_array("AllRecipients")
while i < count_i do
jsonData.i = i
Id = jsonData:int_of("AllRecipients[i].Id")
UserName = jsonData:string_of("AllRecipients[i].UserName")
AccountId = jsonData:int_of("AllRecipients[i].AccountId")
AccountName = jsonData:string_of("AllRecipients[i].AccountName")
AccountTitle = jsonData:string_of("AllRecipients[i].AccountTitle")
FirstName = jsonData:string_of("AllRecipients[i].FirstName")
LastName = jsonData:string_of("AllRecipients[i].LastName")
EmailAddress = jsonData:string_of("AllRecipients[i].EmailAddress")
LastSentOn = jsonData:string_of("AllRecipients[i].LastSentOn")
SendCount = jsonData:int_of("AllRecipients[i].SendCount")
IsFavorite = jsonData:bool_of("AllRecipients[i].IsFavorite")
i = i + 1
end