This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (98 loc) · 2.63 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"log"
"os"
"github.com/sazo/slackbot/pkg/flow"
"github.com/sazo/slackbot/pkg/slackhelper"
"github.com/nlopes/slack"
)
func main() {
api := slack.New(
os.Getenv("SLACK_TOKEN"),
slack.OptionDebug(true),
slack.OptionLog(log.New(os.Stdout, "slack-bot: ", log.Lshortfile|log.LstdFlags)),
)
fm := &flow.FlowManager{
Questions: []*flow.Question{
&flow.Question{
Order: 1,
Question: "Where are you located (e.g. your country and city)?",
IsFirst: true,
},
&flow.Question{
Order: 2,
Question: "Tell us a little about your self, and your experience with Cloud Native (e.g. your background, where you work, which technologies are you using/looking at)",
IsFirst: false,
},
&flow.Question{
Order: 3,
Question: "Thank you, and the last question: What are you hoping to get from this community?",
IsFirst: false,
},
},
}
rtm := api.NewRTM()
go rtm.ManageConnection()
for msg := range rtm.IncomingEvents {
fmt.Print("Event Received: ")
switch ev := msg.Data.(type) {
case *slack.HelloEvent:
// Ignore hello
case *slack.ConnectedEvent:
fmt.Println("Infos:", ev.Info)
fmt.Println("Connection counter:", ev.ConnectionCount)
case *slack.MessageEvent:
fmt.Printf("Message: %v\n", ev)
if(ev.BotID != ""){
continue
}
userID := ev.User
isInFlow := fm.IsInFlow(userID)
if(!isInFlow){
fm.AddNew(ev.Channel, userID)
}
flowObj, err := fm.Get(userID)
if(err != nil){
panic(err)
}
q, _ := flowObj.GetPreviousQuestion()
if q != nil {
q.Answer = ev.Text
}
currentQuestion, _ := flowObj.GetCurrentQuestion()
flowObj.NextQuestion()
if(currentQuestion == nil){ // true if no more question
fm.Remove(userID)
rtm.PostMessage(
flowObj.ChannelID,
slack.MsgOptionText("Thats all thank you! :wave:", false),
slack.MsgOptionUser(flowObj.UserID),
slack.MsgOptionAsUser(true),
);
slackhelper.SendSummary(rtm, flowObj)
}else{
rtm.PostMessage(
flowObj.ChannelID,
slack.MsgOptionText(currentQuestion.Question, false),
slack.MsgOptionUser(flowObj.UserID),
slack.MsgOptionAsUser(true),
);
}
case *slack.PresenceChangeEvent:
fmt.Printf("Presence Change: %v\n", ev)
case *slack.LatencyReport:
fmt.Printf("Current latency: %v\n", ev.Value)
case *slack.RTMError:
fmt.Printf("Error: %s\n", ev.Error())
case *slack.InvalidAuthEvent:
fmt.Printf("Invalid credentials")
return
case *slack.RTMEvent:
fmt.Printf("RTMEvent: %v\n", ev)
default:
// Ignore other events..
// fmt.Printf("Unexpected: %v\n", msg.Data)
}
}
}