Monday, April 5, 2021

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API.

Transactional SMS usually come with One Time PIN (OTP) to verify the user identity who is using your app. It is considered as one of the common two factor authentication method. The OTP is unique and bind to a specific account and it is generated using hash algorithm. The hash algorithm I would recommend is HMAC based one time password, for more details please refer to this Wiki: https://en.wikipedia.org/wiki/HMAC-based_One-Time_Password. You may also find a lot sample code library available from Google.

Prior in calling the Short Message Service API from your system, you need to create an Access Key first. You need the access key to get authorized to invoke the API method to send SMS.



After creating an Access Key, you can go to the Developer Guide to check out the sample code. I find that Alibaba Cloud have done a very good job in online documentation and even provide a real time interpreter to test the source code on the fly which actually help improving the learning curve of the developer. It allow the developer perform code modification directly from the browser and test the code and monitor the difference.


The highlighted yellow text below from the sample source code is the place where you need to modify base on the Access Key that you had generated earlier. The aqua text below is where you put your HMAC One Time Password.

using System;
using System.Collections.Generic;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;

namespace CommonRequestDemo
{
class Program
{
static void Main(string[] args)
{
IClientProfile profile = DefaultProfile.GetProfile("ap-southeast-1", "<accessKeyId>", "<accessSecret>");
DefaultAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.Method = MethodType.POST;
request.Domain = "dysmsapi.ap-southeast-1.aliyuncs.com";
request.Version = "2018-05-01";
request.Action = "SendMessageToGlobe";
// request.Protocol = ProtocolType.HTTP;
request.AddQueryParameters("To", "601612312312");
request.AddQueryParameters("Message", "Your OTP is 123456 from Alibaba Cloud");

try {
CommonResponse response = client.GetCommonResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}

The following link contain all the possible error codes which is useful for you to troubleshoot any issue when you attempt to send SMS.

How to Send SMS with Alibaba Cloud Short Message Service?

SMS is a very commonly used tool to send notification, promotion or transaction verification. I would say majority of the software todays require sending SMS service. Hence, Alibaba Cloud also provide the service for their clients.


In Alibaba Cloud Short Message Service, you can purchase SMS package base on the country mobile that you intend to send, otherwise you can opt for the Global package which is much more expensive but support world wide any mobile number.

The SMS package price is different for different country, potentially due to the telco operator in different country charge the SMS service differently.


After purchasing the SMS package, if you wish to test the SMS service from Alibaba Cloud, you can use the Quick Start function to test sending SMS to your own mobile number to confirm receiving the message.

Next, go to the Go Globe if you are not from China and intend to broadcast promotional SMS to your customers.


Create a New Campaign, then enter the Campaign Name then follow by the promotional message that you wish to send. If you want the SMS content to have variable value, you need to download the sample Excel file from the "example download".

Enter the variable name with $ sign with curly bracket such as ${name}. The variable value will be changed and different base on the Excel file content. The variable name is the first row of the Excel. The To column must be remained as it is, add more column if you wish to have more variables to be supported.

Then, click the Import Number button to upload the Excel file that you have the variable setup.

If you wish to broadcast your SMS at a scheduled time, just hit the Schedule Campaign button and simply choose the date and time you wish.

Lastly, hit the Schedule Campaign button and you are good and just wait for SMS broadcast automatically until the scheduled time.

It is advisable that before scheduling the campaign or perform immediate broadcast, always test the broadcast with a few of your own or friends mobile first to verify the content. SMS broadcast cannot be retracted if there is any mistake that you discover later.

You can check your SMS broadcast report under the Statistic section. So that, you can see which mobile number is failed to be delivered the SMS.


 

Send Transactional SMS with API

This post cover how to send transactional SMS using the Alibaba Cloud Short Message Service API. Transactional SMS usually come with One Tim...