Skip to content

Commit

Permalink
feat: Mattermost notification provider
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Mar 6, 2024
1 parent b9ea7cb commit 08928d1
Show file tree
Hide file tree
Showing 18 changed files with 700 additions and 545 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-latest]
go-version: [1.19.x, 1.20.x]
go-version: [1.21.x, 1.22.x]

runs-on: ${{ matrix.platform }}

Expand Down
50 changes: 35 additions & 15 deletions bot.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,81 @@
package main

import (
"github.com/BoltzExchange/channel-bot/notifications/providers"
"github.com/BoltzExchange/channel-bot/utils"
"github.com/lightningnetwork/lnd/lnrpc"
"sync"

"github.com/google/logger"
"github.com/lightningnetwork/lnd/lnrpc"
)

var lndInfo *lnrpc.GetInfoResponse

func main() {
cfg := loadConfig()
initLogger(cfg.LogFile)
logConfig(cfg)

initLnd(cfg)
initDiscord(cfg)
lndInfo := initLnd(cfg)
provider := getNotificationProvider(cfg)

err := provider.SendMessage("Started channel bot with LND node: **" + lndInfo.Alias + "** (`" + lndInfo.IdentityPubkey + "`)")
checkError("Notification provider", err)

var wg sync.WaitGroup
wg.Add(2)

go func() {
cfg.Notifications.Init(cfg.SignificantChannels, cfg.Lnd, cfg.Discord)
cfg.Notifications.Init(cfg.SignificantChannels, cfg.Lnd, provider)
wg.Done()
}()

go func() {
cfg.ChannelCleaner.Init(cfg.Lnd, cfg.Discord)
cfg.ChannelCleaner.Init(cfg.Lnd, provider)
wg.Done()
}()

wg.Wait()
logger.Info("Shutting down")
}

func initLnd(cfg *config) {
func initLnd(cfg *config) *lnrpc.GetInfoResponse {
logger.Info("Initializing LND client")

err := cfg.Lnd.Connect()
checkError("LND", err)

lndInfo, err = cfg.Lnd.GetInfo()
lndInfo, err := cfg.Lnd.GetInfo()
checkError("LND", err)

lndInfo.Features = nil
logger.Info("Initialized LND client: ", utils.Stringify(lndInfo))

return lndInfo
}

func initDiscord(cfg *config) {
logger.Info("Initializing Discord client")
func getNotificationProvider(cfg *config) providers.NotificationProvider {
logger.Info("Initializing notification provider client")

for _, provider := range []providers.NotificationProvider{
cfg.Mattermost,
cfg.Discord,
} {
initialized := initNotificationProvider(provider)
if initialized != nil {
return initialized
}
}

return cfg.Discord
}

err := cfg.Discord.Init()
checkError("Discord", err)
func initNotificationProvider(provider providers.NotificationProvider) providers.NotificationProvider {
err := provider.Init()
if err != nil {
logger.Warningf("Could not init %s: %s\n", provider.Name(), err.Error())
return nil
}

err = cfg.Discord.SendMessage("Started channel bot with LND node: **" + lndInfo.Alias + "** (`" + lndInfo.IdentityPubkey + "`)")
checkError("Discord", err)
return provider
}

func checkError(service string, err error) {
Expand Down
12 changes: 6 additions & 6 deletions cleaner/cleaner.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cleaner

import (
"github.com/BoltzExchange/channel-bot/discord"
"github.com/BoltzExchange/channel-bot/lnd"
"github.com/BoltzExchange/channel-bot/notifications/providers"
"github.com/google/logger"
"github.com/lightningnetwork/lnd/lnrpc"
"math"
Expand All @@ -17,21 +17,21 @@ type ChannelCleaner struct {
MaxInactive int `long:"cleaner.maxinactive" description:"After how many days of inactivity a public channel should be force closed"`
MaxInactivePrivate int `long:"cleaner.maxinactiveprivate" description:"After how many days of inactivity a private channel should be force closed"`

lnd lnd.LightningClient
discord discord.NotificationService
lnd lnd.LightningClient
notificationProvider providers.NotificationProvider

ticker *time.Ticker
}

func (cleaner *ChannelCleaner) Init(lnd lnd.LightningClient, discord discord.NotificationService) {
func (cleaner *ChannelCleaner) Init(lnd lnd.LightningClient, notificationProvider providers.NotificationProvider) {
if cleaner.Interval == 0 {
return
}

logger.Info("Starting channel cleaner")

cleaner.lnd = lnd
cleaner.discord = discord
cleaner.notificationProvider = notificationProvider

cleaner.forceCloseChannels()

Expand Down Expand Up @@ -111,5 +111,5 @@ func (cleaner *ChannelCleaner) logClosingChannels(channel *lnrpc.Channel, lastUp
"` because it was inactive for " + strconv.Itoa(lastUpdateDelta) + " days"

logger.Info(message)
_ = cleaner.discord.SendMessage(message)
_ = cleaner.notificationProvider.SendMessage(message)
}
8 changes: 8 additions & 0 deletions cleaner/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ type MockDiscordClient struct{}

var sentMessages []string

func (m *MockDiscordClient) Name() string {
return "Mocked discord"
}

func (m *MockDiscordClient) Init() error {
return nil
}

func (m *MockDiscordClient) SendMessage(message string) error {
sentMessages = append(sentMessages, message)
return nil
Expand Down
8 changes: 5 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package main
import (
"fmt"
"github.com/BoltzExchange/channel-bot/cleaner"
"github.com/BoltzExchange/channel-bot/notifications/providers/discord"
"github.com/BoltzExchange/channel-bot/notifications/providers/mattermost"
"github.com/BoltzExchange/channel-bot/utils"
"os"

"github.com/BoltzExchange/channel-bot/build"
"github.com/BoltzExchange/channel-bot/discord"
"github.com/BoltzExchange/channel-bot/lnd"
"github.com/BoltzExchange/channel-bot/notifications"
"github.com/BurntSushi/toml"
Expand All @@ -27,8 +28,9 @@ type config struct {
Notifications *notifications.ChannelManager `group:"Notification Options"`
ChannelCleaner *cleaner.ChannelCleaner `group:"Channel Cleaner Options"`

Lnd *lnd.LND `group:"LND Options"`
Discord *discord.Discord `group:"Discord Options"`
Lnd *lnd.LND `group:"LND Options"`
Discord *discord.Discord `group:"Discord Options"`
Mattermost *mattermost.Mattermost `group:"Mattermost Options"`

Help *helpOptions `group:"Help Options"`

Expand Down
Loading

0 comments on commit 08928d1

Please sign in to comment.