package tencentyun import ( "bytes" "encoding/json" "fmt" "hilo-micCenter/common/config" "io/ioutil" "math/rand" "net/http" "strconv" "strings" "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) //logger.Infof("SendSystemMsg content: %s", content) //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) } }