-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotification.go
88 lines (69 loc) · 2.83 KB
/
notification.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
package whatsapp_chatbot_golang
import (
"errors"
"github.com/green-api/whatsapp-api-client-golang/pkg/api"
)
type Notification struct {
Body map[string]interface{}
StateManager
*api.GreenAPI
StateId string
ErrorChannel *chan error
}
func NewNotification(body map[string]interface{}, stateManager StateManager, greenAPI *api.GreenAPI, errorChannel *chan error) *Notification {
notification := Notification{Body: body, StateManager: stateManager, GreenAPI: greenAPI, StateId: "", ErrorChannel: errorChannel}
notification.createStateId()
return ¬ification
}
func (n *Notification) Text() (string, error) {
if n.isIncomingMessage() || n.isOutgoingMessage() {
typeMessage := n.Body["messageData"].(map[string]interface{})["typeMessage"].(string)
if typeMessage == "textMessage" {
return n.Body["messageData"].(map[string]interface{})["textMessageData"].(map[string]interface{})["textMessage"].(string), nil
} else if typeMessage == "extendedTextMessage" {
return n.Body["messageData"].(map[string]interface{})["extendedTextMessageData"].(map[string]interface{})["text"].(string), nil
}
}
return "", errors.New("text not exist, typeMessage isn't textMessage or extendedTextMessage")
}
func (n *Notification) Sender() (string, error) {
if n.isIncomingMessage() || n.isOutgoingMessage() {
return n.Body["senderData"].(map[string]interface{})["sender"].(string), nil
}
return "", errors.New("sender not found, it isn't message webhook")
}
func (n *Notification) ChatId() (string, error) {
if n.isIncomingMessage() || n.isOutgoingMessage() {
return n.Body["senderData"].(map[string]interface{})["chatId"].(string), nil
}
return "", errors.New("chatId not found, it isn't message webhook")
}
func (n *Notification) MessageType() (string, error) {
return n.Body["messageData"].(map[string]interface{})["typeMessage"].(string), nil
}
func (n *Notification) ActivateNextScene(scene Scene) {
n.StateManager.ActivateNextScene(n.StateId, scene)
}
func (n *Notification) GetCurrentScene() Scene {
return n.StateManager.GetCurrentScene(n.StateId)
}
func (n *Notification) GetStateData() map[string]interface{} {
return n.StateManager.GetStateData(n.StateId)
}
func (n *Notification) SetStateData(newStateData map[string]interface{}) {
n.StateManager.SetStateData(n.StateId, newStateData)
}
func (n *Notification) UpdateStateData(newStateData map[string]interface{}) {
n.StateManager.UpdateStateData(n.StateId, newStateData)
}
func (n *Notification) createStateId() {
if n.isIncomingMessage() {
n.StateId = n.Body["senderData"].(map[string]interface{})["chatId"].(string)
} else if n.isOutgoingMessage() {
n.StateId = n.Body["senderData"].(map[string]interface{})["chatId"].(string)
} else if n.isOutgoingMessageStatus() {
n.StateId = n.Body["chatId"].(string)
} else if n.isIncomingCall() {
n.StateId = n.Body["from"].(string)
}
}