-
Notifications
You must be signed in to change notification settings - Fork 4
/
receive.go
57 lines (48 loc) · 1.19 KB
/
receive.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
package youdu
import (
"encoding/json"
)
var (
ErrReceiveEmptyEncrypt = newError(-1, "encrypt is empty")
ErrReceiveEmptyData = newError(-1, "data is empty")
)
type ReceiveRequest struct {
ToBuin int `json:"toBuin"`
ToApp string `json:"toApp"`
Encrypt string `json:"encrypt"`
}
type ReceiveMessage struct {
FromUser string `json:"fromUser"`
CreateTime int `json:"createTime"`
PackageID string `json:"packageId"`
MsgType MsgType `json:"msgType"`
Text MessageText `json:"text,omitempty"`
Image MessageMedia `json:"image,omitempty"`
File MessageFile `json:"file,omitempty"`
}
type Receive struct {
config *Config
encryptor *Encryptor
}
func NewReceive(config *Config) *Receive {
return &Receive{
config: config,
encryptor: NewEncryptorWithConfig(config),
}
}
func (s *Receive) Decrypt(request ReceiveRequest) (message ReceiveMessage, err error) {
if request.Encrypt == "" {
err = ErrReceiveEmptyEncrypt
return
}
rawData, err := s.encryptor.Decrypt(request.Encrypt)
if err != nil {
return
}
if rawData.Data == nil {
err = ErrReceiveEmptyData
return
}
err = json.Unmarshal(rawData.Data, &message)
return
}