• 七牛云短信


    目录

    代码的接入,基于七牛官方 sdk

    产品定价

    注意

    短信发送频率限制

    短信内容长度计算规则

    • 短信字数 = 短信模版内容字数 + 签名字数
    • 短信字数 ≤ 70 个字数,按照 70 个字数一条短信计算
    • 短信字数 > 70 个字数,即为长短信,按照 67 个字数记为一条短信计算

    Java 接入

    代码具体见 Github

    1. 创建签名代码

    创建好后需要等待审核

    import com.qiniu.common.QiniuException;
    import com.qiniu.common.Zone;
    import com.qiniu.http.Client;
    import com.qiniu.http.Response;
    import com.qiniu.storage.Configuration;
    import com.qiniu.util.Auth;
    import com.qiniu.util.Base64;
    import com.qiniu.util.Json;
    import com.qiniu.util.StringMap;
    import me.lj.qiniu.config.Config;
    
    /**
     * 创建签名
     * https://developer.qiniu.com/sms/api/5844/sms-api-create-signature
     */
    public class CreateSignature {
        public static void main(String[] args) throws QiniuException {
            String ACCESS_KEY = "";
            String SECRET_KEY = "";
    
            Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
    
            String url = "https://sms.qiniuapi.com/v1/signature";
            String host = "sms.qiniuapi.com";
            String contentType = "application/json";
            String body = "{"signature":"Test1","source":"website"}";
    
            String qiniuToken = "Qiniu "+auth.signRequestV2(url, "POST", body.getBytes(), contentType);
            System.out.println(qiniuToken);
    
            StringMap header = new StringMap();
            header.put("Host", host);
            header.put("Authorization", qiniuToken);
            header.put("Content-Type", contentType);
            Configuration c = new Configuration(Zone.zone1());
            Client client = new Client(c);
            Response response = client.post(url, body.getBytes(), header, contentType);
    
            System.out.println("Response: " + response.bodyString());
        }
    }
    

    运行结果

    运行成功,返回 signature_id

    2. 创建和列举模板

    public class CreateAndListTemplate {
        public static void main(String[] args) throws QiniuException {
            String accessKey = Config.SMS_ACCESS_KEY;
            String secretKey = Config.SMS_SECRET_KEY;
    
            Auth auth = Auth.create(accessKey, secretKey);
            String url;
            String host;
            String contentType;
            String method;
            String body;
            Response response;
    
            //create
            url = "https://sms.qiniuapi.com/v1/template";
            host = "sms.qiniuapi.com";
            contentType = "application/json";
            method = "POST";
            //{ "name": "test-template", "template": "Test1", "type": "verification", "description": "测试", "signature_id": "1131017766455742464" }
            body = "{ "name": "test-t", "template": "Test1", "type": "verification", "description": "测试", "signature_id": "1131017766455742464" }";
            response = RPCCall.call(auth, url, host, contentType, method, body);
            System.out.println("Create Res: " + response.bodyString());
    
            //list
            url = "https://sms.qiniuapi.com/v1/template";
            contentType = "application/x-www-form-urlencoded";
            method = "GET";
            response = RPCCall.call(auth, url, host, contentType, method, "");
            System.out.println("List Res: " + response.bodyString());
    
        }
    }
    

    运行结果

    创建好后需要等待审核

    3. 发送短信

    /**
     * 短信
     * https://developer.qiniu.com/sms/api/5897/sms-api-send-message
     */
    public class SnedMessage {
        public static void main(String[] args) throws QiniuException {
            String accessKey = Config.SMS_ACCESS_KEY;
            String secretKey = Config.SMS_SECRET_KEY;
    
            Auth auth = Auth.create(accessKey, secretKey);
            String url;
            String host;
            String contentType;
            String method;
            String body;
            Response response;
    
            //send
            url = "https://sms.qiniuapi.com/v1/message";
            host = "sms.qiniuapi.com";
            contentType = "application/json";
            method = "POST";
            body = "{ "signature_id": "1131017766455742464", "template_id": "1131211147840589824", "mobiles": [ "18801732070" ], "parameters": { "code": 123456 } }";
            response = RPCCall.call(auth, url, host, contentType, method, body);
    
            System.out.println("send Res: " + response.bodyString());
        }
    }
    

    运行结果

    调用成功,返回 job_id。

    接收到短信

    Python 接入

    代码

    运行结果

    运行成功,返回 signature_id

    七牛 portal 界面显示新创建的签名

    PHP 接入

    代码

    <?php
    require_once __DIR__ . '/../../autoload.php';
    
    use QiniuAuth;
    use QiniuHttpClient;
    
    //  https://developer.qiniu.com/sms/api/5844/sms-api-create-signature
    $accessKey = "DP4FyFXIHuThsAqZec6ykFkqjMy6EmSzC1Amd3hd";
    $secretKey = "BoG_hT6idwA85rFQ4vpmJGiHXzVOur9RtQm6RtaQ";
    
    // 初始化Auth状态
    $auth = new Auth($accessKey, $secretKey);
    
    // 初始化请求参数
    $url = "https://sms.qiniuapi.com/v1/signature";
    $method = "POST";
    $host = "sms.qiniuapi.com";
    $contentType = "application/json";
    $body = "{"signature":"php","source":"website"}";
    
    // 鉴权凭证
    $headers = $auth->authorizationV2($url, $method, $body, $contentType);
    $headers['Content-Type'] = $contentType;
    $headers['Host'] = $host;
    
    $response = Client::post($url, $bodyJson, $headers);
    $r = $response->json();
    var_dump($r);
    if ($response->ok()) {
        $r = $response->json();
        var_dump($r);
    }
    

    运行结果

    返回

    {
      'signature_id' =>
      string(19) "1131051944643665920"
    }
    

    GO 接入

    sdk 代码见:https://github.com/qiniu/api.v7/tree/master/sms

    代码

    package sms_test
    
    import (
        "testing"
    
        "github.com/qiniu/api.v7/sms"
    )
    
    func TestSignature(t *testing.T) {
    
        // CreateSignature
        args := sms.SignatureRequest{
            Signature: "Test",
            Source:    sms.Website,
        }
    
        ret, err := manager.CreateSignature(args)
    
        if err != nil {
            t.Fatalf("CreateSignature() error: %v
    ", err)
        }
    
        if len(ret.SignatureID) == 0 {
            t.Fatal("CreateSignature() error: The signature ID cannot be empty")
        }
    
        // QuerySignature
        query := sms.QuerySignatureRequest{}
    
        pagination, err := manager.QuerySignature(query)
    
        if err != nil {
            t.Fatalf("QuerySignature() error: %v
    ", err)
        }
    
        if len(pagination.Items) == 0 {
            t.Fatal("QuerySignature() error: signatures cannot be empty")
        }
    
        if pagination.Total == 0 {
            t.Fatal("QuerySignature() error: total cannot be 0")
        }
    
        // UpdateSignature
        update := sms.SignatureRequest{
            Signature: "test",
        }
    
        err = manager.UpdateSignature(ret.SignatureID, update)
        if err != nil {
            t.Fatalf("UpdateSignature() error: %v
    ", err)
        }
    
        // DeleteSignature
        err = manager.DeleteSignature(ret.SignatureID)
        if err != nil {
            t.Fatalf("DeleteSignature() error: %v
    ", err)
        }
    
    }
    

    运行结果

    参考

    七牛云短信

    建议

    1. 不要叫管理凭证,容易和存储混淆
    Go!!!
  • 相关阅读:
    养兔引起轩然大波,做事情执着的他竟做到了年产值3000万元
    凭良心用好料,做卫生包子让他的日营业收入近3000元
    肉牛养殖前景好,他尝到甜头带富了贫困户
    那些年,我们处理过的SQL问题
    windows右键没有新建选项的解决办法
    windows右键没有新建选项的解决办法
    IT服务,共享经济的下一个风口?
    IT服务,共享经济的下一个风口?
    IT服务,共享经济的下一个风口?
    IT服务,共享经济的下一个风口?
  • 原文地址:https://www.cnblogs.com/shliujing/p/qi-niu-yun-duan-xin--Java-PHP-Python-Go-yu-yan-jie.html
Copyright © 2020-2023  润新知