The background,
In the current development project, I believe that we have encountered the need to send SMS. So we usually use third-party services, so HERE I have done API docking Tencent cloud, so I write down some experience for everyone. Some people may ask, the official website of the document is well written, you want to stay here, blah blah blah? I’m just using the situation as a docking point and point of view to tell you about the problems I’ve encountered. The official website says they know where and how things are configured, and we, as the docking person, have to look everywhere for documentation. So get started!
Ii. Preparation before docking
Then you need to open a Tencent cloud account. Cloud.tencent.com/ how to register for you to see, this is ok. After registering an account. We can search for SMS directly from the product and enter the SMS platform. You can also open the link below. Console.cloud.tencent.com/smsv2/manag…
After opening the SMS platform here, individuals will send 100 domestic, enterprises send 1000. Feeling optional.
When these are ready. The following start
Third, docking
We to docking is to open the document, right, API documentation here: cloud.tencent.com/document/pr…
They provide API and SDK document docking, or the same, I think SDK is reliable, why, the front docking U shield said, they have already written the code, you directly use the method, to avoid all to you avoid. then
We did what he told us to go to our pom.xml file and add the JAR package
<dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java</artifactId> The < version > 3.1.270 < / version > <! --> </dependency> --> </dependency> --> </dependency> --> </dependency>Copy the code
And then, of course, let’s look at the following.
A code example of a short message occurring
import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; / / import optional configuration class import com.tencentcloudapi.com mon. Profile. ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; / / import SMS client module import com. Tencentcloudapi. SMS. V20210111. SmsClient; / / import interface to request a corresponding request response class import com. Tencentcloudapi. SMS. V20210111. Models. SendSmsRequest; import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; /** * Tencent Cloud Sms Sendsms ** / public class Sendsms {public static void main(String[] args) {try { * Instantiate an authentication object, the input parameter needs to pass the Tencent cloud account secretId, secretKey. * Read from an environment variable. Set these two values in the environment variable first. * You can also write dead key pairs in code, but be careful not to copy, upload, or share the code with others, * lest you compromise your property by revealing the key pair. * CAM key query: https://console.cloud.tencent.com/cam/capi * / the Credential cred = new Credential (" secretId ", "secretKey"); // Instantiate an HTTP option, optional, no special requirements can skip HttpProfile HttpProfile = new HttpProfile(); // set proxy // httpprofile.setProxyhost (" real proxy IP "); // httpprofile.setProxyPort (real proxy port); /* The SDK uses POST by default. * If you must use the GET method, you can set it here. The GET method cannot handle some larger requests */ httpprofile.setreqMethod ("POST"); / * SDK have default timeout, not necessary, please do not adjust * if necessary please check in the code in order to get the latest. The default value of * / httpProfile setConnTimeout (60); /* The SDK automatically specifies the domain name. Normally you do not need to specify a domain name, but if you are accessing the financial district service * you must manually specify the domain name, for example SMS Shanghai financial district name: sms.ap-shanghai-fsi.tencentcloudapi.com */ httpProfile.setEndpoint("sms.tencentcloudapi.com"); ClientProfile ClientProfile = new ClientProfile(); ClientProfile ClientProfile = new ClientProfile(); / * SDK default TC3 - HMAC - SHA256 signature * not necessary. Please don't modify this field * / clientProfile setSignMethod (" HmacSHA256 "); clientProfile.setHttpProfile(httpProfile); /* Instantiate the client object to request the product (for example, SMS) * The second argument is the location information, you can directly fill in the string ap-guangzhou, Or reference the default constant */ SmsClient client = new SmsClient(cred, "ap-Guangzhou ",clientProfile); /* Instantiate a request object, depending on the interface called and the actual situation, you can further set the request parameters * you can directly query the SDK source code to determine which interface properties can be set * attributes may be basic types, or may refer to another data structure * IDE is recommended for development. */ SendSmsRequest req = new SendSmsRequest(); /* Fill in the request parameters, where the member variables of the request object are the input parameters of the corresponding interface. * You can view the definition of the request parameters through the interface documentation on the website or jump to the definition of the request object. SMS helper: https://console.cloud.tencent.com/smsv2 * https://cloud.tencent.com/document/product/382/3773 * / / * SMS application ID: SMS SdkAppId Actual SdkAppId generated after the application is added to [SMS console], for example, 1400006666 */ String SdkAppId = "1400009099"; req.setSmsSdkAppId(sdkAppId); /* Message signature content: utF-8 encoding, you must fill in the approved signature, signature information can be logged in [SMS console] */ String signName = "signature content "; req.setSignName(signName); [SMS Helper] */ String SenderId = ""; req.setSenderId(senderid); /* sessionContext = "XXX "; /* sessionContext =" XXX "; req.setSessionContext(sessionContext); /* SMS Helper number Extension: disabled by default, contact [SMS Helper] */ String extendCode = ""; req.setExtendCode(extendCode); /* Template ID: Enter the ID of the approved template. */ String templateId = "400000"; req.setTemplateId(templateId); +[Country or Region code][Mobile phone number] * An example is as follows: +8613711112222, where there is a + number in front, 86 is the country code, 13711112222 is the mobile phone number, A maximum of 200 mobile phone numbers */ String[] phoneNumberSet = {"+8621212313123", "+8612345678902", "+8612345678903"}; req.setPhoneNumberSet(phoneNumberSet); /* templateParamSet: Empty if no template parameter is present */ String[] templateParamSet = {"5678"}; req.setTemplateParamSet(templateParamSet); /* Call the SendSms method from the client object to initiate the request. */ SendSmsResponse res = client.sendsms (req); */ SendSmsResponse res = client. / / output json format string back pack System. Out. The println (SendSmsResponse. ToJsonString (res)); System.out.println(res.getrequestid ())); system.out.println (res.getrequestid ()); } catch (TencentCloudSDKException e) { e.printStackTrace(); }}}Copy the code
In fact, I feel that the official documentation here is really patient. It’s very complete. But the only point I need to add is here
[SMS Helper] */ String SenderId = "";Copy the code
At the beginning, I always thought that there would be this SenderID for international SMS, but the result is that I misunderstood, no matter international SMS or domestic do not need, this thing is not open at all, big customers will give you a. International text messages occur as long as in
+[Country or Region code][Mobile phone number] * An example is as follows: +8613711112222, where there is a + number in front, 86 is the country code, 13711112222 is the mobile phone number, A maximum of 200 mobile phone numbers */ String[] phoneNumberSet = {"+8621212313123", "+8612345678902", "+8612345678903"};Copy the code
Change the country code, and then you can send it if you buy the international package.
There is also a little bit, the website is a bit of a mess.
Domestic and international text messages touch version is not the same oh, if the word ID is created separately is not the same, for our docking special trouble. But there is a point, you can first set up the domestic SMS, and then it has a copy can be copied over. So I have the same ID.
Four,
These large domestic factories do play a particularly good lead role in documentation. For us developers, no customer service for technical docking, follow the documentation, step by step operation. You can implement it. In fact, I have also connected with Tencent cloud COS object file storage, which is also very interesting, I will sort it out in a couple of days. Why is Ali Cloud called OSS? Personally, IT may be that Tencent saw that Ali already has such a product with this name, and I want to build such a product. If the technical name is the same, it is not too LOW, so I will be COS.