message

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 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(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) AdapterName added in v1.0.1

func (serializer FileType) AdapterName() string

func (FileType) ToRawText

func (serializer FileType) ToRawText(msg MessageSegment) string

func (FileType) TypeName added in v1.0.1

func (serializer FileType) TypeName() string

type ImageType

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

func (ImageType) AdapterName added in v1.0.1

func (serializer ImageType) AdapterName() string

func (ImageType) ToRawText

func (serializer ImageType) ToRawText(msg MessageSegment) string

func (ImageType) TypeName added in v1.0.1

func (serializer ImageType) TypeName() 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 NewReply added in v1.0.1

func NewReply(m Message) *Message

func (*Message) AnySegment added in v1.0.1

func (m *Message) AnySegment(data MessageSerializer) *Message

AnySegment attachs any message segment to message

func (*Message) AttachSegment added in v1.0.1

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

Attach a segment for a message

func (*Message) File added in v1.0.1

func (m *Message) File(file string) *Message

File attachs a file message segment to message

func (Message) GetRawText

func (m Message) GetRawText() string

func (Message) GetSegments

func (m Message) GetSegments() []MessageSegment

func (*Message) Image added in v1.0.1

func (m *Message) Image(file string) *Message

Image attachs an image message segment to message

func (*Message) Join added in v1.0.2

func (m *Message) Join(msg Message) *Message

Attach the message contents together

func (*Message) MarshalJSON

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

func (*Message) Text added in v1.0.1

func (m *Message) Text(text string) *Message

Text attachs a plain text message segment to message

func (*Message) UnmarshalJSON

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

func (*Message) Video added in v1.0.1

func (m *Message) Video(file string) *Message

Video attachs a video message segment to message

func (*Message) Voice added in v1.0.1

func (m *Message) Voice(file string) *Message

Voice attachs a voice message segment to message

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 {
	// Which adapter is this serializer for
	AdapterName() string
	// Which message type is this serializer for
	TypeName() string
	// 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) AdapterName added in v1.0.1

func (serializer TextType) AdapterName() string

func (TextType) ToRawText

func (serializer TextType) ToRawText(msg MessageSegment) string

func (TextType) TypeName added in v1.0.1

func (serializer TextType) TypeName() string

type VideoType

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

func (VideoType) AdapterName added in v1.0.1

func (serializer VideoType) AdapterName() string

func (VideoType) ToRawText

func (serializer VideoType) ToRawText(msg MessageSegment) string

func (VideoType) TypeName added in v1.0.1

func (serializer VideoType) TypeName() string

type VoiceType

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

func (VoiceType) AdapterName added in v1.0.1

func (serializer VoiceType) AdapterName() string

func (VoiceType) ToRawText

func (serializer VoiceType) ToRawText(msg MessageSegment) string

func (VoiceType) TypeName added in v1.0.1

func (serializer VoiceType) TypeName() string

Jump to

Keyboard shortcuts

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