-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
60 lines (50 loc) · 1.18 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
package main
import (
"fmt"
"github.com/jinzhu/configor"
"github.com/nlopes/slack"
)
//const BOT_ID = "U5FV0RATF"
//const AT_BOT = "<@" + BOT_ID + ">"
var api *slack.Client
var Config = struct {
Token string
}{}
func main() {
configor.Load(&Config, "config.yml")
api = slack.New(Config.Token)
fmt.Print("Started\n")
botID, err := getBotID(api)
if err != nil {
panic(fmt.Sprintf("Error: %s\n", err))
}
//botName := "<@" + botID + ">"
rtm := api.NewRTM()
go rtm.ManageConnection()
for msg := range rtm.IncomingEvents {
switch evn := msg.Data.(type) {
case *slack.MessageEvent:
//TODO: prevent bot's message triggering the event
//fmt.Printf("Message: %s, %s, %s\n", evn.Text, evn.Channel, evn.User == BOT_ID)
if evn.User != botID {
response := parse(evn.Text)
fmt.Printf("response: %s\n", response)
rtm.SendMessage(rtm.NewOutgoingMessage(response, evn.Channel))
}
}
}
}
func getBotID(api *slack.Client) (string, error) {
users, err := api.GetUsers()
var userID string
if err != nil {
fmt.Printf("errror: %s\n", err)
return "", err
}
for _, user := range users {
if (user.Name == "naheebot") {
userID = user.ID
}
}
return userID, nil
}