互动
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.

181 lines
4.9 KiB

package im
import (
"encoding/json"
"errors"
"fmt"
"github.com/kirinlabs/HttpRequest"
"hudongzhuanjia/utils"
)
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, status int, data map[string]interface{}, 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["status"] = status
m["data"] = data
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))
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 nil
}
// https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
type MsgBody struct {
MsgType string `json:"MsgType"`
MsgContent MsgBodyContent `json:"MsgContent"`
}
type MsgBodyContent struct {
Data string `json:"Data"`
Desc string `json:"Desc"`
Ext string `json:"Ext"`
Sound string `json:"Sound"`
}
type SendGroupMessageParam struct {
GroupId string `json:"GroupId"`
Random int `json:"Random"`
MsgPriority string `json:"MsgPriority,omitempty"`
MsgBody []MsgBody `json:"MsgBody"`
FromAccount string `json:"From_Account,omitempty"`
OfflinePushInfo interface{} `json:"OfflinePushInfo,omitempty"`
ForbidCallbackControl interface{} `json:"ForbidCallbackControl,omitempty"`
OnlineOnlyFlag int `json:"OnlineOnlyFlag,omitempty"`
}
func SendGroupMessage(param *SendGroupMessageParam) error {
sig, err := GenSig("admin")
if err != nil {
return err
}
random := utils.RandomInt(16)
u := fmt.Sprintf("%s/group_open_http_svc/send_group_msg?sdkappid=%d&identifier=admin&usersig=%s&random=%v&contenttype=json",
TxImHostV4, SdkAppid, sig, random)
bs, err := json.Marshal(param)
if err != nil {
return err
}
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 nil
}
type QueryStateResult struct {
ActionStatus string `json:"ActionStatus"`
ErrorInfo string `json:"ErrorInfo"`
ErrorCode int `json:"ErrorCode"`
QueryResult []*QueryStateResultItem `json:"QueryResult"`
}
type QueryStateResultItem struct {
ToAccount string `json:"ToAccount"`
Status string `json:"Status"`
}
// https://console.tim.qq.com/v4/openim/querystate?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
func QueryState(ids []string) (*QueryStateResult, error) {
sig, err := GenSig("admin")
if err != nil {
return nil, err
}
random := utils.RandomInt(16)
u := fmt.Sprintf("%s/openim/querystate?sdkappid=%d&identifier=admin&usersig=%s&random=%v&contenttype=json",
TxImHostV4, SdkAppid, sig, random)
bs, err := json.Marshal(map[string]interface{}{
"To_Account": ids,
})
if err != nil {
return nil, err
}
resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
if err != nil {
return nil, err
}
var res = QueryStateResult{}
err = resp.Json(&res)
if err != nil {
return nil, err
}
if res.ErrorCode != 0 {
return nil, errors.New(res.ErrorInfo)
}
return &res, nil
}