-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer_journey.go
81 lines (58 loc) · 1.62 KB
/
consumer_journey.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
package lowbot
import (
"fmt"
"github.com/google/uuid"
)
type JourneyConsumer struct {
*Consumer
Flow *Flow
Persist FlowPersist
}
func NewJourneyConsumer(flow *Flow, persist FlowPersist) IConsumer {
return &JourneyConsumer{
Consumer: &Consumer{
ConsumerID: uuid.New(),
Name: CONSUMER_JOURNEY_NAME,
},
Flow: flow,
Persist: persist,
}
}
func (consumer *JourneyConsumer) GetConsumer() *Consumer {
return consumer.Consumer
}
func (consumer *JourneyConsumer) Run(interaction *Interaction) ([]*Interaction, error) {
flow, err := consumer.Persist.Get(interaction.From.WhoID)
flowNotExistsOrWasFinished := err != nil || flow.Ended()
if flowNotExistsOrWasFinished {
copyFlow := *consumer.Flow
copyFlow.Start()
flow = ©Flow
}
interactions, err := consumer.getInteractions(flow, interaction)
consumer.Persist.Set(interaction.From.WhoID, flow)
printLog(fmt.Sprintf("WhoID:<%v> Step:<%s> ERR: %v\n", interaction.From.WhoID, flow.CurrentStepName, err))
return interactions, err
}
func (consumer *JourneyConsumer) getInteractions(flow *Flow, interaction *Interaction) ([]*Interaction, error) {
next := true
interactions := []*Interaction{}
for next {
err := flow.Next(interaction)
if err != nil {
return interactions, err
}
action, err := GetAction(flow)
if err != nil {
return interactions, err
}
interaction.SetStep(*flow.CurrentStep)
answerInteraction, wait := action(interaction)
answerInteraction.SetStep(*flow.CurrentStep)
next = !wait
if answerInteraction != nil {
interactions = append(interactions, answerInteraction)
}
}
return interactions, nil
}