tencentyun.go 3.03 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
package tencentyun

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"strconv"
	"strings"
	"time"

	"hilo-micCenter/common/config"

	"github.com/sirupsen/logrus"
	"github.com/tencentyun/tls-sig-api-v2-golang/tencentyun"
)

const (
	timUrl     = "https://console.tim.qq.com/v4"
	overseaUrl = "https://adminapiger.im.qcloud.com/v4"

	basicParameters = "sdkappid={appid}&identifier=administrator&usersig={userSig}&random={random}&contenttype=json"

	sendSystemMsgUrl        = timUrl + "/group_open_http_svc/send_group_system_notification?" + basicParameters
	overseaSendSystemMsgUrl = overseaUrl + "/group_open_http_svc/send_group_system_notification?" + basicParameters
)

func getAdminSig() (string, error) {
	return tencentyun.GenSig(config.GetTencentyunAppId(), config.GetTencentyunKey(), "administrator", 86400*180)
}

func getOverseaAdminSig() (string, error) {
	return tencentyun.GenSig(config.GetTxOverSeaAppId(), config.GetTxOverSeaAppKey(), "administrator", 86400*180)
}

// RespStruct 基本的返回结构
type RespStruct struct {
	ActionStatus string
	ErrorCode    int
	ErrorInfo    string
}

func SendSystemMsg(logger *logrus.Entry, groupId string, members []string, content string) error {
	if config.AppIsRelease() {
		return SendSystemMsgBy(logger, groupId, members, content, overseaSendSystemMsgUrl,
			func() (string, error) {
				return getOverseaAdminSig()
			}, config.GetTxOverSeaAppId())
	} else {
		return SendSystemMsgBy(logger, groupId, members, content, sendSystemMsgUrl,
			func() (string, error) {
				return getAdminSig()
			}, config.GetTencentyunAppId())
	}
}
func SendSystemMsgBy(logger *logrus.Entry, txGroupId string, members []string, content string, reqUrl string, getAdminSig func() (string, error), appId int) error {
	logger = logger.WithField("appId", appId).WithField("txGroupId", txGroupId)
hujiebin's avatar
hujiebin committed
60
	//logger.Infof("SendSystemMsg content: %s", content)
hujiebin's avatar
hujiebin committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	beginTime := time.Now()

	type BodyStruct struct {
		GroupId           string
		ToMembers_Account []string
		Content           string
	}

	sig, err := getAdminSig()
	if err != nil {
		return err
	}
	url := strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(reqUrl,
		"{appid}", strconv.Itoa(appId)),
		"{userSig}", sig),
		"{random}", strconv.FormatUint(uint64(rand.Uint32()), 10))

	body := BodyStruct{
		GroupId:           txGroupId,
		ToMembers_Account: members,
		Content:           content,
	}

	jsonStr, err := json.Marshal(body)
	if err != nil {
		return err
	}

	rsp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonStr))
	if err != nil {
		return err
	}
	defer rsp.Body.Close()

	result, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		return err
	}

	response := RespStruct{}
	if err := json.Unmarshal(result, &response); err != nil {
		return err
	}

	endTime := time.Now()
	logger.Infof("SendSystemMsg takes %dms: rsp: %+v", endTime.Sub(beginTime).Milliseconds(), response)

	if response.ErrorCode == 0 {
		return nil
	} else {
		return fmt.Errorf("tencentyun SendSystemMsg rsp ErrorCode:%v, ErrorInfo:%v", response.ErrorCode, response.ErrorInfo)
	}
}