Skip to content

Commit daaa946

Browse files
committed
fix: ?
1 parent 793abf8 commit daaa946

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/guide/adapter/methods.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,42 @@ They will pull a message or action from the `SendChannel` or `ActionChannel` res
2626
They will not return until a message or action is received.
2727

2828
~~Oh shoot, that's all of it?~~
29+
30+
By the way, for message types and event types, you need to implement the corresponding `MessageType` method to facilitate `GoneBot` processing:
31+
```go
32+
// This describes a simple part of a message
33+
type MessageSegment struct {
34+
// Message type
35+
Type string `json:"type"`
36+
// Make sure it implements MessageType interface
37+
Data MessageType `json:"data"`
38+
}
39+
40+
// Implement this to create a message type
41+
type MessageType interface {
42+
// Which adapter is this message for
43+
AdapterName() string
44+
// Which message type is this message for
45+
TypeName() string
46+
// Convert this message segment to raw text
47+
ToRawText(msg MessageSegment) string
48+
}
49+
```
50+
For example, the text message type of `GoneBot` is implemented like this:
51+
```go
52+
type TextType struct {
53+
Text string `json:"text"`
54+
}
55+
56+
func (serializer TextType) AdapterName() string {
57+
return ""
58+
}
59+
60+
func (serializer TextType) TypeName() string {
61+
return "text"
62+
}
63+
64+
func (serializer TextType) ToRawText(msg MessageSegment) string {
65+
return msg.Data.(TextType).Text
66+
}
67+
```

src/zh/guide/adapter/methods.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 常用方法
2+
title: 常用方法 & 约定
33
icon: meteor
44
order: 3
55
category:
@@ -25,3 +25,42 @@ author: Kingcq
2525
分别从 `SendChannel``ActionChannel` 中取出一个消息或动作,用来处理,在没有消息或动作时会阻塞线程,直到有消息或动作再返回。
2626

2727
~~卧槽,没了~~
28+
29+
同时,对于消息段类型和事件类型,你需要实现对应的 MessageType 方法,以方便 `GoneBot` 处理:
30+
```go
31+
// This describes a simple part of a message
32+
type MessageSegment struct {
33+
// Message type
34+
Type string `json:"type"`
35+
// Make sure it implements MessageType interface
36+
Data MessageType `json:"data"`
37+
}
38+
39+
// Implement this to create a message type
40+
type MessageType interface {
41+
// Which adapter is this message for
42+
AdapterName() string
43+
// Which message type is this message for
44+
TypeName() string
45+
// Convert this message segment to raw text
46+
ToRawText(msg MessageSegment) string
47+
}
48+
```
49+
例如,`GoneBot` 内置的文本消息类型是这样实现的:
50+
```go
51+
type TextType struct {
52+
Text string `json:"text"`
53+
}
54+
55+
func (serializer TextType) AdapterName() string {
56+
return ""
57+
}
58+
59+
func (serializer TextType) TypeName() string {
60+
return "text"
61+
}
62+
63+
func (serializer TextType) ToRawText(msg MessageSegment) string {
64+
return msg.Data.(TextType).Text
65+
}
66+
```

0 commit comments

Comments
 (0)