-
Notifications
You must be signed in to change notification settings - Fork 5
/
message.go
149 lines (128 loc) · 3.54 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package nano
import (
"fmt"
"strconv"
"sync"
"time"
"github.com/FloatTech/ttl"
)
var (
triggeredMessages = ttl.NewCache[string, []string](time.Minute * 5)
triggeredMessagesMu = sync.Mutex{}
)
func logtriggeredmessages(id, reply string) {
triggeredMessagesMu.Lock()
defer triggeredMessagesMu.Unlock()
triggeredMessages.Set(id, append(triggeredMessages.Get(id), reply))
}
// GetTriggeredMessages 获取被 id 消息触发的回复消息 id
func GetTriggeredMessages(id string) []string {
triggeredMessagesMu.Lock()
defer triggeredMessagesMu.Unlock()
return triggeredMessages.Get(id)
}
type MessageSegmentType int
const (
MessageSegmentTypeText MessageSegmentType = iota
MessageSegmentTypeImage
MessageSegmentTypeImageBytes
MessageSegmentTypeReply
MessageSegmentTypeAudio
MessageSegmentTypeVideo
)
// Message impl the array form of message
type Messages []MessageSegment
// MessageSegment impl the single message
// MessageSegment 消息数组
type MessageSegment struct {
Type MessageSegmentType
Data string
}
// String impls the interface fmt.Stringer
func (m MessageSegment) String() string {
return m.Data
}
// Text 纯文本
func Text(text ...interface{}) MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeText,
Data: HideURL(MessageEscape(fmt.Sprint(text...))),
}
}
// Face QQ表情
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html#%E6%94%AF%E6%8C%81%E7%9A%84%E6%A0%BC%E5%BC%8F
func Face(id int) MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeText,
Data: "<emoji:" + strconv.Itoa(id) + ">",
}
}
// Image 普通图片
func Image(file string) MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeImage,
Data: file,
}
}
// ImageBytes 普通图片
func ImageBytes(data []byte) MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeImageBytes,
Data: BytesToString(data),
}
}
// At @某人
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html#%E6%94%AF%E6%8C%81%E7%9A%84%E6%A0%BC%E5%BC%8F
func At(id string) MessageSegment {
if id == "all" {
return AtAll()
}
return MessageSegment{
Type: MessageSegmentTypeText,
Data: "<@!" + id + ">",
}
}
// AtAll @全体成员
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html#%E6%94%AF%E6%8C%81%E7%9A%84%E6%A0%BC%E5%BC%8F
func AtAll() MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeText,
Data: "@everyone",
}
}
// AtChannel #频道
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html#%E6%94%AF%E6%8C%81%E7%9A%84%E6%A0%BC%E5%BC%8F
func AtChannel(id string) MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeText,
Data: "<#channel_id>",
}
}
// Record QQ 语音
// https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html
func Record(url string) MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeAudio,
Data: url,
}
}
// Video QQ 视频
// https://bot.q.qq.com/wiki/develop/api-231017/server-inter/message/send-receive/rich-text-media.html
func Video(url string) MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeVideo,
Data: url,
}
}
// Reply 回复
// https://github.com/botuniverse/onebot-11/tree/master/message/segment.md#%E5%9B%9E%E5%A4%8D
func ReplyTo(id string) MessageSegment {
return MessageSegment{
Type: MessageSegmentTypeReply,
Data: id,
}
}
// ReplyWithMessage returns a reply message
func ReplyWithMessage(messageID string, m ...MessageSegment) Messages {
return append(Messages{ReplyTo(messageID)}, m...)
}