Send Batches

SendBatch API is used to send batch(bulk) messages. The main inputs of a batch is Template + Data to be merged with this template.

Please check SendBatch API documentation under Batch Send > SendBatch.

Here is a simple function to call SendBatch API.

public static async Task<string> SendBatchAsync()
{
    try
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // 1 - Login and Get Token...
            var tokenUrl = "[[BASE_API_URL]]/rest/login";
            var tokenMsg = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
            var userName = "[[USERNAME]]";
            var password = "[[PASSWORD]]";

            var tokenRequest =
                "{" +
                    $"\"userName\": \"{userName}\"," +
                    $"\"password\": \"{password}\"" +
                "}";

            tokenMsg.Content = new StringContent(tokenRequest, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.SendAsync(tokenMsg, HttpCompletionOption.ResponseHeadersRead);

            var result = string.Empty;
            HttpContent content = response.Content;
            using (Task<string> completedTask = content.ReadAsStringAsync())
            {
                result = completedTask.Result;
                completedTask.Dispose();
            }

            dynamic deserializedResultObject = JsonConvert.DeserializeObject(result);
            var token = deserializedResultObject.token.Value;

            // Add token to request...
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

            // This is a batch request.
            using (var formData = new MultipartFormDataContent("request"))
            {
                // Please see an example file below
                var fileBytes = File.ReadAllBytes("[[FILE_PATH]]/data_email.csv");
                // File as Memory stream 
                formData.Add(new StreamContent(new MemoryStream(fileBytes)), "batchFile", "data_email.csv");
                // account id is 23. You must know this account id
                formData.Add(new StringContent("23"), "accountId");
                // public id of the template.  
                formData.Add(new StringContent("c71069be-cc91-49ce-80a5-9f70f5b155eb"), "publicId");
                // you must give a batch id to your request. This may be your campaign id. DO NOT USE - (dash) in your batches. 
                formData.Add(new StringContent("666011"), "batchId");
                // if you have a huge batch file you can seperate this request into parts. This is the part id of the batch default 0
                formData.Add(new StringContent("0"), "batchPartId");
                // This parameter defines the header line of batch file. It consists columns which are in the batch file seperated by Delimiter
                formData.Add(new StringContent("Message-ID;To;Name;Surname"), "fileHeaderMap");
                // delimiter in batch file to seperate the columns
                formData.Add(new StringContent(";"), "delimiter"); // Default ;
                // Batch allowed execution times during the day. With finishTimes value this batch items will be sent only between 12:00-16:00 
                // This parameter is an optional parameter and please do not use these startTimes and finishTimes parameters if you dont need to
                formData.Add(new StringContent("1200"), "startTimes");
                formData.Add(new StringContent("1600"), "finishTimes");
                // The speed of campaign sent. Items/ per minute.
                // This is an optional parameter. Please do not use it if you dont want to slow the sending down 
                formData.Add(new StringContent("3000"), "speed");
                // scheduled time for sending. 														
                formData.Add(new StringContent("2020-03-24T11:00:00.0000000+03:00"), "scheduleTime");
                // this is important for SMS OTP sendings
                // formData.Add(new StringContent("1"), "priority");

                // Target Url of at api...
                var uri = new Uri("[[BASE_API_URL]]/rest/batch/send");

                // Perform api call...
                response = await client.PostAsync(uri, formData);

                //Reading result & returning...
                result = string.Empty;
                using (Task<string> completedTask = response.Content.ReadAsStringAsync())
                {
                    result = completedTask.Result;
                    completedTask.Dispose();
                }
            }
            // If success, you better create a class for api response and deserialize it here...
            return result;
        }
    }
    catch (Exception ex)
    {
        System.Console.WriteLine(ex);
        return null;
    }
}

Notes

  1. accountId parameter must be gathered from the system administrator
  2. publicId of a template can be gathered from the user who created the template or you can create a template by calling Create Template function.
  3. Please use [A-Z][a-z][0-9] string in your batch ids. Example: 12345678, ABC12345
  4. Message-ID column in fileHeaderMap is not required but highly recommended. It uniquely identifies the message and helps you in reporting. If you don't have such an ID, you can use your batchID-MessageId to uniquely identify the message and use it as Message-ID
  5. To column in fileHeaderMap is the recipient and this column is required column.
  6. Name and Surname columns in fileHeaderMap is just optional any you may want to use them because your template may have {{Name}} and {{Surname}} variables in the email or sms template!
  7. startTimes, finishTimes are optional. If you want to send your batch during specific times during the day, please use this variables. For instance, if you set startTimes 0930-1330 and finishTimes 1230-1830, this batch will only be distributed between 09:30-12:30 and start 13:30 till 18:30 during the day.
  8. Please use them if you need to throttle your campaign sending. For instance for 12000 message/ hour speed, you must set this variable to 200. The unif of speed paramter is items/minute.
  9. The code consists a data_email.csv file. The file content is listed below

45ccca03-c7a4-4cff-96c5-a7d3713c1b1f;[email protected];Akito;Crik

d5f46f36-1105-4cb9-8ba9-069a9d3e5316;[email protected];Alti;Zak

446dd5a3-103a-4258-a9f1-ba8ff4bd5a73;[email protected];Osmon;Smith

....

any number of rows. Each row means a new message for the template.

....

....

Important Note

For SMS messages, Field 'To' contains Mobile Numbers like 905332141159

For Mobile Push messages, Field 'To' contains Mobile Tokens

For IOS tokens, the format of the value in 'To' field is I-{Token}. An example is

I-0f09b8f25946bf0d9596996c2c41105dd133e462ebfd29cc35431d1ecd63a4b6

For Android tokens, the format of the value in 'To' field is A-{Token}. An example is

A-dQ8NblhbpMA:APA91bG86NTEW8FSd1LICs-mXPIP7iJYyTp4tOL0s9fAsd4uoHb0PNR4_a9sGfsu9eEHAIanom68LImXu2kZok1ipXHAPH0MthrWcSPFdaoaiAlF2keUuP7q2KYMWZrsXCmmKOsWS4