Java USES the cloud API to send SMS verification codes

  • 2020-06-07 04:25:42
  • OfStack

The following is how to use the machine to complete the batch operation, the SMS business automation.

Get APIKEY

The cloud Chip network offers complete SDK and API to help developers quickly complete business development.

Before starting Coding, you need to get APIKEY, as shown below.

Get APIKEY

Click the eye button and enter the captcha to view APIKEY.

It should be noted that APIKEY is particularly important and must be protected from leaks. On the cloud side, several protection mechanisms are provided, such as verification, sensitive processing, sub-account independent APIKEY, etc. It can be seen that their security awareness is quite good.

Start Coding

With APIKEY, you can go straight to Coding.

The most important interface is the three, single sending interface, batch sending interface, batch personalized sending interface. Once you figure out the three interfaces, the rest of the development is pretty much at hand.

Single sending interface


*  Send a single text message , Smart matching SMS template 
 *
 * @param apikey  After successful registration, you can log in the official website of the cloud film , Enter the background to view 
 * @param text   You need to use an approved template or default template 
 * @param mobile  Received cell phone number , Only single number sending is supported 
 * @return json Format string 
 */
public static String singleSend(String apikey, String text, String mobile) {
  Map<String, String> params = new HashMap<String, String>();// Request parameter set 
  params.put("apikey", apikey);
  params.put("text", text);
  params.put("mobile", mobile);
  return post("https://sms.yunpian.com/v2/sms/single_send.json", params);// Please use by yourself post Way to request , You can use Apache HttpClient
}

In this case, APIKEY was obtained from the cloud background.

For example, I want to send to the number 1888888888, the code is as follows:


String apikey  =  fx33mio3561dah34jdk748vz9dkfjkd373zdfg28df43dfhjadj;
String text  =  "[ Network clouds ]  Your SMS verification code is  1234";
String mobile  =  18888888888 ; 
testSendSms ( apikey, text, mobile);

So a text message is sent. A detailed description can be found here.

Batch transmission interface

You can also send text messages with the same content to multiple phone Numbers, as follows:


/**
 *  Batch send SMS , Multiple Numbers for the same content , Smart matching SMS template 
 *
 * @param apikey  After successful registration, you can log in the official website of the cloud film , Enter the background to view 
 * @param text   You need to use an approved template or default template 
 * @param mobile  Received cell phone number , Multiple phone Numbers are separated by English commas 
 * @return json Format string 
 */
public static String batchSend(String apikey, String text, String mobile) {
  Map<String, String> params = new HashMap<String, String>();// Request parameter set 
  params.put("apikey", apikey);
  params.put("text", text);
  params.put("mobile", mobile);
  return post("https://sms.yunpian.com/v2/sms/batch_send.json", params);// Please use by yourself post Way to request , You can use Apache HttpClient
}

Multiple phone Numbers are separated by commas, which is how mobile is assigned


String mobile  =  "1888888888, 1234567890, 9876543210"

Detailed instructions can be found here.

Batch personalized sending interface

Most of the time, the requirements we encountered were not so simple, and we often sent different content to different phone Numbers, and the cloud provided the corresponding API.


/**
 *  Batch personalized delivery 
 */
public static String multiSend() throws UnsupportedEncodingException {
  Map<String, String> params = new HashMap<String, String>();// Request parameter set 
  params.put("apikey", "your apikey");
  params.put("text", java.net.URLEncoder.encode(" [Cloud Chip network] Your verification code is 1234", "UTF-8") + ","
      + java.net.URLEncoder.encode(" Your login code is 8888", "UTF-8"));
  params.put("mobile", "13812345678,18888888888");
  return post("https://sms.yunpian.com/v2/sms/multi_send.json", params);// Please use by yourself post Way to request , You can use Apache HttpClient
}

The code above is the result of the first message "[network] clouds your verification code is 1234" to 13812345678, article 2 of the text "[network] clouds your login code is 8888" to 18888888888, of course this is implemented or look stupid, but don't worry, clouds web also provides a variable template, just replace the corresponding variable section in the template, you can easily implement for a number of different number send text messages, about variable template, later can talk more, anyway he executed perfectly OCP principles in the design pattern, It's a classic.

For more details, see here.

This article mainly USES Java to implement SMS captchas, and there will be another article about PHP to implement SMS captchas in the future. If you can't wait, there are also instructions on PHP in the cloud document, so you can have a preview.


Related articles: