DypnsSmsVerifyReport

更新时间:
复制 MD 格式

You can receive SMS verification code delivery status reports (DypnsSmsVerifyReport) through MNS message queues. Configure receipt reception for the SendSmsVerifyCode API.

Prerequisites

DypnsSmsVerifyReport message body format

Name

Type

Example

Description

send_time

String

2021-09-06 15:49:55

Time when the message was forwarded to the carrier.

report_time

String

2021-09-06 15:49:58

Time when the delivery receipt was received from the carrier.

success

Boolean

true

Indicates whether the message was sent successfully. Valid values:

  • true: Sent successfully.

  • false: Failed to send.

err_msg

String

User received successfully

Description of the error code.

err_code

String

DELIVERED

Error code.

phone_number

String

159****6532

Recipient phone number.

sms_size

String

1

SMS length. For details about how SMS length is calculated, see SMS sending rules.

biz_id

String

12345

Delivery receipt ID, also known as the transaction serial number. Each call to the SendSmsVerifyCode API under the same Alibaba Cloud account returns a unique BizId.

out_id

String

123456

The outId passed when you invoke the SendSmsVerifyCode API to send an SMS verification code.

Download demo

See Message Service (formerly MNS) consumption demo. Download the demo and SDK for your preferred programming language. This topic uses Java as an example for subsequent steps.

Note
  • After downloading the demo, import the JAR packages from the lib directory by clicking Add as Library, as shown in the figure below.

  • You can also find the Maven dependency in the pom.xml file and install the Alibaba Cloud SDK for Java.

1

Parameter settings

Before using this example, configure the following parameters.

Set AccessKey

Note

To avoid hard coding your AccessKey pair in source code and risking exposure, retrieve your AccessKey from environment variables. For instructions on setting environment variables, see Configure environment variables on Linux, macOS, and Windows.

This topic uses the environment variable names ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET for the following steps. The following code shows how to retrieve your AccessKey from environment variables:

String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");

Set message type

Replace messageType with your desired message type, such as DypnsSmsVerifyReport for SMS verification code delivery receipts.

String messageType="<MESSAGE_TYPE>";

Set queue name

Replace queueName with your message queue name. You can find it in the SMS verification service > SMS verification parameter settings > Status report reception interface.

String queueName="<QUEUE_NAME>";

In the Domestic SMS section of that page, locate the Queue name field under the MNS message queue consumption mode configuration item (format: Alicom-Queue-xxx) and enter it into the queueName variable in your code.

Complete example

The dealMessage method processes the received status report. Add your business logic for handling upstream message content inside this method.

// Parse the message body according to the specific message format described in this document
String arg = (String) contentMap.get("arg");
// Add your business code here

The arg variable represents a parameter from the receipt message body. Valid values include the following: send_time, report_time, success, err_msg, err_code, phone_number, sms_size, biz_id, and out_id.

package com.alicom.mns.sample;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.alicom.mns.tools.DefaultAlicomMessagePuller;
import com.alicom.mns.tools.MessageListener;
import com.aliyun.mns.model.Message;
import com.google.gson.Gson;
/**
 * Use only for receiving messages from Cloud Communication.
 * Do not use for other services.
 */
public class ReceiveDemo {
    private static Log logger=LogFactory.getLog(ReceiveDemo.class);
    static class MyMessageListener implements MessageListener{
        private Gson gson=new Gson();
        @Override
        public boolean dealMessage(Message message) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // Key message attributes
            System.out.println("message receiver time from mns:" + format.format(new Date()));
            System.out.println("message handle: " + message.getReceiptHandle());
            System.out.println("message body: " + message.getMessageBodyAsString());
            System.out.println("message id: " + message.getMessageId());
            System.out.println("message dequeue count:" + message.getDequeueCount());
            System.out.println("Thread:" + Thread.currentThread().getName());
            try{
                Map<String,Object> contentMap=gson.fromJson(message.getMessageBodyAsString(), HashMap.class);
                // Parse the message body according to the specific message format described in this document
                String arg = (String) contentMap.get("arg");
                // Add your business code here
            }catch(com.google.gson.JsonSyntaxException e){
                logger.error("error_json_format:"+message.getMessageBodyAsString(),e);
                // Message format errors should not occur. If they do, delete the message to prevent repeated failures.
                return true;
            } catch (Throwable e) {
                // Return false for exceptions caused by your own code so the message is not deleted and will be retried based on the retry policy.
                return false;
            }
            // Return true to indicate successful processing. The SDK will then call MNS's delete method to remove the message from the queue.
            return true;
        }
    }
    public static void main(String[] args) throws Exception, ParseException {
        DefaultAlicomMessagePuller puller=new DefaultAlicomMessagePuller();
        // Set thread pool size, task queue size, and idle thread sleep time
        puller.setConsumeMinThreadSize(6);
        puller.setConsumeMaxThreadSize(16);
        puller.setThreadQueueSize(200);
        puller.setPullMsgThreadSize(1);
        // Enable debug logging only during server-side debugging. Disable it otherwise to avoid performance overhead.
        puller.openDebugLog(false);
        // Retrieve AccessKey ID and AccessKey secret from environment variables
        String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        /*
        * Replace messageType and queueName with your actual message type and queue name.
        * Supported Cloud Communication receipt message types:
        * 1: SMS delivery receipt: SmsReport
        * 2: SMS upstream: SmsUp       
        * 3: International SMS delivery receipt: GlobeSmsReport
        */
        String messageType="<MESSAGE_TYPE>"; // Replace with your product's message type
        String queueName="<QUEUE_NAME>"; // After enabling the corresponding service in the Cloud Communication console, you can find the queue name there (format: Alicom-Queue-******-SmsUp)
        puller.startReceiveMsg(accessKeyId,accessKeySecret,messageType,queueName,new MyMessageListener());
    }
}

Sample output after running the demo:

/Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/bin/java ...
Connected to the target VM, address: '127.0.0.1:62393', transport: 'socket'
14:00:07.056 [PullMessageTask-thread-0] DEBUG com.aliyun.mns.client.CloudAccount - initiated CloudAccount, accessId: xxx    accessKey=xxx
message receiver time from mns: xxx
message handle: xxx
message body: xxx
message id: xxx
message dequeue count:1
Thread:pool-1-thread-1