✨ 初始化项目
This commit is contained in:
354
private/api.go
Normal file
354
private/api.go
Normal file
@@ -0,0 +1,354 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/5/27 19:32
|
||||
* @Desc: 单聊消息
|
||||
*/
|
||||
|
||||
package private
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/tencent-im/internal/conv"
|
||||
"git.echol.cn/loser/tencent-im/internal/core"
|
||||
"git.echol.cn/loser/tencent-im/internal/types"
|
||||
)
|
||||
|
||||
const (
|
||||
service = "openim"
|
||||
commandSendMessage = "sendmsg"
|
||||
commandSendMessages = "batchsendmsg"
|
||||
commandImportMessage = "importmsg"
|
||||
commandFetchMessages = "admin_getroammsg"
|
||||
commandRevokeMessage = "admin_msgwithdraw"
|
||||
commandSetMessageRead = "admin_set_msg_read"
|
||||
commandGetUnreadMessageNum = "get_c2c_unread_msg_num"
|
||||
)
|
||||
|
||||
type API interface {
|
||||
// SendMessage 单发单聊消息
|
||||
// 管理员向帐号发消息,接收方看到消息发送者是管理员。
|
||||
// 管理员指定某一帐号向其他帐号发消息,接收方看到发送者不是管理员,而是管理员指定的帐号。
|
||||
// 该接口不会检查发送者和接收者的好友关系(包括黑名单),同时不会检查接收者是否被禁言。
|
||||
// 单聊消息 MsgSeq 字段的作用及说明:该字段在发送消息时由用户自行指定,该值可以重复,非后台生成,非全局唯一。与群聊消息的 MsgSeq 字段不同,群聊消息的 MsgSeq 由后台生成,每个群都维护一个 MsgSeq,从1开始严格递增。单聊消息历史记录对同一个会话的消息先以时间戳排序,同秒内的消息再以 MsgSeq 排序。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/2282
|
||||
SendMessage(message *Message) (ret *SendMessageRet, err error)
|
||||
|
||||
// SendMessages 批量发单聊消息
|
||||
// 支持一次对最多500个用户进行单发消息。
|
||||
// 与单发消息相比,该接口更适用于营销类消息、系统通知 tips 等时效性较强的消息。
|
||||
// 管理员指定某一帐号向目标帐号批量发消息,接收方看到发送者不是管理员,而是管理员指定的帐号。
|
||||
// 该接口不触发回调请求。
|
||||
// 该接口不会检查发送者和接收者的好友关系(包括黑名单),同时不会检查接收者是否被禁言。
|
||||
// 单聊消息 MsgSeq 字段的作用及说明:该字段在发送消息时由用户自行指定,该值可以重复,非后台生成,非全局唯一。与群聊消息的 MsgSeq 字段不同,群聊消息的 MsgSeq 由后台生成,每个群都维护一个 MsgSeq,从1开始严格递增。单聊消息历史记录对同一个会话的消息先以时间戳排序,同秒内的消息再以 MsgSeq 排序。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1612
|
||||
SendMessages(message *Message) (ret *SendMessagesRet, err error)
|
||||
|
||||
// ImportMessage 导入单聊消息
|
||||
// 导入历史单聊消息到即时通信 IM。
|
||||
// 平滑过渡期间,将原有即时通信实时单聊消息导入到即时通信 IM。
|
||||
// 该接口不会触发回调。
|
||||
// 该接口会根据 From_Account , To_Account ,MsgSeq , MsgRandom , MsgTimeStamp 字段的值对导入的消息进行去重。仅当这五个字段的值都对应相同时,才判定消息是重复的,消息是否重复与消息内容本身无关。
|
||||
// 重复导入的消息不会覆盖之前已导入的消息(即消息内容以首次导入的为准)。
|
||||
// 单聊消息 MsgSeq 字段的作用及说明:该字段在发送消息时由用户自行指定,该值可以重复,非后台生成,非全局唯一。与群聊消息的 MsgSeq 字段不同,群聊消息的 MsgSeq 由后台生成,每个群都维护一个 MsgSeq,从1开始严格递增。单聊消息历史记录对同一个会话的消息先以时间戳排序,同秒内的消息再以 MsgSeq 排序。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/2568
|
||||
ImportMessage(message *Message) (err error)
|
||||
|
||||
// FetchMessages 查询单聊消息
|
||||
// 管理员按照时间范围查询某单聊会话的消息记录。
|
||||
// 查询的单聊会话由请求中的 From_Account 和 To_Account 指定。查询结果包含会话双方互相发送的消息,具体每条消息的发送方和接收方由每条消息里的 From_Account 和 To_Account 指定。
|
||||
// 一般情况下,请求中的 From_Account 和 To_Account 字段值互换,查询结果不变。但通过 单发单聊消息 或 批量发单聊消息 接口发送的消息,如果指定 SyncOtherMachine 值为2,则需要指定正确的 From_Account 和 To_Account 字段值才能查询到该消息。
|
||||
// 例如,通过 单发单聊消息 接口指定帐号 A 给帐号 B 发一条消息,同时指定 SyncOtherMachine 值为2。则调用本接口时,From_Account 必须设置为帐号 B,To_Account 必须设置为帐号 A 才能查询到该消息。
|
||||
// 查询结果包含被撤回的消息,由消息里的 MsgFlagBits 字段标识。
|
||||
// 若想通过 REST API 撤回单聊消息 接口撤回某条消息,可先用本接口查询出该消息的 MsgKey,然后再调用撤回接口进行撤回。
|
||||
// 可查询的消息记录的时间范围取决于漫游消息存储时长,默认是7天。支持在控制台修改消息漫游时长,延长消息漫游时长是增值服务。具体请参考 漫游消息存储。
|
||||
// 若请求时间段内的消息总大小超过应答包体大小限制(目前为13K)时,则需要续拉。您可以通过应答中的 Complete 字段查看是否已拉取请求的全部消息。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/42794
|
||||
FetchMessages(arg *FetchMessagesArg) (ret *FetchMessagesRet, err error)
|
||||
|
||||
// PullMessages 续拉取单聊消息
|
||||
// 本API是借助"查询单聊消息"API进行扩展实现
|
||||
// 管理员按照时间范围查询某单聊会话的全部消息记录
|
||||
// 查询的单聊会话由请求中的 From_Account 和 To_Account 指定。查询结果包含会话双方互相发送的消息,具体每条消息的发送方和接收方由每条消息里的 From_Account 和 To_Account 指定。
|
||||
// 一般情况下,请求中的 From_Account 和 To_Account 字段值互换,查询结果不变。但通过 单发单聊消息 或 批量发单聊消息 接口发送的消息,如果指定 SyncOtherMachine 值为2,则需要指定正确的 From_Account 和 To_Account 字段值才能查询到该消息。
|
||||
// 例如,通过 单发单聊消息 接口指定帐号 A 给帐号 B 发一条消息,同时指定 SyncOtherMachine 值为2。则调用本接口时,From_Account 必须设置为帐号 B,To_Account 必须设置为帐号 A 才能查询到该消息。
|
||||
// 查询结果包含被撤回的消息,由消息里的 MsgFlagBits 字段标识。
|
||||
// 若想通过 REST API 撤回单聊消息 接口撤回某条消息,可先用本接口查询出该消息的 MsgKey,然后再调用撤回接口进行撤回。
|
||||
// 可查询的消息记录的时间范围取决于漫游消息存储时长,默认是7天。支持在控制台修改消息漫游时长,延长消息漫游时长是增值服务。具体请参考 漫游消息存储。
|
||||
// 若请求时间段内的消息总大小超过应答包体大小限制(目前为13K)时,则需要续拉。您可以通过应答中的 Complete 字段查看是否已拉取请求的全部消息。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/42794
|
||||
PullMessages(arg *PullMessagesArg, fn func(ret *FetchMessagesRet)) (err error)
|
||||
|
||||
// RevokeMessage 撤回单聊消息
|
||||
// 管理员撤回单聊消息。
|
||||
// 该接口可以撤回所有单聊消息,包括客户端发出的单聊消息,由 REST API 单发 和 批量发 接口发出的单聊消息。
|
||||
// 若需要撤回由客户端发出的单聊消息,您可以开通 发单聊消息之前回调 或 发单聊消息之后回调 ,通过该回调接口记录每条单聊消息的 MsgKey ,然后填在本接口的 MsgKey 字段进行撤回。您也可以通过 查询单聊消息 查询出待撤回的单聊消息的 MsgKey 后,填在本接口的 MsgKey 字段进行撤回。
|
||||
// 若需要撤回由 REST API 单发 和 批量发 接口发出的单聊消息,需要记录这些接口回包里的 MsgKey 字段以进行撤回。
|
||||
// 调用该接口撤回消息后,该条消息的离线、漫游存储,以及消息发送方和接收方的客户端的本地缓存都会被撤回。
|
||||
// 该接口可撤回的单聊消息没有时间限制,即可以撤回任何时间的单聊消息。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/38980
|
||||
RevokeMessage(fromUserId, toUserId, msgKey string) (err error)
|
||||
|
||||
// SetMessageRead 设置单聊消息已读
|
||||
// 设置用户的某个单聊会话的消息全部已读。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/50349
|
||||
SetMessageRead(userId, peerUserId string) (err error)
|
||||
|
||||
// GetUnreadMessageNum 查询单聊未读消息计数
|
||||
// App 后台可以通过该接口查询特定账号的单聊总未读数(包含所有的单聊会话)或者单个单聊会话的未读数。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/56043
|
||||
GetUnreadMessageNum(userId string, peerUserIds ...string) (ret *GetUnreadMessageNumRet, err error)
|
||||
}
|
||||
|
||||
type api struct {
|
||||
client core.Client
|
||||
}
|
||||
|
||||
func NewAPI(client core.Client) API {
|
||||
return &api{client: client}
|
||||
}
|
||||
|
||||
// SendMessage 单发单聊消息
|
||||
// 管理员向帐号发消息,接收方看到消息发送者是管理员。
|
||||
// 管理员指定某一帐号向其他帐号发消息,接收方看到发送者不是管理员,而是管理员指定的帐号。
|
||||
// 该接口不会检查发送者和接收者的好友关系(包括黑名单),同时不会检查接收者是否被禁言。
|
||||
// 单聊消息 MsgSeq 字段的作用及说明:该字段在发送消息时由用户自行指定,该值可以重复,非后台生成,非全局唯一。与群聊消息的 MsgSeq 字段不同,群聊消息的 MsgSeq 由后台生成,每个群都维护一个 MsgSeq,从1开始严格递增。单聊消息历史记录对同一个会话的消息先以时间戳排序,同秒内的消息再以 MsgSeq 排序。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/2282
|
||||
func (a *api) SendMessage(message *Message) (ret *SendMessageRet, err error) {
|
||||
if err = message.CheckError(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
req := &sendMessageReq{}
|
||||
req.FromUserId = message.GetSender()
|
||||
req.ToUserId = message.GetLastReceiver()
|
||||
req.MsgLifeTime = message.GetLifeTime()
|
||||
req.MsgTimeStamp = message.GetTimestamp()
|
||||
req.OfflinePushInfo = message.GetOfflinePushInfo()
|
||||
req.CloudCustomData = conv.String(message.GetCustomData())
|
||||
req.MsgSeq = message.GetSerialNo()
|
||||
req.MsgBody = message.GetBody()
|
||||
req.MsgRandom = message.GetRandom()
|
||||
req.SendMsgControl = message.GetSendMsgControl()
|
||||
req.ForbidCallbackControl = message.GetForbidCallbackControl()
|
||||
req.SyncOtherMachine = message.GetSyncOtherMachine()
|
||||
|
||||
resp := &sendMessageResp{}
|
||||
|
||||
if err = a.client.Post(service, commandSendMessage, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ret = &SendMessageRet{
|
||||
MsgKey: resp.MsgKey,
|
||||
MsgTime: resp.MsgTime,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SendMessages 批量发单聊消息
|
||||
// 支持一次对最多500个用户进行单发消息。
|
||||
// 与单发消息相比,该接口更适用于营销类消息、系统通知 tips 等时效性较强的消息。
|
||||
// 管理员指定某一帐号向目标帐号批量发消息,接收方看到发送者不是管理员,而是管理员指定的帐号。
|
||||
// 该接口不触发回调请求。
|
||||
// 该接口不会检查发送者和接收者的好友关系(包括黑名单),同时不会检查接收者是否被禁言。
|
||||
// 单聊消息 MsgSeq 字段的作用及说明:该字段在发送消息时由用户自行指定,该值可以重复,非后台生成,非全局唯一。与群聊消息的 MsgSeq 字段不同,群聊消息的 MsgSeq 由后台生成,每个群都维护一个 MsgSeq,从1开始严格递增。单聊消息历史记录对同一个会话的消息先以时间戳排序,同秒内的消息再以 MsgSeq 排序。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/1612
|
||||
func (a *api) SendMessages(message *Message) (ret *SendMessagesRet, err error) {
|
||||
if err = message.CheckError(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
req := &sendMessagesReq{}
|
||||
req.FromUserId = message.GetSender()
|
||||
req.ToUserIds = message.GetReceivers()
|
||||
req.OfflinePushInfo = message.GetOfflinePushInfo()
|
||||
req.CloudCustomData = conv.String(message.GetCustomData())
|
||||
req.MsgSeq = message.GetSerialNo()
|
||||
req.MsgBody = message.GetBody()
|
||||
req.MsgRandom = message.GetRandom()
|
||||
req.SendMsgControl = message.GetSendMsgControl()
|
||||
req.SyncOtherMachine = message.GetSyncOtherMachine()
|
||||
|
||||
resp := &sendMessagesResp{}
|
||||
|
||||
if err = a.client.Post(service, commandSendMessages, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ret = &SendMessagesRet{
|
||||
MsgKey: resp.MsgKey,
|
||||
Errors: resp.Errors,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// ImportMessage 导入单聊消息
|
||||
// 导入历史单聊消息到即时通信 IM。
|
||||
// 平滑过渡期间,将原有即时通信实时单聊消息导入到即时通信 IM。
|
||||
// 该接口不会触发回调。
|
||||
// 该接口会根据 From_Account , To_Account ,MsgSeq , MsgRandom , MsgTimeStamp 字段的值对导入的消息进行去重。仅当这五个字段的值都对应相同时,才判定消息是重复的,消息是否重复与消息内容本身无关。
|
||||
// 重复导入的消息不会覆盖之前已导入的消息(即消息内容以首次导入的为准)。
|
||||
// 单聊消息 MsgSeq 字段的作用及说明:该字段在发送消息时由用户自行指定,该值可以重复,非后台生成,非全局唯一。与群聊消息的 MsgSeq 字段不同,群聊消息的 MsgSeq 由后台生成,每个群都维护一个 MsgSeq,从1开始严格递增。单聊消息历史记录对同一个会话的消息先以时间戳排序,同秒内的消息再以 MsgSeq 排序。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/2568
|
||||
func (a *api) ImportMessage(message *Message) (err error) {
|
||||
if err = message.CheckError(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
req := &importMessageReq{}
|
||||
req.FromUserId = message.GetSender()
|
||||
req.ToUserId = message.GetLastReceiver()
|
||||
req.MsgTimeStamp = message.GetTimestamp()
|
||||
req.CloudCustomData = conv.String(message.GetCustomData())
|
||||
req.MsgSeq = message.GetSerialNo()
|
||||
req.MsgBody = message.GetBody()
|
||||
req.MsgRandom = message.GetRandom()
|
||||
req.SyncFromOldSystem = message.GetSyncOtherMachine()
|
||||
|
||||
if err = a.client.Post(service, commandImportMessage, req, &types.ActionBaseResp{}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FetchMessages 查询单聊消息
|
||||
// 管理员按照时间范围查询某单聊会话的消息记录。
|
||||
// 查询的单聊会话由请求中的 From_Account 和 To_Account 指定。查询结果包含会话双方互相发送的消息,具体每条消息的发送方和接收方由每条消息里的 From_Account 和 To_Account 指定。
|
||||
// 一般情况下,请求中的 From_Account 和 To_Account 字段值互换,查询结果不变。但通过 单发单聊消息 或 批量发单聊消息 接口发送的消息,如果指定 SyncOtherMachine 值为2,则需要指定正确的 From_Account 和 To_Account 字段值才能查询到该消息。
|
||||
// 例如,通过 单发单聊消息 接口指定帐号 A 给帐号 B 发一条消息,同时指定 SyncOtherMachine 值为2。则调用本接口时,From_Account 必须设置为帐号 B,To_Account 必须设置为帐号 A 才能查询到该消息。
|
||||
// 查询结果包含被撤回的消息,由消息里的 MsgFlagBits 字段标识。
|
||||
// 若想通过 REST API 撤回单聊消息 接口撤回某条消息,可先用本接口查询出该消息的 MsgKey,然后再调用撤回接口进行撤回。
|
||||
// 可查询的消息记录的时间范围取决于漫游消息存储时长,默认是7天。支持在控制台修改消息漫游时长,延长消息漫游时长是增值服务。具体请参考 漫游消息存储。
|
||||
// 若请求时间段内的消息总大小超过应答包体大小限制(目前为13K)时,则需要续拉。您可以通过应答中的 Complete 字段查看是否已拉取请求的全部消息。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/42794
|
||||
func (a *api) FetchMessages(arg *FetchMessagesArg) (ret *FetchMessagesRet, err error) {
|
||||
resp := &fetchMessagesResp{}
|
||||
|
||||
if err = a.client.Post(service, commandFetchMessages, arg, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ret = &FetchMessagesRet{
|
||||
LastMsgKey: resp.LastMsgKey,
|
||||
LastMsgTime: resp.LastMsgTime,
|
||||
Count: resp.MsgCount,
|
||||
List: resp.MsgList,
|
||||
HasMore: resp.Complete != 1,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// PullMessages 续拉取单聊消息
|
||||
// 本API是借助"查询单聊消息"API进行扩展实现
|
||||
// 管理员按照时间范围查询某单聊会话的全部消息记录
|
||||
// 查询的单聊会话由请求中的 From_Account 和 To_Account 指定。查询结果包含会话双方互相发送的消息,具体每条消息的发送方和接收方由每条消息里的 From_Account 和 To_Account 指定。
|
||||
// 一般情况下,请求中的 From_Account 和 To_Account 字段值互换,查询结果不变。但通过 单发单聊消息 或 批量发单聊消息 接口发送的消息,如果指定 SyncOtherMachine 值为2,则需要指定正确的 From_Account 和 To_Account 字段值才能查询到该消息。
|
||||
// 例如,通过 单发单聊消息 接口指定帐号 A 给帐号 B 发一条消息,同时指定 SyncOtherMachine 值为2。则调用本接口时,From_Account 必须设置为帐号 B,To_Account 必须设置为帐号 A 才能查询到该消息。
|
||||
// 查询结果包含被撤回的消息,由消息里的 MsgFlagBits 字段标识。
|
||||
// 若想通过 REST API 撤回单聊消息 接口撤回某条消息,可先用本接口查询出该消息的 MsgKey,然后再调用撤回接口进行撤回。
|
||||
// 可查询的消息记录的时间范围取决于漫游消息存储时长,默认是7天。支持在控制台修改消息漫游时长,延长消息漫游时长是增值服务。具体请参考 漫游消息存储。
|
||||
// 若请求时间段内的消息总大小超过应答包体大小限制(目前为13K)时,则需要续拉。您可以通过应答中的 Complete 字段查看是否已拉取请求的全部消息。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/42794
|
||||
func (a *api) PullMessages(arg *PullMessagesArg, fn func(ret *FetchMessagesRet)) (err error) {
|
||||
var (
|
||||
ret *FetchMessagesRet
|
||||
req = &FetchMessagesArg{
|
||||
FromUserId: arg.FromUserId,
|
||||
ToUserId: arg.ToUserId,
|
||||
MaxLimited: arg.MaxLimited,
|
||||
MinTime: arg.MinTime,
|
||||
MaxTime: arg.MaxTime,
|
||||
}
|
||||
)
|
||||
|
||||
for ret == nil || ret.HasMore {
|
||||
ret, err = a.FetchMessages(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fn(ret)
|
||||
|
||||
if ret.HasMore {
|
||||
req.LastMsgKey = ret.LastMsgKey
|
||||
req.MaxTime = ret.LastMsgTime
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// RevokeMessage 撤回单聊消息
|
||||
// 管理员撤回单聊消息。
|
||||
// 该接口可以撤回所有单聊消息,包括客户端发出的单聊消息,由 REST API 单发 和 批量发 接口发出的单聊消息。
|
||||
// 若需要撤回由客户端发出的单聊消息,您可以开通 发单聊消息之前回调 或 发单聊消息之后回调 ,通过该回调接口记录每条单聊消息的 MsgKey ,然后填在本接口的 MsgKey 字段进行撤回。您也可以通过 查询单聊消息 查询出待撤回的单聊消息的 MsgKey 后,填在本接口的 MsgKey 字段进行撤回。
|
||||
// 若需要撤回由 REST API 单发 和 批量发 接口发出的单聊消息,需要记录这些接口回包里的 MsgKey 字段以进行撤回。
|
||||
// 调用该接口撤回消息后,该条消息的离线、漫游存储,以及消息发送方和接收方的客户端的本地缓存都会被撤回。
|
||||
// 该接口可撤回的单聊消息没有时间限制,即可以撤回任何时间的单聊消息。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/38980
|
||||
func (a *api) RevokeMessage(fromUserId, toUserId, msgKey string) (err error) {
|
||||
req := &revokeMessageReq{FromUserId: fromUserId, ToUserId: toUserId, MsgKey: msgKey}
|
||||
|
||||
if err = a.client.Post(service, commandRevokeMessage, req, &types.ActionBaseResp{}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetMessageRead 设置单聊消息已读
|
||||
// 设置用户的某个单聊会话的消息全部已读。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/50349
|
||||
func (a *api) SetMessageRead(userId, peerUserId string) (err error) {
|
||||
req := &setMessageReadReq{UserId: userId, PeerUserId: peerUserId}
|
||||
|
||||
if err = a.client.Post(service, commandSetMessageRead, req, &types.ActionBaseResp{}); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetUnreadMessageNum 查询单聊未读消息计数
|
||||
// App 后台可以通过该接口查询特定账号的单聊总未读数(包含所有的单聊会话)或者单个单聊会话的未读数。
|
||||
// 点击查看详细文档:
|
||||
// https://cloud.tencent.com/document/product/269/56043
|
||||
func (a *api) GetUnreadMessageNum(userId string, peerUserIds ...string) (ret *GetUnreadMessageNumRet, err error) {
|
||||
req := &getUnreadMessageNumReq{UserId: userId, PeerUserIds: peerUserIds}
|
||||
resp := &getUnreadMessageNumResp{}
|
||||
|
||||
if err = a.client.Post(service, commandGetUnreadMessageNum, req, resp); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ret = &GetUnreadMessageNumRet{
|
||||
Total: resp.AllUnreadMsgNum,
|
||||
Results: make(map[string]int, len(resp.PeerUnreadMsgNums)),
|
||||
Errors: resp.PeerErrors,
|
||||
}
|
||||
|
||||
for _, item := range resp.PeerUnreadMsgNums {
|
||||
ret.Results[item.UserId] = item.UnreadMsgNum
|
||||
}
|
||||
|
||||
return
|
||||
}
|
42
private/enum.go
Normal file
42
private/enum.go
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/9/7 10:22
|
||||
* @Desc: TODO
|
||||
*/
|
||||
|
||||
package private
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/tencent-im/internal/enum"
|
||||
)
|
||||
|
||||
const (
|
||||
// 同步至其他设备
|
||||
SyncOtherMachineYes = enum.SyncOtherMachineYes // 把消息同步到From_Account在线终端和漫游上
|
||||
SyncOtherMachineNo = enum.SyncOtherMachineNo // 消息不同步至From_Account
|
||||
|
||||
// 推送标识
|
||||
PushFlagYes = enum.PushFlagYes // 正常推送
|
||||
PushFlagNo = enum.PushFlagYes // 不离线推送
|
||||
|
||||
// 华为推送通知消息分类
|
||||
HuaWeiImportanceLow = enum.HuaWeiImportanceLow // LOW类消息
|
||||
HuaWeiImportanceNormal = enum.HuaWeiImportanceNormal // NORMAL类消息
|
||||
|
||||
// 华为推送为“打开应用内指定页面”的前提下透传参数行为
|
||||
HuaweiIntentParamAction = enum.HuaweiIntentParamAction // 将透传内容Ext作为Action参数
|
||||
HuaweiIntentParamIntent = enum.HuaweiIntentParamIntent // 将透传内容Ext作为Intent参数
|
||||
|
||||
// VIVO手机推送消息分类
|
||||
VivoClassificationOperation = enum.VivoClassificationOperation // 运营类消息
|
||||
VivoClassificationSystem = enum.VivoClassificationSystem // 系统类消息
|
||||
|
||||
// IOS徽章计数模式
|
||||
BadgeModeNormal = enum.BadgeModeNormal // 本条消息需要计数
|
||||
BadgeModeIgnore = enum.BadgeModeIgnore // 本条消息不需要计数
|
||||
|
||||
// IOS10的推送扩展开关
|
||||
MutableContentNormal = enum.MutableContentNormal // 关闭iOS10的推送扩展
|
||||
MutableContentEnable = enum.MutableContentEnable // 开启iOS10的推送扩展
|
||||
)
|
183
private/message.go
Normal file
183
private/message.go
Normal file
@@ -0,0 +1,183 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/8/31 18:04
|
||||
* @Desc: 私聊消息实体
|
||||
*/
|
||||
|
||||
package private
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.echol.cn/loser/tencent-im/internal/entity"
|
||||
"git.echol.cn/loser/tencent-im/internal/types"
|
||||
)
|
||||
|
||||
var errNotSetMsgReceiver = errors.New("message receiver is not set")
|
||||
|
||||
type Message struct {
|
||||
entity.Message
|
||||
receivers []string // 接收方UserId(可以为多个)
|
||||
syncOtherMachine int // 同步到其他器
|
||||
timestamp int64 // 消息时间戳,UNIX 时间戳(单位:秒)
|
||||
seq int // 消息序列号
|
||||
customData interface{} // 自定义数据
|
||||
sendControls map[string]bool // 发送消息控制
|
||||
callbackControls map[string]bool // 禁用回调
|
||||
}
|
||||
|
||||
func NewMessage() *Message {
|
||||
return &Message{}
|
||||
}
|
||||
|
||||
// AddReceivers 添加接收方
|
||||
func (m *Message) AddReceivers(userId ...string) {
|
||||
if m.receivers == nil {
|
||||
m.receivers = make([]string, 0)
|
||||
}
|
||||
m.receivers = append(m.receivers, userId...)
|
||||
}
|
||||
|
||||
// SetReceivers 设置接收方
|
||||
func (m *Message) SetReceivers(userId ...string) {
|
||||
if m.receivers != nil {
|
||||
m.receivers = m.receivers[0:0]
|
||||
}
|
||||
m.AddReceivers(userId...)
|
||||
}
|
||||
|
||||
// GetReceivers 获取接收方
|
||||
func (m *Message) GetReceivers() []string {
|
||||
return m.receivers
|
||||
}
|
||||
|
||||
func (m *Message) GetLastReceiver() string {
|
||||
return m.receivers[0]
|
||||
}
|
||||
|
||||
// SetSyncOtherMachine 设置同步到其他机器
|
||||
func (m *Message) SetSyncOtherMachine(syncOtherMachine types.SyncOtherMachine) {
|
||||
m.syncOtherMachine = int(syncOtherMachine)
|
||||
}
|
||||
|
||||
// GetSyncOtherMachine 获取同步至其他设备
|
||||
func (m *Message) GetSyncOtherMachine() int {
|
||||
return m.syncOtherMachine
|
||||
}
|
||||
|
||||
// SetSerialNo 设置消息序列号
|
||||
func (m *Message) SetSerialNo(seq int) {
|
||||
m.seq = seq
|
||||
}
|
||||
|
||||
// GetSerialNo 获取消息序列号
|
||||
func (m *Message) GetSerialNo() int {
|
||||
return m.seq
|
||||
}
|
||||
|
||||
// SetTimestamp 设置消息的时间戳
|
||||
func (m *Message) SetTimestamp(timestamp int64) {
|
||||
m.timestamp = timestamp
|
||||
}
|
||||
|
||||
// GetTimestamp 获取消息的时间戳
|
||||
func (m *Message) GetTimestamp() int64 {
|
||||
return m.timestamp
|
||||
}
|
||||
|
||||
// SetCustomData 设置自定义数据
|
||||
func (m *Message) SetCustomData(data interface{}) {
|
||||
m.customData = data
|
||||
}
|
||||
|
||||
// GetCustomData 获取自定义数据
|
||||
func (m *Message) GetCustomData() interface{} {
|
||||
return m.customData
|
||||
}
|
||||
|
||||
// SetForbidBeforeSendMsgCallback 设置禁止发消息前回调
|
||||
func (m *Message) SetForbidBeforeSendMsgCallback() {
|
||||
if m.callbackControls == nil {
|
||||
m.callbackControls = make(map[string]bool, 0)
|
||||
}
|
||||
m.callbackControls["ForbidBeforeSendMsgCallback"] = true
|
||||
}
|
||||
|
||||
// SetForbidAfterSendMsgCallback 设置禁止发消息后回调
|
||||
func (m *Message) SetForbidAfterSendMsgCallback() {
|
||||
if m.callbackControls == nil {
|
||||
m.callbackControls = make(map[string]bool, 0)
|
||||
}
|
||||
m.callbackControls["ForbidAfterSendMsgCallback"] = true
|
||||
}
|
||||
|
||||
// GetForbidCallbackControl 获取消息回调禁止开关
|
||||
func (m *Message) GetForbidCallbackControl() (controls []string) {
|
||||
if m.callbackControls != nil {
|
||||
if n := len(m.callbackControls); n > 0 {
|
||||
controls = make([]string, 0, n)
|
||||
for k := range m.callbackControls {
|
||||
controls = append(controls, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SetNoUnread 设置该条消息不计入未读数
|
||||
func (m *Message) SetNoUnread() {
|
||||
if m.sendControls == nil {
|
||||
m.sendControls = make(map[string]bool, 0)
|
||||
}
|
||||
m.sendControls["NoUnread"] = true
|
||||
}
|
||||
|
||||
// SetNoLastMsg 设置该条消息不更新会话列表
|
||||
func (m *Message) SetNoLastMsg() {
|
||||
if m.sendControls == nil {
|
||||
m.sendControls = make(map[string]bool, 0)
|
||||
}
|
||||
m.sendControls["NoLastMsg"] = true
|
||||
}
|
||||
|
||||
// GetSendMsgControl 获取消息发送控制选项
|
||||
func (m *Message) GetSendMsgControl() (controls []string) {
|
||||
if m.sendControls != nil {
|
||||
if n := len(m.sendControls); n > 0 {
|
||||
controls = make([]string, 0, n)
|
||||
for k := range m.sendControls {
|
||||
controls = append(controls, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// CheckError 检测错误
|
||||
func (m *Message) CheckError() (err error) {
|
||||
if err = m.CheckLifeTimeArgError(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = m.CheckBodyArgError(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err = m.checkReceiverArgError(); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// checkReceiverArgError 检测接收方参数
|
||||
func (m *Message) checkReceiverArgError() error {
|
||||
if m.receivers == nil || len(m.receivers) == 0 {
|
||||
return errNotSetMsgReceiver
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
192
private/types.go
Normal file
192
private/types.go
Normal file
@@ -0,0 +1,192 @@
|
||||
/**
|
||||
* @Author: Echo
|
||||
* @Author:1711788888@qq.com
|
||||
* @Date: 2021/5/29 17:42
|
||||
* @Desc: 私聊消息数据结构
|
||||
*/
|
||||
|
||||
package private
|
||||
|
||||
import "git.echol.cn/loser/tencent-im/internal/types"
|
||||
|
||||
type (
|
||||
// 发送消息(请求)
|
||||
sendMessageReq struct {
|
||||
FromUserId string `json:"From_Account,omitempty"` // (选填)消息发送方UserID(用于指定发送消息方帐号)
|
||||
ToUserId string `json:"To_Account"` // (必填)消息接收方UserID
|
||||
MsgLifeTime int `json:"MsgLifeTime,omitempty"` // (选填)消息离线保存时长(单位:秒),最长为7天(604800秒)
|
||||
MsgSeq int `json:"MsgSeq,omitempty"` // (选填)消息序列号,后台会根据该字段去重及进行同秒内消息的排序,详细规则请看本接口的功能说明。若不填该字段,则由后台填入随机数。
|
||||
MsgRandom uint32 `json:"MsgRandom"` // (必填)消息随机数,后台用于同一秒内的消息去重。请确保该字段填的是随机数
|
||||
MsgTimeStamp int64 `json:"MsgTimeStamp,omitempty"` // (选填)消息时间戳,UNIX 时间戳(单位:秒)
|
||||
MsgBody []*types.MsgBody `json:"MsgBody"` // (必填)消息内容,具体格式请参考 消息格式描述(注意,一条消息可包括多种消息元素,MsgBody 为 Array 类型)
|
||||
SyncOtherMachine int `json:"SyncOtherMachine,omitempty"` // (选填)消息是否同步到在线终端和漫游上 1:把消息同步到 From_Account 在线终端和漫游上;2:消息不同步至 From_Account; 若不填写默认情况下会将消息存 From_Account 漫游
|
||||
CloudCustomData string `json:"CloudCustomData,omitempty"` // (选填)消息回调禁止开关,只对本条消息有效,
|
||||
SendMsgControl []string `json:"SendMsgControl,omitempty"` // (选填)消息发送控制选项,是一个 String 数组,只对本条消息有效。
|
||||
ForbidCallbackControl []string `json:"ForbidCallbackControl,omitempty"` // (选填)消息回调禁止开关,只对本条消息有效
|
||||
OfflinePushInfo *types.OfflinePushInfo `json:"OfflinePushInfo,omitempty"` // (选填)离线推送信息配置
|
||||
}
|
||||
|
||||
// 发送消息(响应)
|
||||
sendMessageResp struct {
|
||||
types.ActionBaseResp
|
||||
MsgTime int `json:"MsgTime"` // 消息时间戳,UNIX 时间戳
|
||||
MsgKey string `json:"MsgKey"` // 消息唯一标识,用于撤回。长度不超过50个字符
|
||||
}
|
||||
|
||||
// SendMessageRet 发送消息结果
|
||||
SendMessageRet struct {
|
||||
MsgKey string // 消息唯一标识,用于撤回。长度不超过50个字符
|
||||
MsgTime int // 消息时间戳,UNIX 时间戳
|
||||
}
|
||||
|
||||
// 批量发单聊消息(请求)
|
||||
sendMessagesReq struct {
|
||||
FromUserId string `json:"From_Account,omitempty"` // (选填)消息发送方UserID(用于指定发送消息方帐号)
|
||||
ToUserIds []string `json:"To_Account"` // (必填)消息接收方UserID
|
||||
MsgSeq int `json:"MsgSeq,omitempty"` // (选填)消息序列号,后台会根据该字段去重及进行同秒内消息的排序,详细规则请看本接口的功能说明。若不填该字段,则由后台填入随机数。
|
||||
MsgRandom uint32 `json:"MsgRandom"` // (必填)消息随机数,后台用于同一秒内的消息去重。请确保该字段填的是随机数
|
||||
MsgBody []*types.MsgBody `json:"MsgBody"` // (必填)消息内容,具体格式请参考 消息格式描述(注意,一条消息可包括多种消息元素,MsgBody 为 Array 类型)
|
||||
SyncOtherMachine int `json:"SyncOtherMachine,omitempty"` // (选填)消息是否同步到在线终端和漫游上 1:把消息同步到 From_Account 在线终端和漫游上;2:消息不同步至 From_Account; 若不填写默认情况下会将消息存 From_Account 漫游
|
||||
CloudCustomData string `json:"CloudCustomData,omitempty"` // (选填)消息回调禁止开关,只对本条消息有效,
|
||||
SendMsgControl []string `json:"SendMsgControl,omitempty"` // (选填)消息发送控制选项,是一个 String 数组,只对本条消息有效。
|
||||
OfflinePushInfo *types.OfflinePushInfo `json:"OfflinePushInfo,omitempty"` // (选填)离线推送信息配置
|
||||
}
|
||||
|
||||
// 批量发单聊消息(响应)
|
||||
sendMessagesResp struct {
|
||||
types.ActionBaseResp
|
||||
MsgKey string `json:"MsgKey"`
|
||||
Errors []SendMessageError `json:"ErrorList"`
|
||||
}
|
||||
|
||||
// SendMessageError 发送消息错误项
|
||||
SendMessageError struct {
|
||||
UserId string `json:"To_Account"`
|
||||
ErrorCode int `json:"ErrorCode"`
|
||||
}
|
||||
|
||||
// SendMessagesRet 发送消息结果
|
||||
SendMessagesRet struct {
|
||||
MsgKey string
|
||||
Errors []SendMessageError
|
||||
}
|
||||
|
||||
// 导入消息(请求)
|
||||
importMessageReq struct {
|
||||
FromUserId string `json:"From_Account,omitempty"` // (选填)消息发送方UserID(用于指定发送消息方帐号)
|
||||
ToUserId string `json:"To_Account"` // (必填)消息接收方UserID
|
||||
MsgSeq int `json:"MsgSeq,omitempty"` // (选填)消息序列号,后台会根据该字段去重及进行同秒内消息的排序,详细规则请看本接口的功能说明。若不填该字段,则由后台填入随机数。
|
||||
MsgRandom uint32 `json:"MsgRandom"` // (必填)消息随机数,后台用于同一秒内的消息去重。请确保该字段填的是随机数
|
||||
MsgTimeStamp int64 `json:"MsgTimeStamp,omitempty"` // (选填)消息时间戳,UNIX 时间戳(单位:秒)
|
||||
MsgBody []*types.MsgBody `json:"MsgBody"` // (必填)消息内容,具体格式请参考 消息格式描述(注意,一条消息可包括多种消息元素,MsgBody 为 Array 类型)
|
||||
SyncFromOldSystem int `json:"SyncFromOldSystem,omitempty"` // (选填)消息是否同步到在线终端和漫游上 1:把消息同步到 From_Account 在线终端和漫游上;2:消息不同步至 From_Account; 若不填写默认情况下会将消息存 From_Account 漫游
|
||||
CloudCustomData string `json:"CloudCustomData,omitempty"` // (选填)消息回调禁止开关,只对本条消息有效,
|
||||
}
|
||||
|
||||
// FetchMessagesArg 拉取消息参数
|
||||
FetchMessagesArg struct {
|
||||
FromUserId string `json:"From_Account"` // (必填)会话其中一方的 UserID,若已指定发送消息方帐号,则为消息发送方
|
||||
ToUserId string `json:"To_Account"` // (必填)会话其中一方的 UserID
|
||||
MaxLimited int `json:"MaxCnt"` // (必填)请求的消息条数
|
||||
MinTime int64 `json:"MinTime"` // (必填)请求的消息时间范围的最小值
|
||||
MaxTime int64 `json:"MaxTime"` // (必填)请求的消息时间范围的最大值
|
||||
LastMsgKey string `json:"LastMsgKey,omitempty"` // (选填)上一次拉取到的最后一条消息的 MsgKey,续拉时需要填该字段,填写方法见上方
|
||||
}
|
||||
|
||||
// 拉取消息参数(响应)
|
||||
fetchMessagesResp struct {
|
||||
types.ActionBaseResp
|
||||
Complete int `json:"Complete"` // 是否全部拉取,0表示未全部拉取,需要续拉,1表示已全部拉取
|
||||
LastMsgTime int64 `json:"LastMsgTime"` // 本次拉取到的消息里的最后一条消息的时间
|
||||
LastMsgKey string `json:"LastMsgKey"` // 本次拉取到的消息里的最后一条消息的标识
|
||||
MsgCount int `json:"MsgCnt"` // 本次拉取到的消息条数
|
||||
MsgList []*MessageItem `json:"MsgList"` // 消息列表
|
||||
}
|
||||
|
||||
// FetchMessagesRet 消息结果
|
||||
FetchMessagesRet struct {
|
||||
LastMsgTime int64 // 本次拉取到的消息里的最后一条消息的时间
|
||||
LastMsgKey string // 本次拉取到的消息里的最后一条消息的标识
|
||||
Count int // 本次拉取到的消息条数
|
||||
HasMore bool // 是否还有更多数据
|
||||
List []*MessageItem // 消息列表
|
||||
}
|
||||
|
||||
// MessageItem 消息项
|
||||
MessageItem struct {
|
||||
FromUserId string `json:"From_Account"`
|
||||
ToUserId string `json:"To_Account"`
|
||||
MsgSeq int `json:"MsgSeq"`
|
||||
MsgRandom int `json:"MsgRandom"`
|
||||
MsgTimeStamp int64 `json:"MsgTimeStamp"`
|
||||
MsgFlagBits int `json:"MsgFlagBits"`
|
||||
MsgKey string `json:"MsgKey"`
|
||||
MsgBody []*types.MsgBody `json:"MsgBody"`
|
||||
CloudCustomData string `json:"CloudCustomData"`
|
||||
}
|
||||
|
||||
// PullMessagesArg 持续拉取单聊消息参数
|
||||
PullMessagesArg struct {
|
||||
FromUserId string `json:"From_Account"` // (必填)会话其中一方的 UserID,若已指定发送消息方帐号,则为消息发送方
|
||||
ToUserId string `json:"To_Account"` // (必填)会话其中一方的 UserID
|
||||
MaxLimited int `json:"MaxCnt"` // (必填)请求的消息条数
|
||||
MinTime int64 `json:"MinTime"` // (必填)请求的消息时间范围的最小值
|
||||
MaxTime int64 `json:"MaxTime"` // (必填)请求的消息时间范围的最大值
|
||||
}
|
||||
|
||||
// 撤销消息(请求)
|
||||
revokeMessageReq struct {
|
||||
FromUserId string `json:"From_Account"` // (必填)消息发送方UserID
|
||||
ToUserId string `json:"To_Account"` // (必填)消息接收方UserID
|
||||
MsgKey string `json:"MsgKey"` // (必填)待撤回消息的唯一标识。该字段由 REST API 接口 单发单聊消息 和 批量发单聊消息 返回
|
||||
}
|
||||
|
||||
// 设置单聊消息已读(请求)
|
||||
setMessageReadReq struct {
|
||||
UserId string `json:"Report_Account"` // (必填)进行消息已读的用户UserId
|
||||
PeerUserId string `json:"Peer_Account"` // (必填)进行消息已读的单聊会话的另一方用户UserId
|
||||
}
|
||||
|
||||
// 查询单聊未读消息计数(请求)
|
||||
getUnreadMessageNumReq struct {
|
||||
UserId string `json:"To_Account"` // (必填)待查询的用户UserId
|
||||
PeerUserIds []string `json:"Peer_Account,omitempty"` // (选填)待查询的单聊会话对端的用户UserId
|
||||
}
|
||||
|
||||
// 查询单聊未读消息计数(响应)
|
||||
getUnreadMessageNumResp struct {
|
||||
types.ActionBaseResp
|
||||
AllUnreadMsgNum int `json:"AllC2CUnreadMsgNum"` // 单聊消息总未读数
|
||||
PeerUnreadMsgNums []unreadMessageNum `json:"C2CUnreadMsgNumList"` // 单聊消息未读对端列表
|
||||
PeerErrors []*UnreadMessageError `json:"ErrorList"` // 查询错误列表
|
||||
}
|
||||
|
||||
// 未读消息数
|
||||
unreadMessageNum struct {
|
||||
UserId string `json:"Peer_Account"` // 单聊会话对端UserId
|
||||
UnreadMsgNum int `json:"C2CUnreadMsgNum"` // 该单聊会话的未读数
|
||||
}
|
||||
|
||||
// 查询错误项
|
||||
UnreadMessageError struct {
|
||||
UserId string `json:"Peer_Account"` // 查询错误的目标UserId
|
||||
ErrorCode int `json:"ErrorCode"` // 查询错误的错误码。若目标帐号的错误码为70107表示该帐号不存在
|
||||
}
|
||||
|
||||
// GetUnreadMessageNumRet 未读消息结果
|
||||
GetUnreadMessageNumRet struct {
|
||||
Total int // 单聊消息总未读数
|
||||
Results map[string]int // 未读消息数列表
|
||||
Errors []*UnreadMessageError // 错误消息列表
|
||||
}
|
||||
|
||||
ImageInfo = types.ImageInfo
|
||||
MsgTextContent = types.MsgTextContent
|
||||
MsgFaceContent = types.MsgFaceContent
|
||||
MsgFileContent = types.MsgFileContent
|
||||
MsgImageContent = types.MsgImageContent
|
||||
MsgSoundContent = types.MsgSoundContent
|
||||
MsgVideoContent = types.MsgVideoContent
|
||||
MsgCustomContent = types.MsgCustomContent
|
||||
MsgLocationContent = types.MsgLocationContent
|
||||
)
|
Reference in New Issue
Block a user