message.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package hook
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gorilla/websocket"
  7. "wechat-api/workphone"
  8. )
  9. // 开启/关闭实时消息接收功能
  10. func (h *Hook) ConfigureMsgRecive(isEnable string, url string) (err error) {
  11. resp, err := h.Client.R().SetBody(&ConfigureMsgReciveReq{
  12. IsEnable: isEnable,
  13. URL: url,
  14. }).Post("http://" + h.ServerIp + ":" + h.WxPort + "/ConfigureMsgRecive")
  15. if err != nil {
  16. return
  17. }
  18. if !resp.IsSuccessState() {
  19. err = fmt.Errorf("ConfigureMsgRecive failed with status code %d", resp.StatusCode)
  20. return
  21. }
  22. return
  23. }
  24. // 发送微信文本消息
  25. func (h *Hook) SendTextMsg(wxid, msg, wxWxid string) error {
  26. if h.ServerIp == "" {
  27. conn, err := h.connWorkPhone()
  28. if err != nil {
  29. err = fmt.Errorf("SendTextMsg failed")
  30. return err
  31. }
  32. defer func(conn *websocket.Conn) {
  33. err = conn.Close()
  34. if err != nil {
  35. err = fmt.Errorf("SendTextMsg failed")
  36. }
  37. }(conn)
  38. encodedString := base64.StdEncoding.EncodeToString([]byte(msg))
  39. //sendMsg := &workphone.TalkToFriendTaskMessage{
  40. // WeChatId: wxWxid,
  41. // FriendId: wxid,
  42. // ContentType: workphone.EnumContentType_Text,
  43. // Content: []byte(encodedString),
  44. //}
  45. //content, err := anypb.New(sendMsg) <-- 这里有坑,会增加字段 "@type": "type.googleapis.com/Jubo.JuLiao.IM.Wx.Proto.TalkToFriendTaskMessage",
  46. //if err != nil {
  47. // return fmt.Errorf("failed to pack message: %v", err)
  48. //}
  49. //transportMessage := &workphone.TransportMessage{
  50. // MsgType: workphone.EnumMsgType_TalkToFriendTask,
  51. // Content: content,
  52. //}
  53. // 将 TransportMessage 转换为 JSON
  54. //transportMessageJSON, err := protojson.MarshalOptions{
  55. // UseProtoNames: true,
  56. //}.Marshal(transportMessage)
  57. //if err != nil {
  58. // return fmt.Errorf("failed to marshal transport message: %v", err)
  59. //}
  60. message := map[string]interface{}{
  61. "MsgType": "TalkToFriendTask",
  62. "Content": map[string]interface{}{
  63. "WeChatId": wxWxid,
  64. "FriendId": wxid,
  65. "ContentType": "Text",
  66. "Content": encodedString,
  67. },
  68. }
  69. transportMessageJSON, err := json.Marshal(message)
  70. if err != nil {
  71. return err
  72. }
  73. // 发送 JSON 消息
  74. err = conn.WriteMessage(websocket.TextMessage, transportMessageJSON)
  75. if err != nil {
  76. return fmt.Errorf("failed to send message: %v", err)
  77. }
  78. // 读取回复消息
  79. _, replyMessage, err := conn.ReadMessage()
  80. if err != nil {
  81. return fmt.Errorf("failed to read message: %v", err)
  82. }
  83. var replyMessageData map[string]interface{}
  84. err = json.Unmarshal([]byte(replyMessage), &replyMessageData)
  85. if err != nil {
  86. return fmt.Errorf("failed to send message: %v", err)
  87. }
  88. if replyMessageData["msgType"] == nil || replyMessageData["msgType"] != "MsgReceivedAck" {
  89. return fmt.Errorf("failed to send message: %v")
  90. }
  91. return nil
  92. } else {
  93. resp, err := h.Client.R().SetBody(&SendTextMsgReq{
  94. Wxid: wxid,
  95. Msg: msg,
  96. }).Post("http://" + h.ServerIp + ":" + h.WxPort + "/SendTextMsg")
  97. if err != nil {
  98. return err
  99. }
  100. if !resp.IsSuccessState() {
  101. err = fmt.Errorf("SendTextMsg failed with status code %d", resp.StatusCode)
  102. return err
  103. }
  104. return nil
  105. }
  106. }
  107. // 发送微信图片
  108. func (h *Hook) SendPicMsg(wxid, picpath, diyfilename, wxWxid string) (err error) {
  109. if h.ServerIp == "" {
  110. conn, err := h.connWorkPhone()
  111. if err != nil {
  112. err = fmt.Errorf("SendTextMsg failed")
  113. return err
  114. }
  115. defer func(conn *websocket.Conn) {
  116. err = conn.Close()
  117. if err != nil {
  118. err = fmt.Errorf("SendTextMsg failed")
  119. }
  120. }(conn)
  121. encodedString := base64.StdEncoding.EncodeToString([]byte(picpath))
  122. message := map[string]interface{}{
  123. "MsgType": "TalkToFriendTask",
  124. "Content": map[string]interface{}{
  125. "WeChatId": wxWxid,
  126. "FriendId": wxid,
  127. "ContentType": "File",
  128. "Content": encodedString,
  129. },
  130. }
  131. transportMessageJSON, err := json.Marshal(message)
  132. if err != nil {
  133. return err
  134. }
  135. // 发送 JSON 消息
  136. err = conn.WriteMessage(websocket.TextMessage, transportMessageJSON)
  137. if err != nil {
  138. return fmt.Errorf("failed to send message: %v", err)
  139. }
  140. // 读取回复消息
  141. _, replyMessage, err := conn.ReadMessage()
  142. if err != nil {
  143. return fmt.Errorf("failed to read message: %v", err)
  144. }
  145. var replyMessageData map[string]interface{}
  146. err = json.Unmarshal([]byte(replyMessage), &replyMessageData)
  147. if err != nil {
  148. return fmt.Errorf("failed to send message: %v", err)
  149. }
  150. if replyMessageData["msgType"] == nil || replyMessageData["msgType"] != workphone.EnumMsgType_name[int32(workphone.EnumMsgType_MsgReceivedAck)] {
  151. return fmt.Errorf("failed to send message: %v")
  152. }
  153. return nil
  154. } else {
  155. resp, err := h.Client.R().SetBody(&SendPicMsgReq{
  156. Wxid: wxid,
  157. Picpath: picpath,
  158. Diyfilename: diyfilename,
  159. }).Post("http://" + h.ServerIp + ":" + h.WxPort + "/SendPicMsg")
  160. if err != nil {
  161. return err
  162. }
  163. if !resp.IsSuccessState() {
  164. err = fmt.Errorf("SendPicMsg failed with status code %d", resp.StatusCode)
  165. return err
  166. }
  167. return nil
  168. }
  169. }
  170. // 发送微信图片(本地测试)
  171. func (h *Hook) SendPicMsgLocal(wxid, picpath string) (err error) {
  172. resp, err := h.Client.R().SetBody(&SendPicMsgLocalReq{
  173. Wxid: wxid,
  174. Picpath: picpath,
  175. }).Post("http://" + h.ServerIp + ":" + h.WxPort + "/SendPicMsg")
  176. if err != nil {
  177. return
  178. }
  179. if !resp.IsSuccessState() {
  180. err = fmt.Errorf("SendPicMsgLocal failed with status code %d", resp.StatusCode)
  181. return
  182. }
  183. return
  184. }