-
Notifications
You must be signed in to change notification settings - Fork 158
/
messenger.go
80 lines (60 loc) · 1.52 KB
/
messenger.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
package customerService
import (
"context"
"errors"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/contract"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/messages"
)
type Messenger struct {
Message *contract.MessageInterface
To string
Account string
Client *Client
}
func NewMessenger(client *Client) *Messenger {
return &Messenger{
Client: client,
}
}
func (comp *Messenger) SetMessage(message *contract.MessageInterface) *Messenger {
comp.Message = message
return comp
}
func (comp *Messenger) SetBy(account string) *Messenger {
comp.Account = account
return comp
}
func (comp *Messenger) From(account string) *Messenger {
return comp.SetBy(account)
}
func (comp *Messenger) SetTo(openID string) *Messenger {
comp.To = openID
return comp
}
func (comp *Messenger) Send(ctx context.Context) (result interface{}, err error) {
if comp.Message == nil {
return result, errors.New("no message to send")
}
switch (*comp.Message).(type) {
case *messages.Raw:
content := (*comp.Message).(*messages.Raw).GetString("content", "")
result, err = comp.Client.Send(ctx, content)
default:
prepends := &object.HashMap{
"touser": comp.To,
}
if comp.Account != "" {
(*prepends)["customservice"] = &object.HashMap{
"kf_account": comp.Account,
}
}
var json *object.HashMap
json, err = (*comp.Message).TransformForJsonRequest(prepends, true)
if err != nil {
return
}
result, err = comp.Client.Send(ctx, json)
}
return result, err
}