云之讯短信接口GO实现

Victorfun · · 2835 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

 将项目变量appId, accountSId和token使用自己的替换掉就可以了。

发送信息时直接调用函数SendSMS即可,传入参数电话号码,模板ID以及模板中参数。由于云之讯的参数是以逗号分隔的,所以参数值中不能有逗号,同时参数值也不能为空。

package UcGoSdk

import (
	"bytes"
	"errors"
	"crypto/md5"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"
)

const (
	softVersion   = "2014-06-30"
	baseUrl       = "https://api.ucpaas.com"
	appId         = "appId"
	accountSId    = "accountSId"
	token         = "token"
)

var timestamp string

type UcPaaS struct {
	TmplSMS templateSMS `json:"templateSMS"`
}

type templateSMS struct {
	AppId      string `json:"appId"`
	TemplateId string `json:"templateId"`
	PhoneNum   string `json:"to"`
	Param      string `json:"param"`
}

type result struct {
	Resp response `json:"resp"`
}

type response struct {
	RespCode      string        `json:"respCode"`
	Failure       int           `josn:"failure,omitempty"`
	ReturnTmplSMS returnTmplSMS `json:"templateSMS"`
}

type returnTmplSMS struct {
	CreateDate string `json:"createDate"`
	SMSId      string `json:"smsId"`
}

/**
 * Name: initUc
 * Description: Create the instance of UcPaaS
 */
func initUc(phoneNum, tempId string, params ...string) *UcPaaS {
	curTime := time.Now().Format("20060102150405000")
	intCurTime, _ := strconv.Atoi(curTime)
	timestamp = strconv.Itoa(intCurTime)
	catParam := ""
	for index, param := range params {
		if index == 0 {
			catParam = param
		} else {
			catParam = catParam + "," + param
		}
	}
	//param := verifyCode + "," + expireTime

	return &UcPaaS{
		TmplSMS: templateSMS{
			AppId:      appId,
			TemplateId: tempId,
			PhoneNum:   phoneNum,
			Param:      catParam,
		},
	}
}

/**
 * Name: getAuthorization
 * Descriotion: 包头验证信息,使用Base64编码(账户Id:时间戳)
 */
func (res *result) getAuthorization(accountSId string) string {
	src := accountSId + ":" + timestamp
	return base64.StdEncoding.EncodeToString([]byte(src))
}

/**
 * Name: getSigParameter
 * Description: 验证参数,URL后必须带有sig参数,sig= MD5(账户Id + 账户授权令牌 + 时间戳,共32位)(注:转成大写)
 */
func (res *result) getSigParameter(accountSId, token string) string {
	sig := accountSId + token + timestamp
	//获取MD5
	return strings.ToUpper(fmt.Sprintf("%x", md5.Sum([]byte(sig))))
}

/**
 * Name: getResult
 * @return respcode 000000表示发送成功
 *
 */
func (res *result) getResult(url, dataType, method string, data []byte) {
	resp := res.connection(url, dataType, method, data)
	_ = json.Unmarshal([]byte(resp), res)
}

/**
 * Name: connection
 * @param url
 * @param dataType
 * @param method post或get
 * @param body POST数据
 * @return string
 */
func (res *result) connection(url, dataType, method string, data []byte) string {
	mine := ""
	if dataType == "json" {
		mine = "application/json"
	} else {
		mine = "application/xml"
	}
	client := &http.Client{}
	//Get data
	req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
	if err != nil {
		return err.Error()
	}
	req.Header.Add("Accept", mine)
	req.Header.Add("Content-Type", (mine + ";charset=utf-8"))
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	req.Header.Add("Authorization", res.getAuthorization(accountSId))
	req.Header.Add("Content-Length", strconv.Itoa(len(data)))

	resp, err := client.Do(req)
	defer resp.Body.Close()
	if err != nil {
		return ""
	}
	respBody, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return ""
	}
	return string(respBody)
}

/**
 * Name:SendSMS
 * Description: Send the text message.
 * @param phoneNum: The phone number
 * @param tempId: The message template id
 * @param params... : The parameter list.
 */
func SendSMS(phoneNum, tempId string, params ...string) (respCode string, err error) {
	var rs result
	//初始化
	uc := initUc(phoneNum, tempId, params...)
	//获取Url
	resource := "/" + softVersion + "/Accounts/" + accountSId + "/Messages/templateSMS"
	parseUrl, err := url.ParseRequestURI(baseUrl)
	if err != nil {
		return "", errors.New("请求地址出错")
	}
	parseUrl.Path = resource
	urlStr := fmt.Sprintf("%v?sig=%s", parseUrl, rs.getSigParameter(accountSId, token))
	//获取post数据
	data, err := json.MarshalIndent(uc, "", "  ")
	if err != nil {
		return "", err
	}
	//POST必须大写
	rs.getResult(urlStr, "json", "POST", data)
	respCode = rs.Resp.RespCode
	if respCode != "000000" {
		return respCode, errors.New("发送验证码失败。")
	}
	return "", nil
}

 


有疑问加站长微信联系(非本文作者)

本文来自:开源中国博客

感谢作者:Victorfun

查看原文:云之讯短信接口GO实现

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2835 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传