-
Notifications
You must be signed in to change notification settings - Fork 174
/
messageBuilder.go
99 lines (74 loc) · 2.1 KB
/
messageBuilder.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
package broadcasting
import (
"errors"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/contract"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
"github.com/ArtisanCloud/PowerWeChat/v3/src/officialAccount/broadcasting/request"
)
type MessageBuilder struct {
To *object.HashMap
Message contract.MessageInterface
Attribute *object.Attribute
}
func NewMessengerBuilder() *MessageBuilder {
return &MessageBuilder{
Attribute: object.NewAttribute(&object.HashMap{}),
}
}
func (comp *MessageBuilder) SetMessage(message contract.MessageInterface) *MessageBuilder {
comp.Message = message
return comp
}
func (comp *MessageBuilder) SetTo(to *power.HashMap) *MessageBuilder {
objTo, _ := power.PowerHashMapToObjectHashMap(to)
comp.To = objTo
return comp
}
func (comp *MessageBuilder) ToTag(tagID int) *MessageBuilder {
comp.SetTo(&power.HashMap{
"filter": &power.HashMap{
"is_to_all": false,
"tag_id": tagID,
},
})
return comp
}
func (comp *MessageBuilder) ToUsers(openIDs []string) *MessageBuilder {
comp.SetTo(&power.HashMap{
"touser": openIDs,
})
return comp
}
func (comp *MessageBuilder) ToAll() *MessageBuilder {
comp.SetTo(&power.HashMap{
"filter": &power.HashMap{
"is_to_all": true,
},
})
return comp
}
func (comp *MessageBuilder) With(attributes *power.HashMap) (*MessageBuilder, error) {
objAttributes, _ := power.PowerHashMapToObjectHashMap(attributes)
comp.Attribute.SetAttributes(objAttributes)
return comp, nil
}
func (comp *MessageBuilder) Build(prepends *object.HashMap) (*object.HashMap, error) {
if comp.Message == nil {
return nil, errors.New("no message content to send")
}
content, err := comp.Message.TransformForJsonRequest(&object.HashMap{}, true)
if err != nil {
return nil, err
}
if prepends == nil {
prepends = comp.To
}
message := object.MergeHashMap(prepends, content, comp.Attribute.GetAttributes())
return message, nil
}
func (comp *MessageBuilder) BuildForPreview(by string, user *request.Reception) (*object.HashMap, error) {
return comp.Build(&object.HashMap{
by: user,
})
}