message

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 18, 2024 License: Apache-2.0 Imports: 5 Imported by: 4

Documentation

Index

Constants

View Source
const ACTION_CHAN_CAPACITY = 64
View Source
const LOG_MESSAGE_LEN_THRESHOLD = 256
View Source
const MESSAGE_CHAN_CAPACITY = 64

Variables

View Source
var SerializerRegistry = make(map[string]MessageSerializer)

Functions

func AddReceivedCount

func AddReceivedCount()

AddReceivedCount increases the received count by one

func AddSentCount

func AddSentCount()

AddSentCount increases the sent count by one

func GetReceivedCount

func GetReceivedCount() (result int)

GetReceivedCount returns the current received count

func GetSentCount

func GetSentCount() (result int)

GetSentCount returns the current sent count

func Init

func Init()

func RegisterSerializer

func RegisterSerializer(typeName, adapterName string, serializer MessageSerializer)

Types

type ActionCall

type ActionCall struct {
	// Which adapter to call
	AdapterName string
	// Which action
	Action any
	// Which channel to push the result, will initialize automatically
	ResultChannel chan any
}

If you want to call adapter action, you should use this struct

type ActionChannel

type ActionChannel struct {
	// contains filtered or unexported fields
}

func NewActionChannel

func NewActionChannel() *ActionChannel

Create a new ActionChannel

func (*ActionChannel) Pull

func (ac *ActionChannel) Pull() *ActionCall

Pull a message from the channel

func (*ActionChannel) Push

func (ac *ActionChannel) Push(action *ActionCall)

Push a message to the channel

If the channel is full, the oldest message will be dropped

type FileType

type FileType struct {
	MessageType
	File string `json:"file"`
}

func (FileType) Matcher

func (serializer FileType) Matcher(typeName, adapterName string) bool

func (FileType) ToRawText

func (serializer FileType) ToRawText(msg MessageSegment) string

type ImageType

type ImageType struct {
	MessageType
	File string `json:"file"`
}

func (ImageType) Matcher

func (serializer ImageType) Matcher(typeName, adapterName string) bool

func (ImageType) ToRawText

func (serializer ImageType) ToRawText(msg MessageSegment) string

type Message

type Message struct {
	// Is the message to me?
	IsToMe bool
	// Which group is it in?(Only useful with group message and notice)
	Group string
	// Who is sending this message?
	Sender string
	// Whi is receiving this message?
	Receiver string
	// Who am i?
	Self string
	// contains filtered or unexported fields
}

This describes the whole message

func (*Message) Attach

func (m *Message) Attach(seg MessageSegment, serializer MessageSerializer)

Attach a segment for a message

func (Message) GetRawText

func (m Message) GetRawText() string

func (Message) GetSegments

func (m Message) GetSegments() []MessageSegment

func (*Message) MarshalJSON

func (m *Message) MarshalJSON() ([]byte, error)

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(data []byte) error

type MessageChannel

type MessageChannel struct {
	// contains filtered or unexported fields
}

func NewMessageChannel

func NewMessageChannel() *MessageChannel

Create a new MessageChannel

func (*MessageChannel) Pull

func (mc *MessageChannel) Pull() Message

Pull a message from the channel

func (*MessageChannel) Push

func (mc *MessageChannel) Push(msg Message, isReceive bool)

Push a message to the channel

If the channel is full, the oldest message will be dropped

If isReceive is true, the message will be counted as received, otherwise it will be counted as result

type MessageSegment

type MessageSegment struct {
	// Message type
	Type string `json:"type"`
	// Which adapter is this message type for?
	// Leave it empty if you are using universal message type
	// If you have multiple adapters that have the same name, what can i say?
	Adapter string `json:"adapter"`
	// Use a message serializer to decode this
	Data string `json:"data"`
}

This describes a simple part of a message

type MessageSerializer

type MessageSerializer interface {
	// Whether the message segment is supported by this serializer
	Matcher(typeName, adapterName string) bool
	// Serialize a data to string
	Serialize(data any, messageType reflect.Type) string
	// Deserialize a message from string, will change to serializer type
	Deserialize(str string, messageType reflect.Type) any
	// Convert this message segment to raw text
	ToRawText(msg MessageSegment) string
}

Implement this to create a message serializer

func GetSerializer

func GetSerializer(typeName, adapterName string) MessageSerializer

type MessageType

type MessageType struct{}

func (MessageType) Deserialize

func (serializer MessageType) Deserialize(data string, messageType reflect.Type) any

Deserialize a message from string, will change to serializer type

func (MessageType) Serialize

func (serializer MessageType) Serialize(data any, messageType reflect.Type) string

Serialize a data to string

type TextType

type TextType struct {
	MessageType
	Text string `json:"text"`
}

func (TextType) Matcher

func (serializer TextType) Matcher(typeName, adapterName string) bool

func (TextType) ToRawText

func (serializer TextType) ToRawText(msg MessageSegment) string

type VideoType

type VideoType struct {
	MessageType
	File string `json:"file"`
}

func (VideoType) Matcher

func (serializer VideoType) Matcher(typeName, adapterName string) bool

func (VideoType) ToRawText

func (serializer VideoType) ToRawText(msg MessageSegment) string

type VoiceType

type VoiceType struct {
	MessageType
	File string `json:"file"`
}

func (VoiceType) Matcher

func (serializer VoiceType) Matcher(typeName, adapterName string) bool

func (VoiceType) ToRawText

func (serializer VoiceType) ToRawText(msg MessageSegment) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL