第45节 OpenMI 发消息的时候做了什么
❤️💕💕记录sealos开源项目的学习过程。k8s,docker和云原生的学习。Myblog:http://nsddd.top
[TOC]
流程
func (c *Conversation) CreateTextMessage(ctx context.Context, text string) (*sdk_struct.MsgStruct, error) {
	s := sdk_struct.MsgStruct{}
	err := c.initBasicInfo(ctx, &s, constant.UserMsgType, constant.Text)
	if err != nil {
		return nil, err
	}
	s.Content = text
	return &s, nil
}
func (c *Conversation) CreateAdvancedTextMessage(ctx context.Context, text string, messageEntities []*sdk_struct.MessageEntity) (*sdk_struct.MsgStruct, error) {
	s := sdk_struct.MsgStruct{}
	err := c.initBasicInfo(ctx, &s, constant.UserMsgType, constant.AdvancedText)
	if err != nil {
		return nil, err
	}
	s.MessageEntityElem.Text = text
	s.MessageEntityElem.MessageEntityList = messageEntities
	s.Content = utils.StructToJsonString(s.MessageEntityElem)
	return &s, nil
}
- CreateTextMessage(ctx context.Context, text string) (*sdk_struct.MsgStruct, error):这个方法用于创建一个普通文本消息。它接收一个上下文对象- ctx和一个字符串- text作为参数,返回一个指向- sdk_struct.MsgStruct类型对象的指针和一个错误对象。该方法首先创建一个空的- sdk_struct.MsgStruct对象- s,然后调用- initBasicInfo方法来初始化消息的基本信息,包括消息类型和内容类型。最后,将文本消息内容- text赋值给- s.Content字段,并返回- s指针和nil。
- CreateAdvancedTextMessage(ctx context.Context, text string, messageEntities []*sdk_struct.MessageEntity) (*sdk_struct.MsgStruct, error):这个方法用于创建一个高级文本消息,支持自定义消息实体。它接收一个上下文对象- ctx、一个字符串- text和一个- sdk_struct.MessageEntity类型的切片- messageEntities作为参数,返回一个指向- sdk_struct.MsgStruct类型对象的指针和一个错误对象。该方法首先创建一个空的- sdk_struct.MsgStruct对象- s,然后调用- initBasicInfo方法来初始化消息的基本信息,包括消息类型和内容类型。接下来,将文本消息内容- text赋值给- s.MessageEntityElem.Text字段,并将- sdk_struct.MessageEntity类型的切片- messageEntities赋值给- s.MessageEntityElem.MessageEntityList字段。最后,将- s.MessageEntityElem结构体序列化成- JSON格式的字符串并赋值给- s.Content字段,并返回- s指针和- nil。
结构体字段
这是一个很重并且很大的一个消息结构体,MsgStruct 结构体是我们给客户端用到的结构体。
type MsgStruct struct {
	ClientMsgID          string                `json:"clientMsgID,omitempty"`
	ServerMsgID          string                `json:"serverMsgID,omitempty"`
	CreateTime           int64                 `json:"createTime"`
	SendTime             int64                 `json:"sendTime"`
	SessionType          int32                 `json:"sessionType"`
	SendID               string                `json:"sendID,omitempty"`
	RecvID               string                `json:"recvID,omitempty"`
	MsgFrom              int32                 `json:"msgFrom"`
	ContentType          int32                 `json:"contentType"`
	SenderPlatformID     int32                 `json:"platformID"`
	SenderNickname       string                `json:"senderNickname,omitempty"`
	SenderFaceURL        string                `json:"senderFaceUrl,omitempty"`
	GroupID              string                `json:"groupID,omitempty"`
	Content              string                `json:"content,omitempty"`
	Seq                  int64                 `json:"seq"`
	IsRead               bool                  `json:"isRead"`
	Status               int32                 `json:"status"`
	IsReact              bool                  `json:"isReact,omitempty"`
	IsExternalExtensions bool                  `json:"isExternalExtensions,omitempty"`
	OfflinePush          sdkws.OfflinePushInfo `json:"offlinePush,omitempty"`
	AttachedInfo         string                `json:"attachedInfo,omitempty"`
	Ex                   string                `json:"ex,omitempty"`
	PictureElem          struct {
		SourcePath      string          `json:"sourcePath,omitempty"`
		SourcePicture   PictureBaseInfo `json:"sourcePicture,omitempty"`
		BigPicture      PictureBaseInfo `json:"bigPicture,omitempty"`
		SnapshotPicture PictureBaseInfo `json:"snapshotPicture,omitempty"`
	} `json:"pictureElem,omitempty"`
	SoundElem struct {
		UUID      string `json:"uuid,omitempty"`
		SoundPath string `json:"soundPath,omitempty"`
		SourceURL string `json:"sourceUrl,omitempty"`
		DataSize  int64  `json:"dataSize"`
		Duration  int64  `json:"duration"`
		SoundType string `json:"soundType,omitempty"`
	} `json:"soundElem,omitempty"`
	VideoElem struct {
		VideoPath      string `json:"videoPath,omitempty"`
		VideoUUID      string `json:"videoUUID,omitempty"`
		VideoURL       string `json:"videoUrl,omitempty"`
		VideoType      string `json:"videoType,omitempty"`
		VideoSize      int64  `json:"videoSize"`
		Duration       int64  `json:"duration"`
		SnapshotPath   string `json:"snapshotPath,omitempty"`
		SnapshotUUID   string `json:"snapshotUUID,omitempty"`
		SnapshotSize   int64  `json:"snapshotSize"`
		SnapshotURL    string `json:"snapshotUrl,omitempty"`
		SnapshotWidth  int32  `json:"snapshotWidth"`
		SnapshotHeight int32  `json:"snapshotHeight"`
		SnapshotType   string `json:"snapshotType,omitempty"`
	} `json:"videoElem,omitempty"`
	FileElem struct {
		FilePath  string `json:"filePath,omitempty"`
		UUID      string `json:"uuid,omitempty"`
		SourceURL string `json:"sourceUrl,omitempty"`
		FileName  string `json:"fileName,omitempty"`
		FileSize  int64  `json:"fileSize"`
		FileType  string `json:"fileType,omitempty"`
	} `json:"fileElem,omitempty"`
	MergeElem struct {
		Title             string           `json:"title,omitempty"`
		AbstractList      []string         `json:"abstractList,omitempty"`
		MultiMessage      []*MsgStruct     `json:"multiMessage,omitempty"`
		MessageEntityList []*MessageEntity `json:"messageEntityList,omitempty"`
	} `json:"mergeElem,omitempty"`
	AtElem struct {
		Text         string     `json:"text,omitempty"`
		AtUserList   []string   `json:"atUserList,omitempty"`
		AtUsersInfo  []*AtInfo  `json:"atUsersInfo,omitempty"`
		QuoteMessage *MsgStruct `json:"quoteMessage,omitempty"`
		IsAtSelf     bool       `json:"isAtSelf"`
	} `json:"atElem,omitempty"`
	FaceElem struct {
		Index int    `json:"index"`
		Data  string `json:"data,omitempty"`
	} `json:"faceElem,omitempty"`
	LocationElem struct {
		Description string  `json:"description,omitempty"`
		Longitude   float64 `json:"longitude"`
		Latitude    float64 `json:"latitude"`
	} `json:"locationElem,omitempty"`
	CustomElem struct {
		Data        string `json:"data,omitempty"`
		Description string `json:"description,omitempty"`
		Extension   string `json:"extension,omitempty"`
	} `json:"customElem,omitempty"`
	QuoteElem struct {
		Text              string           `json:"text,omitempty"`
		QuoteMessage      *MsgStruct       `json:"quoteMessage,omitempty"`
		MessageEntityList []*MessageEntity `json:"messageEntityList,omitempty"`
	} `json:"quoteElem,omitempty"`
	NotificationElem struct {
		Detail      string `json:"detail,omitempty"`
		DefaultTips string `json:"defaultTips,omitempty"`
	} `json:"notificationElem,omitempty"`
	MessageEntityElem struct {
		Text              string           `json:"text,omitempty"`
		MessageEntityList []*MessageEntity `json:"messageEntityList,omitempty"`
	} `json:"messageEntityElem,omitempty"`
	AttachedInfoElem AttachedInfoElem `json:"attachedInfoElem,omitempty"`
}
这是一个名为 MsgStruct 的结构体,包含了一个即时通讯消息的各种元素。下面是每个字段的解释:
- ClientMsgID:客户端生成的消息 ID。
- ServerMsgID:服务器生成的消息 ID。
- CreateTime:消息创建时间,Unix 时间戳。
- SendTime:消息发送时间,Unix 时间戳。
- SessionType:会话类型,如单聊、群聊等。
- SendID:消息发送者的 ID。
- RecvID:消息接收者的 ID。
- MsgFrom:消息来源,如发送方或接收方。
- ContentType:消息内容类型,如文本、图片、音频等。
- SenderPlatformID:发送者使用的平台 ID。
- SenderNickname:发送者的昵称。
- SenderFaceURL:发送者的头像 URL。
- GroupID:如果是群聊消息,群聊 ID。
- Content:消息内容。
- Seq:消息序列号。
- IsRead:消息是否已读。
- Status:消息状态,如发送中、发送成功、发送失败等。
- IsReact:是否需要反馈。
- IsExternalExtensions:是否包含扩展信息。
- OfflinePush:离线推送信息,包括标题、内容、声音等。
- AttachedInfo:附加信息。
- Ex:预留字段。
- PictureElem:图片消息元素,包括原图、大图、快照等。
- SoundElem:音频消息元素,包括 UUID、路径、大小、时长等。
- VideoElem:视频消息元素,包括路径、UUID、大小、时长、快照等。
- FileElem:文件消息元素,包括路径、UUID、大小、名称、类型等。
- MergeElem:合并消息元素,包括标题、摘要、多条消息、消息实体等。
- AtElem:@ 消息元素,包括文本、@ 用户列表、@ 用户信息、引用消息、是否 @ 自己等。
- FaceElem:表情消息元素,包括表情索引、数据等。
- LocationElem:位置消息元素,包括描述、经度、纬度等。
- CustomElem:自定义消息元素,包括数据、描述、扩展等。
- QuoteElem:引用消息元素,包括文本、引用的消息、消息实体等。
- NotificationElem:通知消息元素,包括详情、默认提示等。
- MessageEntityElem:消息实体元素,包括文本、消息实体列表等。
- AttachedInfoElem:附加信息元素,包括类型、数据等。
发送消息需要的结构体字段
type TextElem struct {
	Text string `json:"text,omitempty"`
}
type MsgStruct struct {
	ClientMsgID          string                `json:"clientMsgID,omitempty"`
	CreateTime           int64                 `json:"createTime"`
	SendTime             int64                 `json:"sendTime"`
	MsgFrom              int32                 `json:"msgFrom"`
	ContentType          int32                 `json:"contentType"`
	SenderPlatformID     int32                 `json:"platformID"`
	SenderNickname       string                `json:"senderNickname,omitempty"`
	SenderFaceURL        string                `json:"senderFaceUrl,omitempty"`
	Status               int32                 `json:"status"`
	IsReact              bool                  `json:"isReact,omitempty"`
	IsExternalExtensions bool                  `json:"isExternalExtensions,omitempty"`
	OfflinePush          sdkws.OfflinePushInfo `json:"offlinePush,omitempty"`
	AttachedInfo         string                `json:"attachedInfo,omitempty"`
	Ex                   string                `json:"ex,omitempty"`
	TextElem             TextElem              `json:"textElem,omitempty"`
}
创建结构体的逻辑部分:
func (c *Conversation) initBasicInfo(ctx context.Context, message *sdk_struct.MsgStruct, msgFrom, contentType int32) error {
	message.CreateTime = utils.GetCurrentTimestampByMill()
	message.SendTime = message.CreateTime
	message.IsRead = false
	message.Status = constant.MsgStatusSending
	message.SendID = c.loginUserID
	userInfo, err := c.db.GetLoginUser(ctx, c.loginUserID)
	if err != nil {
		return err
	} else {
		message.SenderFaceURL = userInfo.FaceURL
		message.SenderNickname = userInfo.Nickname
	}
	ClientMsgID := utils.GetMsgID(message.SendID)
	message.ClientMsgID = ClientMsgID
	message.MsgFrom = msgFrom
	message.ContentType = contentType
	message.SenderPlatformID = c.platformID
	message.IsExternalExtensions = c.IsExternalExtensions
	return nil
}
END 链接
- ✴️版权声明 © :本书所有内容遵循CC-BY-SA 3.0协议(署名-相同方式共享)©