本文为您介绍短信服务Go SDK的使用方法及示例。

准备工作

  • 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见AccessKey
  • 使用Govendor工具执行以下命令,安装阿里云Go SDK。
    go get github.com/alibabacloud-go/dysmsapi-20170525/v2

发送短信

  • 代码示例
    package main
    
    import (
      "os"
      dysmsapi20170525  "github.com/alibabacloud-go/dysmsapi-20170525/v2/client"
      openapi  "github.com/alibabacloud-go/darabonba-openapi/client"
      "github.com/alibabacloud-go/tea/tea"
    )
    
    
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    func CreateClient (accessKeyId *string, accessKeySecret *string) (_result *dysmsapi20170525.Client, _err error) {
      config := &openapi.Config{
        // 您的AccessKey ID
        AccessKeyId: accessKeyId,
        // 您的AccessKey Secret
        AccessKeySecret: accessKeySecret,
      }
      // 访问的域名
      config.Endpoint = tea.String("dysmsapi.aliyuncs.com")
      _result = &dysmsapi20170525.Client{}
      _result, _err = dysmsapi20170525.NewClient(config)
      return _result, _err
    }
    
    func _main (args []*string) (_err error) {
      client, _err := CreateClient(tea.String("accessKeyId"), tea.String("accessKeySecret"))
      if _err != nil {
        return _err
      }
    
      sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
        PhoneNumbers: tea.String("1503871****"),
        SignName: tea.String("阿里大于测试专用"),
        TemplateCode: tea.String("SMS_71255707"),
        TemplateParam: tea.String("{\"customer\":\"1111\"}"),
      }
      // 复制代码运行请自行打印 API 的返回值
      _, _err = client.SendSms(sendSmsRequest)
      if _err != nil {
        return _err
      }
      return _err
    }
    
    
    func main() {
      err := _main(tea.StringSlice(os.Args[1:]))
      if err != nil {
        panic(err)
      }
    }
                        
  • 结果示例
    {  "RequestId": "614048FB-0619-4439-A1D5-AA8B218A****",  "Message": "OK",  "BizId": "386715418801811068^0",  "Code": "OK"}

相关文档

  • 如果您想了解更多示例,请参见OpenAPI开发者门户
    说明 登录开发者门户平台,SDK版本选择时,请选择升级版 SDK
  • 短信服务相关API,请参见API概览