-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
channels.go
97 lines (81 loc) · 2.64 KB
/
channels.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
package channels
//go:generate mockgen -source=channels.go -destination=mockchannels/mockchannels.go -package=mockchannels
import (
"net/http"
"strings"
"github.com/jaimeteb/chatto/internal/channels/messages"
"github.com/jaimeteb/chatto/internal/channels/rest"
"github.com/jaimeteb/chatto/internal/channels/slack"
"github.com/jaimeteb/chatto/internal/channels/telegram"
"github.com/jaimeteb/chatto/internal/channels/twilio"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
// Config struct combines all available client configurations
type Config struct {
Telegram telegram.Config `mapstructure:"telegram"`
Twilio twilio.Config `mapstructure:"twilio"`
Slack slack.Config `mapstructure:"slack"`
REST rest.Config `mapstructure:"rest"`
}
// Channels combines all available channel clients
type Channels struct {
Telegram Channel
Twilio Channel
REST Channel
Slack Channel
}
// Channel interface implements a channel to send and receive messages on
type Channel interface {
// ReceiveMessage from the channel
ReceiveMessage(body []byte) (*messages.Receive, error)
// ReceiveMessages from the channel. Starts a long running process, receives questions and sends them to the receiveChan
ReceiveMessages(receiveChan chan messages.Receive)
// SendMessage to the channel
SendMessage(response *messages.Response) error
// ValidateCallback validates a callback to the channel
ValidateCallback(r *http.Request) bool
// String returns the channel's name
String() string
}
// LoadConfig loads channels configuration from chn.yml
func LoadConfig(path string) (*Config, error) {
config := viper.New()
config.SetConfigName("chn")
config.AddConfigPath(path)
config.AutomaticEnv()
config.SetEnvPrefix("CHATTO_CHN")
config.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if err := config.ReadInConfig(); err != nil {
switch err.(type) {
case viper.ConfigFileNotFoundError:
log.Warn("File chn.yml not found, using only REST channel")
default:
return nil, err
}
}
var channelsConfig Config
if err := config.Unmarshal(&channelsConfig); err != nil {
return nil, err
}
return &channelsConfig, nil
}
// New initializes all channels
func New(channelsConfig *Config) *Channels {
chnls := Channels{}
// REST
chnls.REST = rest.New(channelsConfig.REST)
// TELEGRAM
if channelsConfig.Telegram != (telegram.Config{}) {
chnls.Telegram = telegram.New(channelsConfig.Telegram)
}
// TWILIO
if channelsConfig.Twilio != (twilio.Config{}) {
chnls.Twilio = twilio.New(channelsConfig.Twilio)
}
// SLACK
if channelsConfig.Slack != (slack.Config{}) {
chnls.Slack = slack.New(channelsConfig.Slack)
}
return &chnls
}