GO群发营销短信DEMO示例代码
//golang版本Demo
//实现任务提交以及获取余额
package main
import (
"bytes"
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"sort"
"strings"
"time"
)
//批量发送
func batchSend() {
type sendData struct {
APIID string `json:"api_id"`
Signature string `json:"signature"`
Timestamp int `json:"timestamp"`
RequestID string `json:"request_id"`
ProductID int `json:"product_id"` //产品ID
Phone []string `json:"phone"` //手机号
SignName string `json:"sign_name"` //短信签名
Content string `json:"content"` //短信内容
TemplateID int `json:"template_id"` //模板ID
TemplateVar map[string]string `json:"template_var"` //模板变量
SendTime string `json:"send_time"` //定时时间
}
data := sendData{}
data.APIID = "xxxxxx"
data.RequestID = randomString(20, "Aa0")
data.Timestamp = int(time.Now().Unix())
data.Phone = []string{"18801000000", "13101011111"}
data.ProductID = 1013
data.Content = "这是短信内容"
data.SignName = "这是签名"
signData := make(map[string]string)
signData["api_id"] = data.APIID
signData["timestamp"] = fmt.Sprintf("%d", data.Timestamp)
signData["request_id"] = data.RequestID
data.Signature = sign(signData, "xxxxxx")
url := "https://api.ihuyi.com/sms-yx/v1/batchSend"
client := &http.Client{
Timeout: time.Second * 10,
}
sendRawData, _ := json.Marshal(data)
request, err := http.NewRequest("POST", url, strings.NewReader(string(sendRawData)))
if err != nil {
fmt.Print(err)
return
}
request.Header.Set("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
fmt.Print(err)
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Print(err)
return
}
getData := string(body)
fmt.Println(getData)
}
//获取余额
func balance() {
type sendData struct {
APIID string `json:"api_id"`
Signature string `json:"signature"`
Timestamp int `json:"timestamp"`
RequestID string `json:"request_id"`
}
data := sendData{}
data.APIID = "xxxxxxx"
data.RequestID = randomString(20, "Aa0")
data.Timestamp = int(time.Now().Unix())
signData := make(map[string]string)
signData["api_id"] = data.APIID
signData["timestamp"] = fmt.Sprintf("%d", data.Timestamp)
signData["request_id"] = data.RequestID
data.Signature = sign(signData, "xxxxxxx")
url := "https://api.ihuyi.com/sms-yx/v1/balance"
client := &http.Client{
Timeout: time.Second * 10,
}
sendRawData, _ := json.Marshal(data)
request, err := http.NewRequest("POST", url, strings.NewReader(string(sendRawData)))
if err != nil {
fmt.Print(err)
return
}
request.Header.Set("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
fmt.Print(err)
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Print(err)
return
}
getData := string(body)
fmt.Println(getData)
}
//getMd5String 返回md5加密字符串
func getMd5String(s string) string {
h := md5.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
//获取签名
func sign(data map[string]string, appKEY string) string {
var keys []string
data["api_key"] = appKEY
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
var hash []string
for _, v := range keys {
hash = append(hash, fmt.Sprintf("%s=%s", v, data[v]))
}
return getMd5String(strings.Join(hash, "&"))
}
//RandomString 随机生成字符串
//RandomString(8, "A")
//RandomString(8, "a0")
//RandomString(20, "Aa0")
func randomString(randLength int, randType string) (result string) {
var num = "0123456789"
var lower = "abcdefghijklmnopqrstuvwxyz"
var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := bytes.Buffer{}
if strings.Contains(randType, "0") {
b.WriteString(num)
}
if strings.Contains(randType, "a") {
b.WriteString(lower)
}
if strings.Contains(randType, "A") {
b.WriteString(upper)
}
var str = b.String()
var strLen = len(str)
if strLen == 0 {
result = ""
return
}
rand.Seed(time.Now().UnixNano())
b = bytes.Buffer{}
for i := 0; i < randLength; i++ {
b.WriteByte(str[rand.Intn(strLen)])
}
result = b.String()
return
}
func main() {
batchSend()
balance()
}