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