github.com/872409/go-netease-im@v1.0.2-0.20201109080841-fdb3e13691c5/message.go (about) 1 package netease 2 3 import ( 4 "encoding/json" 5 "errors" 6 "strconv" 7 8 jsoniter "github.com/json-iterator/go" 9 ) 10 11 const ( 12 sendMsgPoint = neteaseBaseURL + "/msg/sendMsg.action" 13 sendBatchMsgPoint = neteaseBaseURL + "/msg/sendBatchMsg.action" 14 sendBatchAttachMsgPoint = neteaseBaseURL + "/msg/sendBatchAttachMsg.action" 15 messageRecallPoint = neteaseBaseURL + "/msg/recall.action" 16 ) 17 18 const ( 19 // MsgTypeText 文本消息 20 MsgTypeText = iota 21 // MsgTypeImage 图片消息 22 MsgTypeImage 23 // MsgTypeVoice 语音消息 24 MsgTypeVoice 25 // MsgTypeVideo 视频消息 26 MsgTypeVideo 27 MsgTypeLocation 28 MsgTypeFile = 6 29 MsgTypeTip = 10 30 ) 31 32 // SendTextMessage 发送文本消息,消息内容最长5000 33 func (c *ImClient) SendTextMessage(fromID, toID string, msg *TextMessage, opt *ImSendMessageOption) error { 34 bd, err := jsonTool.MarshalToString(msg) 35 if err != nil { 36 return err 37 } 38 return c.SendMessage(fromID, toID, bd, 0, MsgTypeText, opt) 39 } 40 41 // SendTextMessage 发送文本消息,消息内容最长5000 42 func (c *ImClient) SendTipMessage(fromID, toID string, msg *TextMessage, opt *ImSendMessageOption) error { 43 bd, err := jsonTool.MarshalToString(msg) 44 if err != nil { 45 return err 46 } 47 return c.SendMessage(fromID, toID, bd, 0, MsgTypeTip, opt) 48 } 49 50 // SendBatchTextMessage 批量发送文本消息 51 func (c *ImClient) SendBatchTextMessage(fromID string, toIDs []string, msg *TextMessage, opt *ImSendMessageOption) (string, error) { 52 bd, err := jsonTool.MarshalToString(msg) 53 if err != nil { 54 return "", err 55 } 56 57 return c.SendBatchMessage(fromID, bd, toIDs, MsgTypeText, opt) 58 } 59 60 // SendBatchImageMessage 批量发送图片 61 func (c *ImClient) SendBatchImageMessage(fromID string, toIDs []string, msg *ImageMessage, opt *ImSendMessageOption) (string, error) { 62 bd, err := jsonTool.MarshalToString(msg) 63 if err != nil { 64 return "", err 65 } 66 67 return c.SendBatchMessage(fromID, bd, toIDs, MsgTypeImage, opt) 68 } 69 70 // SendBatchVoiceMessage . 71 func (c *ImClient) SendBatchVoiceMessage(fromID string, toIDs []string, msg *VoiceMessage, opt *ImSendMessageOption) (string, error) { 72 bd, err := jsonTool.MarshalToString(msg) 73 if err != nil { 74 return "", err 75 } 76 77 return c.SendBatchMessage(fromID, bd, toIDs, MsgTypeVoice, opt) 78 } 79 80 // SendBatchVideoMessage . 81 func (c *ImClient) SendBatchVideoMessage(fromID string, toIDs []string, msg *VideoMessage, opt *ImSendMessageOption) (string, error) { 82 bd, err := jsonTool.MarshalToString(msg) 83 if err != nil { 84 return "", err 85 } 86 87 return c.SendBatchMessage(fromID, bd, toIDs, MsgTypeVideo, opt) 88 } 89 90 // SendMessage 发送普通消息 91 /** 92 * @param fromID 发送者accid,用户帐号,最大32字符,必须保证一个APP内唯一 93 * @param toID ope==0是表示accid即用户id,ope==1表示tid即群id 94 * @param ope 0:点对点个人消息,1:群消息(高级群),其他返回414 95 * @param msgType 0 表示文本消息,1 表示图片,2 表示语音,3 表示视频,4 表示地理位置信息,6 表示文件,100 自定义消息类型(特别注意,对于未对接易盾反垃圾功能的应用,该类型的消息不会提交反垃圾系统检测) 96 * @param body 最大长度5000字符,为一个JSON串 97 */ 98 func (c *ImClient) SendMessage(fromID, toID, body string, ope, msgType int, opt *ImSendMessageOption) error { 99 param := map[string]string{"from": fromID} 100 101 param["ope"] = strconv.Itoa(ope) 102 param["to"] = toID 103 param["type"] = strconv.Itoa(msgType) 104 param["body"] = body 105 106 if opt != nil { 107 param["antispam"] = strconv.FormatBool(opt.Antispam) 108 109 if opt.AntispamCustom != nil { 110 param["antispamCustom"], _ = jsonTool.MarshalToString(opt.AntispamCustom) 111 } 112 113 if opt.Option != nil { 114 param["option"], _ = jsonTool.MarshalToString(opt.Option) 115 } 116 117 if len(opt.Pushcontent) > 0 { 118 param["pushcontent"] = opt.Pushcontent 119 } 120 121 if len(opt.Payload) > 0 { 122 param["payload"] = opt.Payload 123 } 124 125 if len(opt.Extension) > 0 { 126 param["ext"] = opt.Extension 127 } 128 129 if opt.ForcePushList != nil { 130 param["forcepushlist"], _ = jsonTool.MarshalToString(opt.ForcePushList) 131 } 132 133 if len(opt.ForcePushContent) > 0 { 134 param["forcepushcontent"] = opt.ForcePushContent 135 } 136 param["forcepushall"] = strconv.FormatBool(opt.ForcePushAll) 137 if len(opt.Bid) > 0 { 138 param["bid"] = opt.Bid 139 } 140 } 141 client := c.client.R() 142 c.setCommonHead(client) 143 client.SetFormData(param) 144 145 resp, err := client.Post(sendMsgPoint) 146 147 var jsonRes map[string]*json.RawMessage 148 err = jsoniter.Unmarshal(resp.Body(), &jsonRes) 149 if err != nil { 150 return err 151 } 152 153 var code int 154 err = json.Unmarshal(*jsonRes["code"], &code) 155 if err != nil { 156 return err 157 } 158 159 if code != 200 { 160 return errors.New(string(resp.Body())) 161 } 162 163 return nil 164 } 165 166 // SendBatchMessage 批量发送点对点普通消息 167 /** 168 * @param fromID 发送者accid,用户帐号,最大32字符,必须保证一个APP内唯一 169 * @param toIDs ["aaa","bbb"](JSONArray对应的accid,如果解析出错,会报414错误),限500人 170 * @param msgType 0 表示文本消息,1 表示图片,2 表示语音,3 表示视频,4 表示地理位置信息,6 表示文件,100 自定义消息类型 171 */ 172 func (c *ImClient) SendBatchMessage(fromID, body string, toIDs []string, msgType int, opt *ImSendMessageOption) (string, error) { 173 param := map[string]string{"fromAccid": fromID} 174 175 to, err := jsonTool.MarshalToString(toIDs) 176 if err != nil { 177 return "", err 178 } 179 param["toAccids"] = to 180 param["type"] = strconv.Itoa(msgType) 181 param["body"] = body 182 183 if opt != nil { 184 if opt.Option != nil { 185 param["option"], _ = jsonTool.MarshalToString(opt.Option) 186 } 187 188 if len(opt.ForcePushContent) > 0 { 189 param["forcepushcontent"] = opt.ForcePushContent 190 } 191 192 if len(opt.Payload) > 0 { 193 param["payload"] = opt.Payload 194 } 195 196 if len(opt.Extension) > 0 { 197 param["ext"] = opt.Extension 198 } 199 200 if len(opt.Bid) > 0 { 201 param["bid"] = opt.Bid 202 } 203 } 204 client := c.client.R() 205 c.setCommonHead(client) 206 client.SetFormData(param) 207 208 resp, err := client.Post(sendBatchMsgPoint) 209 210 var jsonRes map[string]*json.RawMessage 211 err = jsoniter.Unmarshal(resp.Body(), &jsonRes) 212 if err != nil { 213 return string(resp.Body()), err 214 } 215 216 var code int 217 err = json.Unmarshal(*jsonRes["code"], &code) 218 if err != nil { 219 return string(resp.Body()), err 220 } 221 222 if code != 200 { 223 return string(resp.Body()), errors.New("云信接口返回错误") 224 } 225 226 return string(resp.Body()), nil 227 } 228 229 // SendBatchAttachMsg 批量发送点对点自定义系统通知 230 /** 231 * @param fromID 发送者accid,用户帐号,最大32字符,必须保证一个APP内唯一 232 * @param toIDs ["aaa","bbb"](JSONArray对应的accid,如果解析出错,会报414错误),限500人 233 * @param attach 自定义通知内容,第三方组装的字符串,建议是JSON串,最大长度4096字符 234 */ 235 func (c *ImClient) SendBatchAttachMsg(fromID, attach string, toIDs []string, opt *ImSendAttachMessageOption) error { 236 param := map[string]string{"fromAccid": fromID} 237 238 to, err := jsonTool.MarshalToString(toIDs) 239 if err != nil { 240 return err 241 } 242 243 param["toAccids"] = to 244 param["attach"] = attach 245 if opt != nil { 246 if len(opt.Pushcontent) > 0 { 247 param["pushcontent"] = opt.Pushcontent 248 } 249 250 if len(opt.Payload) > 0 { 251 param["payload"] = opt.Payload 252 } 253 254 if len(opt.Sound) > 0 { 255 param["sound"] = opt.Payload 256 } 257 258 if opt.Save == 1 || opt.Save == 2 { 259 param["save"] = strconv.Itoa(opt.Save) 260 } 261 262 if opt.Option != nil { 263 param["option"], _ = jsonTool.MarshalToString(opt.Option) 264 } 265 } 266 267 client := c.client.R() 268 c.setCommonHead(client) 269 client.SetFormData(param) 270 271 resp, err := client.Post(sendBatchAttachMsgPoint) 272 273 var jsonRes map[string]*json.RawMessage 274 err = jsoniter.Unmarshal(resp.Body(), &jsonRes) 275 if err != nil { 276 return err 277 } 278 279 var code int 280 err = json.Unmarshal(*jsonRes["code"], &code) 281 if err != nil { 282 return err 283 } 284 285 if code != 200 { 286 return errors.New(string(resp.Body())) 287 } 288 289 return nil 290 } 291 292 // RecallMessage 消息撤回 293 /** 294 * @param deleteMsgid 要撤回消息的msgid 295 * @param timetag 要撤回消息的创建时间 296 * @param fromID 发消息的accid 297 * @param toID 如果点对点消息,为接收消息的accid,如果群消息,为对应群的tid 298 * @param msgtype 7:表示点对点消息撤回,8:表示群消息撤回,其它为参数错误 299 */ 300 func (c *ImClient) RecallMessage(deleteMsgid, timetag, fromID, toID string, msgtype int) error { 301 param := map[string]string{"from": fromID, "to": toID, "type": strconv.Itoa(msgtype), "timetag": timetag, "deleteMsgid": deleteMsgid, "msg": "."} 302 303 client := c.client.R() 304 c.setCommonHead(client) 305 client.SetFormData(param) 306 307 resp, err := client.Post(messageRecallPoint) 308 if err != nil { 309 return err 310 } 311 312 var jsonRes map[string]*json.RawMessage 313 jsonTool.Unmarshal(resp.Body(), &jsonRes) 314 315 var code int 316 err = json.Unmarshal(*jsonRes["code"], &code) 317 if err != nil { 318 return err 319 } 320 321 if code != 200 { 322 return errors.New(string(resp.Body())) 323 } 324 325 return nil 326 }