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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package im
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/kirinlabs/HttpRequest"
  7. "hudongzhuanjia/utils"
  8. )
  9. func AccountImport(identifier, nickname, faceurl string) (string, error) {
  10. //sign, err := tencentyun.GenSig(SdkAppid, SdkAppKey, "admin", 86400*180)
  11. sig, err := GenSig("admin")
  12. if err != nil {
  13. return "", err
  14. }
  15. random := utils.RandomStr(32)
  16. u := fmt.Sprintf("%s/im_open_login_svc/account_import?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json",
  17. TxImHostV4, SdkAppid, sig, random)
  18. bs, _ := json.Marshal(&AccountImportParam{
  19. Identifier: identifier,
  20. Nick: nickname,
  21. FaceUrl: faceurl,
  22. })
  23. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  24. if err != nil {
  25. return "", err
  26. }
  27. res := CommonResult{}
  28. err = resp.Json(&res)
  29. if err != nil {
  30. return "", err
  31. }
  32. if res.ErrorCode != 0 {
  33. return "", errors.New(res.ErrorInfo)
  34. }
  35. //return tencentyun.GenSig(SdkAppid, SdkAppKey, identifier, 86400000)
  36. return GenSig(identifier)
  37. }
  38. // 群通知
  39. type SendGroupSystemNotificationParam struct {
  40. GroupId string `json:"GroupId"`
  41. Content string `json:"Content"`
  42. ToMembersAccount []string `json:"ToMembers_Account,omitempty"`
  43. }
  44. func SendGroupSystemNotification(groupId string, status int, data map[string]interface{}, members ...string) error {
  45. sig, err := GenSig("admin")
  46. if err != nil {
  47. return err
  48. }
  49. random := utils.RandomStr(32)
  50. u := fmt.Sprintf("%s/group_open_http_svc/send_group_system_notification?sdkappid=%d&identifier=admin&usersig=%s&random=%s&contenttype=json",
  51. TxImHostV4, SdkAppid, sig, random)
  52. var m = make(map[string]interface{})
  53. m["status"] = status
  54. m["data"] = data
  55. content, err := json.Marshal(&m)
  56. if err != nil {
  57. return err
  58. }
  59. bs, err := json.Marshal(&SendGroupSystemNotificationParam{
  60. GroupId: groupId,
  61. Content: string(content),
  62. ToMembersAccount: members,
  63. })
  64. if err != nil {
  65. return err
  66. }
  67. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  68. if err != nil {
  69. return err
  70. }
  71. res := CommonResult{}
  72. err = resp.Json(&res)
  73. if err != nil {
  74. return err
  75. }
  76. if res.ErrorCode != 0 {
  77. return errors.New(res.ErrorInfo)
  78. }
  79. return nil
  80. }
  81. // https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  82. type MsgBody struct {
  83. MsgType string `json:"MsgType"`
  84. MsgContent MsgBodyContent `json:"MsgContent"`
  85. }
  86. type MsgBodyContent struct {
  87. Data string `json:"Data"`
  88. Desc string `json:"Desc"`
  89. Ext string `json:"Ext"`
  90. Sound string `json:"Sound"`
  91. }
  92. type SendGroupMessageParam struct {
  93. GroupId string `json:"GroupId"`
  94. Random int `json:"Random"`
  95. MsgPriority string `json:"MsgPriority,omitempty"`
  96. MsgBody []MsgBody `json:"MsgBody"`
  97. FromAccount string `json:"From_Account,omitempty"`
  98. OfflinePushInfo interface{} `json:"OfflinePushInfo,omitempty"`
  99. ForbidCallbackControl interface{} `json:"ForbidCallbackControl,omitempty"`
  100. OnlineOnlyFlag int `json:"OnlineOnlyFlag,omitempty"`
  101. }
  102. func SendGroupMessage(param *SendGroupMessageParam) error {
  103. sig, err := GenSig("admin")
  104. if err != nil {
  105. return err
  106. }
  107. random := utils.RandomInt(16)
  108. u := fmt.Sprintf("%s/group_open_http_svc/send_group_msg?sdkappid=%d&identifier=admin&usersig=%s&random=%v&contenttype=json",
  109. TxImHostV4, SdkAppid, sig, random)
  110. bs, err := json.Marshal(param)
  111. if err != nil {
  112. return err
  113. }
  114. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  115. if err != nil {
  116. return err
  117. }
  118. res := CommonResult{}
  119. err = resp.Json(&res)
  120. if err != nil {
  121. return err
  122. }
  123. if res.ErrorCode != 0 {
  124. return errors.New(res.ErrorInfo)
  125. }
  126. return nil
  127. }
  128. type QueryStateResult struct {
  129. ActionStatus string `json:"ActionStatus"`
  130. ErrorInfo string `json:"ErrorInfo"`
  131. ErrorCode int `json:"ErrorCode"`
  132. QueryResult []*QueryStateResultItem `json:"QueryResult"`
  133. }
  134. type QueryStateResultItem struct {
  135. ToAccount string `json:"ToAccount"`
  136. Status string `json:"Status"`
  137. }
  138. // https://console.tim.qq.com/v4/openim/querystate?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  139. func QueryState(ids []string) (*QueryStateResult, error) {
  140. sig, err := GenSig("admin")
  141. if err != nil {
  142. return nil, err
  143. }
  144. random := utils.RandomInt(16)
  145. u := fmt.Sprintf("%s/openim/querystate?sdkappid=%d&identifier=admin&usersig=%s&random=%v&contenttype=json",
  146. TxImHostV4, SdkAppid, sig, random)
  147. bs, err := json.Marshal(map[string]interface{}{
  148. "To_Account": ids,
  149. })
  150. if err != nil {
  151. return nil, err
  152. }
  153. resp, err := HttpRequest.NewRequest().Debug(true).JSON().Post(u, string(bs))
  154. if err != nil {
  155. return nil, err
  156. }
  157. var res = QueryStateResult{}
  158. err = resp.Json(&res)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if res.ErrorCode != 0 {
  163. return nil, errors.New(res.ErrorInfo)
  164. }
  165. return &res, nil
  166. }