You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.2 KiB
117 lines
3.2 KiB
package im
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/kirinlabs/HttpRequest"
|
|
"github.com/tencentyun/tls-sig-api-v2-golang/tencentyun"
|
|
"hudongzhuanjia/utils"
|
|
)
|
|
|
|
var (
|
|
//SdkAppid = 1400345581
|
|
//SdkAppKey = "e3d095ce8253fe18109d6c1ad068907978cac0a428fbad181d8958631b78e930"
|
|
|
|
SdkAppKey = "9683fc9d2e50857f33c7ef5431942458f2dbc7042ba526f87042092afb646331"
|
|
SdkAppid = 1400337419
|
|
TxImHostV4 = "https://console.tim.qq.com/v4"
|
|
)
|
|
|
|
type NoticeType int
|
|
|
|
const NoticeLiveRedPackStart NoticeType = 256 // 通知直播用户红包开始了
|
|
const NoticeLiveRedPackEnd NoticeType = 257 // 通知直播用户红包结束了
|
|
const NoticeShakeRedPackStart NoticeType = 258 // 通知摇红包开始了
|
|
const NoticeShakeRedPackEnd NoticeType = 259 // 通知摇红包结束了
|
|
|
|
type CommonResult struct {
|
|
ActionStatus string `json:"ActionStatus"`
|
|
ErrorCode int `json:"ErrorCode"`
|
|
ErrorInfo string `json:"ErrorInfo"`
|
|
}
|
|
|
|
type AccountImportParam struct {
|
|
Identifier string `json:"Identifier"`
|
|
Nick string `json:"Nick"`
|
|
FaceUrl string `json:"FaceUrl"`
|
|
}
|
|
|
|
func GenSig(identifier string) (string, error) {
|
|
return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
|
|
}
|
|
|
|
func AccountImport(identifier, nickname, faceurl string) (string, error) {
|
|
//sign, err := tencentyun.GenSig(SdkAppid, SdkAppKey, "admin", 86400*180)
|
|
sig, err := GenSig("admin")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
random := utils.RandomStr(32)
|
|
u := fmt.Sprintf("%s/im_open_login_svc/account_import?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json",
|
|
TxImHostV4, SdkAppid, sig, random)
|
|
bs, _ := json.Marshal(&AccountImportParam{
|
|
Identifier: identifier,
|
|
Nick: nickname,
|
|
FaceUrl: faceurl,
|
|
})
|
|
resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
res := CommonResult{}
|
|
err = resp.Json(&res)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if res.ErrorCode != 0 {
|
|
return "", errors.New(res.ErrorInfo)
|
|
}
|
|
|
|
//return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
|
|
return GenSig(identifier)
|
|
}
|
|
|
|
// 群通知
|
|
type SendGroupSystemNotificationParam struct {
|
|
GroupId string `json:"GroupId"`
|
|
Content string `json:"Content"`
|
|
ToMembersAccount []string `json:"ToMembers_Account,omitempty"`
|
|
}
|
|
|
|
func SendGroupSystemNotification(groupId string, noticeType NoticeType, notice string, members ...string) error {
|
|
sig, err := GenSig("admin")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
random := utils.RandomStr(32)
|
|
u := fmt.Sprintf("%s/group_open_http_svc/send_group_system_notification?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json",
|
|
TxImHostV4, SdkAppid, sig, random)
|
|
|
|
var m = make(map[string]interface{})
|
|
m["type"] = noticeType
|
|
m["notice"] = notice
|
|
content, err := json.Marshal(&m)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
bs, err := json.Marshal(&SendGroupSystemNotificationParam{
|
|
GroupId: groupId,
|
|
Content: string(content),
|
|
ToMembersAccount: members,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
|
|
res := CommonResult{}
|
|
err = resp.Json(&res)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if res.ErrorCode != 0 {
|
|
return errors.New(res.ErrorInfo)
|
|
}
|
|
return nil
|
|
}
|