-
Notifications
You must be signed in to change notification settings - Fork 151
/
messager.go
107 lines (83 loc) · 2.16 KB
/
messager.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
package message
import (
"errors"
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/contract"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/messages"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/power"
"github.com/ArtisanCloud/PowerWeChat/v2/src/work/message/response"
"reflect"
"strings"
)
type Messager struct {
message *messages.Message
To *power.HashMap
AgentID int
secretive bool
Client *Client
}
func NewMessager(client *Client) *Messager {
m := &Messager{
Client: client,
}
return m
}
func (msg *Messager) Message(message contract.MessageInterface) *Messager {
msg.message.MessageInterface = message
return msg
}
func (msg *Messager) OfAgent(agentID int) *Messager {
msg.AgentID = agentID
return msg
}
func (msg *Messager) ToUser(userIDs interface{}) *Messager {
return msg.SetRecipients(userIDs, "touser")
}
func (msg *Messager) ToParty(partyIDs interface{}) *Messager {
return msg.SetRecipients(partyIDs, "toparty")
}
func (msg *Messager) ToTag(tagIDs interface{}) *Messager {
return msg.SetRecipients(tagIDs, "totag")
}
func (msg *Messager) Secretive() *Messager {
msg.secretive = true
return msg
}
func (msg *Messager) SetRecipients(ids interface{}, key string) *Messager {
var strIDs string
switch reflect.TypeOf(ids).Kind() {
case reflect.Array:
arrayIDs := ids.([]string)
strIDs = strings.Join(arrayIDs, "|")
case reflect.String:
strIDs = ids.(string)
default:
}
msg.To = &power.HashMap{key: strIDs}
return msg
}
func (msg *Messager) Send(message *messages.Message) (*response.ResponseMessageSend, error) {
if message != nil {
msg.Message(message)
}
if msg.AgentID <= 0 {
return nil, errors.New("no agentid specified")
}
secretive := 0
if msg.secretive {
secretive = 1
}
messageToSend, err := msg.message.TransformForJsonRequest(object.MergeHashMap(&object.HashMap{
"agentid": msg.AgentID,
"safe": secretive,
}, msg.To.ToHashMap()), true)
if err != nil {
return nil, err
}
msg.secretive = false
powerMessages, err := power.HashMapToPower(messageToSend)
if err != nil {
return nil, err
}
return msg.Client.Send(powerMessages)
}