-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit, - adds discord bot - adds discord notifier - adds required discord configurations into comm_config.yaml - adds discordgo module into go.mod - adds discord configs into all yaml files - adds apk add build-base into dockerfile Build Env
- Loading branch information
Showing
11 changed files
with
425 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// Copyright (c) 2020 InfraCloud Technologies | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software is furnished to do so, | ||
// subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
package bot | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/infracloudio/botkube/pkg/config" | ||
"github.com/infracloudio/botkube/pkg/execute" | ||
"github.com/infracloudio/botkube/pkg/log" | ||
) | ||
|
||
// DiscordBot listens for user's message, execute commands and sends back the response | ||
type DiscordBot struct { | ||
Token string | ||
AllowKubectl bool | ||
RestrictAccess bool | ||
ClusterName string | ||
ChannelID string | ||
BotID string | ||
DefaultNamespace string | ||
} | ||
|
||
// discordMessage contains message details to execute command and send back the result | ||
type discordMessage struct { | ||
Event *discordgo.MessageCreate | ||
BotID string | ||
Request string | ||
Response string | ||
IsAuthChannel bool | ||
Session *discordgo.Session | ||
} | ||
|
||
// NewDiscordBot returns new Bot object | ||
func NewDiscordBot(c *config.Config) Bot { | ||
return &DiscordBot{ | ||
Token: c.Communications.Discord.Token, | ||
BotID: c.Communications.Discord.BotID, | ||
AllowKubectl: c.Settings.Kubectl.Enabled, | ||
RestrictAccess: c.Settings.Kubectl.RestrictAccess, | ||
ClusterName: c.Settings.ClusterName, | ||
ChannelID: c.Communications.Discord.Channel, | ||
DefaultNamespace: c.Settings.Kubectl.DefaultNamespace, | ||
} | ||
} | ||
|
||
// Start starts the DiscordBot websocket connection and listens for messages | ||
func (b *DiscordBot) Start() { | ||
api, err := discordgo.New("Bot " + b.Token) | ||
if err != nil { | ||
log.Error("error creating Discord session,", err) | ||
return | ||
} | ||
|
||
// Register the messageCreate func as a callback for MessageCreate events. | ||
api.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { | ||
dm := discordMessage{ | ||
Event: m, | ||
BotID: b.BotID, | ||
Session: s, | ||
} | ||
|
||
dm.HandleMessage(b) | ||
}) | ||
|
||
// Open a websocket connection to Discord and begin listening. | ||
go func() { | ||
err := api.Open() | ||
if err != nil { | ||
log.Error("error opening connection,", err) | ||
return | ||
} | ||
}() | ||
|
||
log.Info("BotKube connected to Discord!") | ||
} | ||
|
||
// HandleMessage handles the incomming messages | ||
func (dm *discordMessage) HandleMessage(b *DiscordBot) { | ||
|
||
// Serve only if starts with mention | ||
if !strings.HasPrefix(dm.Event.Content, "<@!"+dm.BotID+"> ") { | ||
return | ||
} | ||
|
||
log.Debugf("Discord incoming message: %+v", dm.Event.Content) | ||
|
||
// Serve only if current channel is in config | ||
if b.ChannelID == dm.Event.ChannelID { | ||
dm.IsAuthChannel = true | ||
} | ||
|
||
// Trim the @BotKube prefix | ||
dm.Request = strings.TrimPrefix(dm.Event.Content, "<@!"+dm.BotID+"> ") | ||
if len(dm.Request) == 0 { | ||
return | ||
} | ||
|
||
e := execute.NewDefaultExecutor(dm.Request, b.AllowKubectl, b.RestrictAccess, b.DefaultNamespace, b.ClusterName, b.ChannelID, dm.IsAuthChannel) | ||
dm.Response = e.Execute() | ||
dm.Send() | ||
} | ||
|
||
func (dm discordMessage) Send() { | ||
// Upload message as a file if too long | ||
if len(dm.Response) >= 2000 { | ||
params := &discordgo.MessageSend{ | ||
Content: dm.Request, | ||
Files: []*discordgo.File{ | ||
{ | ||
Name: "Response", | ||
Reader: strings.NewReader(dm.Response), | ||
}, | ||
}, | ||
} | ||
|
||
if _, err := dm.Session.ChannelMessageSendComplex(dm.Event.ChannelID, params); err != nil { | ||
log.Error("Error in uploading file:", err) | ||
} | ||
return | ||
} else if len(dm.Response) == 0 { | ||
log.Info("Invalid request. Dumping the response") | ||
return | ||
} | ||
|
||
if _, err := dm.Session.ChannelMessageSend(dm.Event.ChannelID, dm.Response); err != nil { | ||
log.Error("Error in sending message:", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.