Detailed steps to use Alibaba big fish Java

Keywords: Programming Java Mobile SDK JSON

Summary

  • Alibaba greater (formerly Alibaba big fish) is a product of Alibaba communication, which integrates the communication capabilities of three major operators.
  • Aligreater provides personalized services including SMS, voice, direct charging of traffic, private special line, shop mobile number, etc.

Open SMS service

  • Step 1:

  • Step 2:

  • Step 3:

  • Step 4:

  • Step 5: open

  • Step 6: Activate

  • Step 7: charge money, just a little bit, one SMS 0.045

Signature management

Summary

The framed one is the signature

Add signature

Template management

Summary

It's the template that's framed

add template

Using Alibaba fish in Java

Help documents

https://help.aliyun.com/product/44282.html?spm=5176.12212976.0.0.15b31cbeK3Pm5Y

Official example demo

https://help.aliyun.com/document_detail/101893.html?spm=a2c4g.11174283.6.651.62c72c42nxOfdH

Tools

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
/**
 * Created on 17/6/7.
 * The DEMO program of SMS API product contains a SmsDemo class in the project, which directly passes the
 * Execute the main function to experience the API function of the SMS product (just replace AK with AK that has opened the cloud communication SMS product function)
 * The project relies on two jar packages (stored in the libs directory of the project)
 * 1:aliyun-java-sdk-core.jar
 * 2:aliyun-java-sdk-dysmsapi.jar
 *
 * Note: UTF-8 is adopted for Demo project coding
 * Please do not refer to this DEMO for international SMS
 */
public class SmsUtil {

    //Product Name: cloud communication short message API product, developers do not need to replace it
    static final String product = "Dysmsapi";
    //Product domain name, developers do not need to replace
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO here needs to be replaced by the developer's own AK (found on Alibaba cloud access console)
    static final String accessKeyId = "LTAIjJJq67SloCLL";//
    static final String accessKeySecret = "B3BZSQDnuUnfZXf22bbovyasJmCNDt";

    public static SendSmsResponse sendSms(String telephone,String name, String code ,String address , String phone) throws ClientException {

        //Self adjustable timeout
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //Initialization of acsClient, region is not supported temporarily
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //Assembly request object - for details, please refer to the console - Documentation section
        SendSmsRequest request = new SendSmsRequest();
        //Required: mobile number to be sent
        request.setPhoneNumbers(telephone); 		//15000000000
        //Required: SMS signature - can be found in SMS console
        request.setSignName("I want to learn");			
        //Required: SMS template - can be found in SMS console
        request.setTemplateCode("SMS_130929258"); 	//SMS_85550064
        //Optional: the variables in the template replace the JSON string. For example, when the template content is "Dear ${name}, and your verification code is ${code}", the value here is
        //Hello ${name}, please take the item code: ${code}, go to ${address} to pick up the item. If you have any questions, please consult ${phone}.
        request.setTemplateParam("{\"name\":\""+name+"\",\"code\":\""+code+"\",\"address\":\""+address+"\",\"phone\":\""+phone+"\"}");

        //Optional - uplink SMS extension code (please ignore this field for users without special requirements)
        //request.setSmsUpExtendCode("90997");

        //Optional: outId is the extension field provided to the business party, which will be brought back to the caller in the SMS receipt message
        request.setOutId("yourOutId");

        //hint an exception may be thrown here. Pay attention to catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }

    public static QuerySendDetailsResponse querySendDetails(String bizId) throws ClientException {

        //Self adjustable timeout
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //Initialization of acsClient, region is not supported temporarily
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //Assemble request object
        QuerySendDetailsRequest request = new QuerySendDetailsRequest();
        //Required - number
        request.setPhoneNumber("15151772559");
        //Optional - serial number
        request.setBizId(bizId);
        //Required - send date supports query within 30 days, format yyyyMMdd
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        request.setSendDate(ft.format(new Date()));
        //Required - page size
        request.setPageSize(10L);
        //Required - the current page number is counted from 1
        request.setCurrentPage(1L);

        //hint an exception may be thrown here. Pay attention to catch
        QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);

        return querySendDetailsResponse;
    }

    /** Random generation of 4-bit verification code*/
    public static String getNumber(){
    	int number;//Define two variables
        Random ne = new Random();//Instantiate a random object ne
        number = ne.nextInt(9999-1000+1)+1000;//Assign random values to variables 1000-9999
        System.out.println("The resulting random number is:"+number);//output
        return String.valueOf(number);
    }
}

Call tool class

SendSmsResponse sendSmsResponse = SmsUtil.sendSms("17554248502", "Diffuse Road", "521", "A country, a palace", "541881452");
//Response code
System.out.println(sendSmsResponse.getCode());
//Response information
System.out.println(sendSmsResponse.getMessage());

Posted by Agtronic on Mon, 27 Apr 2020 09:42:33 -0700