commit 1ee4d0cadd387c4c50a4ae9cadfd21273ac96045 Author: xcx Date: Tue Mar 24 20:19:47 2020 +0800 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61da167 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +txim \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..91818e7 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# txim diff --git a/api/api.go b/api/api.go new file mode 100644 index 0000000..6c927a5 --- /dev/null +++ b/api/api.go @@ -0,0 +1,74 @@ +package api + +import ( + "fmt" + "math/rand" + + "github.com/imroc/req" + "github.com/tencentyun/tls-sig-api-v2-golang/tencentyun" +) + +const ( + API_HOST = "console.tim.qq.com" + API_VERSION = "v4" +) + +var g struct { + sdkAppID int + identifier string + key string +} + +func Init(sdkAppID int, key, identifier string) { + g.sdkAppID = sdkAppID + g.key = key + g.identifier = identifier +} + +func UserSig(identifier string, expire int) string { + sig, _ := tencentyun.GenSig(g.sdkAppID, g.key, g.identifier, expire) + return sig +} + +//https://cloud.tencent.com/document/product/269/1615 +type GroupCreateReq struct { + OwnerAccount string `json:"Owner_Account"` + Type string `json:"Type"` + Name string `json:"Name"` + ApplyJoinOption string `json:"ApplyJoinOption"` +} +type GroupCreateAns struct { + ActionStatus string `json:"ActionStatus"` + ErrorInfo string `json:"ErrorInfo"` + ErrorCode int `json:"ErrorCode"` + GroupId string `json:"GroupId"` +} + +func GroupCreate(r *GroupCreateReq) (*GroupCreateAns, error) { + a := GroupCreateAns{} + err := Api("group_open_http_svc", "create_group", r, &a) + return &a, err +} + +func Api(servicename, command string, in, out interface{}) error { + host := fmt.Sprintf("https://%s/%s/%s/%s", API_HOST, API_VERSION, servicename, command) + + query := req.QueryParam{ + "sdkappid": g.sdkAppID, + "identifier": g.identifier, + "usersig": UserSig(g.identifier, 5*60), + "random": rand.Uint32(), + "contenttype": "json", + } + + resp, err := req.Post(host, query, req.BodyJSON(in)) + if err != nil { + return err + } + + if err := resp.ToJSON(out); err != nil { + return err + } + + return nil +} diff --git a/demo.go b/demo.go new file mode 100644 index 0000000..cfe2bce --- /dev/null +++ b/demo.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + + txim "github.com/xucx/txim/api" +) + +func main() { + txim.Init(1, "xxx", "xxx") + + //生成签名 + sig := txim.UserSig("xxx", 300) + fmt.Println("签名:", sig) + + //创建房间 + ans, err := txim.GroupCreate(&txim.GroupCreateReq{ + OwnerAccount: "administrator", + Type: "ChatRoom", + Name: "测试聊天室", + ApplyJoinOption: "FreeAccess", + }) + fmt.Printf("创建房间结果:%x,%+v\n", err, ans) + +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..45c2dca --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/xucx/txim + +go 1.13 + +require ( + github.com/imroc/req v0.3.0 + github.com/tencentyun/tls-sig-api-v2-golang v1.0.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6b47a9e --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/imroc/req v0.3.0 h1:3EioagmlSG+z+KySToa+Ylo3pTFZs+jh3Brl7ngU12U= +github.com/imroc/req v0.3.0/go.mod h1:F+NZ+2EFSo6EFXdeIbpfE9hcC233id70kf0byW97Caw= +github.com/tencentyun/tls-sig-api-v2-golang v1.0.0 h1:NavMw9XO2iCLv8hTKaJW2kTaGR2SdNljMABbe39yu6Q= +github.com/tencentyun/tls-sig-api-v2-golang v1.0.0/go.mod h1:u7WiArmCTXTaQAHJwAOaLgpJ5e2xdY5/cgMEy3ubL60=