spring_sms/
client.rs

1use std::collections::HashMap;
2use spring::tracing;
3use crate::aliyun::Aliyun;
4use crate::config::SmsConfig;
5
6#[derive(Debug, Clone)]
7pub struct SmsClient {
8    config: SmsConfig,
9}
10
11impl SmsClient {
12    pub fn new(config: SmsConfig) -> Self {
13        Self { config }
14    }
15    pub async fn send_sms_by_aliyun(&self, phone_numbers: &str, template_code: &str, template_param: Option<HashMap<&str, &str>>) -> anyhow::Result<()> {
16        let aliyun_config = &self.config.aliyun;
17        match aliyun_config {
18            None => Err(anyhow::Error::msg("Sms config not found")),
19            Some(config) => {
20                Aliyun::new(config)
21                    .send_sms(phone_numbers, template_code, template_param)
22                    .await
23                    .map_err(|e| {
24                        tracing::error!("Sms Send Fail: {}", e);
25                        anyhow::Error::msg(format!("{}", e))
26                    })
27            }
28        }
29    }
30}