Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

阿里云短信

Feature: sms-ali

📖 官方文档:https://help.aliyun.com/zh/sms/developer-reference/api-dysmsapi-2017-05-25-sendsms

纯 Rust 实现,通过阿里云 SMS API 直接发送短信,不依赖第三方 SDK。

config.toml 配置

[sms_ali]
access_key_id = "your-access-key-id"
access_key_secret = "your-access-key-secret"
sign_name = "你的签名"       # 默认短信签名
template_code = "SMS_123456" # 默认验证码模板 Code

API

SmsAli

方法参数返回说明
new-SmsAli创建空实例
sendphone_numbers, sign_name?, template_code?, template_param?Result<AliSmsResponse>发送短信(通用)
send_codephone_numbers, codeResult<AliSmsResponse>发送验证码短信
send_templatephone_numbers, sign_name, template_code, template_paramResult<AliSmsResponse>发送自定义模板短信
with_report_callbackcallbackSelf注册短信回执回调函数

AliSmsResponse

字段类型说明
request_idString请求 ID
codeString状态码,"OK" 表示成功
messageString状态描述
biz_idOption<String>发送回执 ID

使用示例

#![allow(unused)]
fn main() {
// 发送验证码(使用 config.toml 中的默认签名和模板)
let resp = state.sms_ali.send_code("13800138000", "1234").await?;

// 发送自定义模板
let resp = state
    .sms_ali
    .send_template(
        "13800138000",
        "你的签名",
        "SMS_123456",
        r#"{"name":"张三","order":"20250101"}"#,
    )
    .await?;

// 批量发送(逗号分隔,最多 100 个)
let resp = state
    .sms_ali
    .send("13800138000,13900139000", None, None, Some(r#"{"code":"5678"}"#))
    .await?;
}

签名机制

使用阿里云 RPC 签名 V1(HMAC-SHA1),与 OSS 模块相同的签名方式,纯 Rust 实现。

回执回调

启用 sms-ali feature 后,框架会自动在 config.tomlsms_ali.callback_path(默认 sms/ali/report)注册 POST 路由,用于接收阿里云推送的短信回执报告。

配置

[sms_ali]
callback_path = "sms/ali/report"  # 可选,默认值如左

注册回调

#![allow(unused)]
fn main() {
use afaster::SmsAli;
use afaster::state::smsali::{AliSmsReport, AliSmsCallbackResult};

let app = AFaster::new()
    .with_sms_ali(|ali| {
        ali.with_report_callback(|(state, reports): (AppState, Vec<AliSmsReport>)| {
            async move {
                for report in reports {
                    if report.success {
                        tracing::info!(phone = %report.phone_number, "短信送达");
                    } else {
                        tracing::warn!(phone = %report.phone_number, err = %report.err_code, "短信未送达");
                    }
                }
                Ok(AliSmsCallbackResult::ok())
            }
        })
    })
    .run()
    .await;
}

注意:未注册回调时,回执报告会被静默丢弃并返回成功(避免云平台重试),开启 log feature 会打印 debug 日志。

AliSmsReport 字段

字段类型说明
biz_idString发送回执 ID(与 SendSms 返回的 BizId 对应)
phone_numberString手机号
send_timeString发送时间
report_timeString运营商回执时间
successbool是否成功送达
err_codeString运营商错误码(成功时为空)
err_msgString运营商错误描述
template_codeString短信模板 Code
sign_nameString短信签名
dest_codeString上行短信扩展码(SmsUp 类型)
contentString上行短信内容(SmsUp 类型)

错误码

错误码English中文
41101SMS credentials not configured短信凭证未配置
51101HMAC-SHA1 init failedHMAC-SHA1 初始化失败
51103Alibaba SMS request failed阿里云短信请求失败
51104Alibaba SMS response parse failed阿里云短信响应解析失败
51105Alibaba SMS API error阿里云短信 API 返回错误
51106SMS report callback not registered短信回执回调未注册