-
Notifications
You must be signed in to change notification settings - Fork 0
/
botmain.go
78 lines (67 loc) · 1.97 KB
/
botmain.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
package main
import (
"flag"
"github.com/spf13/viper"
"log"
"github.com/sur5an/WebhookSlackBotInGoLang/utils"
"github.com/sur5an/WebhookSlackBotInGoLang/slackintegration"
"github.com/sur5an/WebhookSlackBotInGoLang/rabbitmqinteraction"
)
const (
SlackToken = "slackToken"
RabbitMQDetails = "rabbitMQ"
)
var rabbitMQDefaults = map[string]string{
"username": "guest",
"password": "guest",
"host": "localhost",
"port": "5672",
"channel_to_listen": "",
}
func readConfig(configFile string, defaults map[string]interface{}) *viper.Viper {
v := viper.New()
for key, value := range defaults {
v.SetDefault(key, value)
}
v.SetConfigFile(configFile)
v.SetConfigType("yaml")
err := v.ReadInConfig()
utils.FailOnError(err, "unable to read the bot config")
return v
}
func main() {
inputConfig := flag.String("config", "./config/slackbot.yaml", "configuration file")
flag.Parse()
v := readConfig(*inputConfig,
map[string]interface{}{
"slackToken": nil,
"rabbitMQ": rabbitMQDefaults,
})
var client slackintegration.SlackClient
client.Connect(v.GetString(SlackToken))
getMessage := make(chan rabbitmqinteraction.SlackMessage)
sendAck := make(chan bool)
defer close(getMessage)
defer close(sendAck)
var mqClient rabbitmqinteraction.RabbitMQClient
rabbitMQDetails := v.GetStringMapString(RabbitMQDetails)
mqClient.Init(rabbitMQDetails)
mqClient.Connect()
defer mqClient.Channel.Close()
defer mqClient.Connection.Close()
go mqClient.Listen(rabbitMQDetails[rabbitmqinteraction.RabbitMQChannelToListen], getMessage, sendAck)
for {
messageToSlack := <-getMessage
log.Printf("Got a message to %s (%f)", messageToSlack.Consumer, messageToSlack.Time)
if messageToSlack.Consumer == rabbitmqinteraction.ForClient &&
messageToSlack.MessageToSend == rabbitmqinteraction.ChannelCloseEvent {
break
}
err := client.SendMessage(messageToSlack.MessageToSend, messageToSlack.Consumer)
ack := false
if err != nil {
ack = true
}
sendAck <- ack
}
}