Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement telegram as opt in #52

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,9 @@ func waitForInitialConfig(storageAdapter *storage.Storage) {
if err != nil || len(operatorIds) == 0 {
logger.Info("Waiting for operator IDs to be set...")
} else {
// Check for Telegram config
telegramConfig, err := storageAdapter.GetTelegramConfig()
if err != nil || telegramConfig.Token == "" || telegramConfig.UserID == 0 {
logger.Info("Waiting for Telegram config to be set...")
} else {
// Both operator IDs and Telegram config are set
logger.Info("Operator IDs and Telegram config are set. Proceeding with initialization.")
return
}
// Operator IDs are set
logger.Info("Operator IDs are set. Proceeding with initialization.")
return
}
time.Sleep(2 * time.Second) // Poll every 2 seconds
}
Expand Down Expand Up @@ -106,10 +100,13 @@ func main() {
beaconchainAdapter := beaconchain.NewBeaconchainAdapter(networkConfig.BeaconchainURL)
executionAdapter := execution.NewExecutionAdapter(networkConfig.RpcUrl)
exitValidatorAdapter := exitvalidator.NewExitValidatorAdapter(beaconchainAdapter, networkConfig.SignerUrl)

// Initialize the notifier adapter (Telegram configuration optional)
notifierAdapter, err := notifier.NewNotifierAdapter(ctx, storageAdapter)
if err != nil {
logger.Fatal("Failed to initialize Telegram notifier: %v", err)
logger.Warn("Telegram notifier not initialized: %v", err)
}

csFeeDistributorImplAdapter, err := csfeedistributorimpl.NewCsFeeDistributorImplAdapter(networkConfig.WsURL, networkConfig.CSFeeDistributorAddress)
if err != nil {
logger.Fatal("Failed to initialize CsFeeDistributorImpl adapter: %v", err)
Expand Down
49 changes: 38 additions & 11 deletions internal/adapters/notifier/notifier_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,41 @@ import (
)

type TelegramBot struct {
Bot *tgbotapi.BotAPI
UserID int64
mu sync.RWMutex
Bot *tgbotapi.BotAPI
UserID int64
servicePrefix string
mu sync.RWMutex
}

func NewNotifierAdapter(ctx context.Context, storageAdapter ports.StoragePort) (*TelegramBot, error) {
const servicePrefix = "TelegramNotifier"

// Load the initial configuration for Telegram
initialConfig, err := storageAdapter.GetTelegramConfig()
if err != nil {
return nil, err
logger.WarnWithPrefix(servicePrefix, "Failed to load initial Telegram configuration: %v", err)
// Return a TelegramBot instance without initializing the bot
return &TelegramBot{servicePrefix: servicePrefix}, nil
}

if initialConfig.Token == "" || initialConfig.UserID == 0 {
logger.WarnWithPrefix(servicePrefix, "Telegram configuration is incomplete. Notifications will be disabled.")
// Return a TelegramBot instance without initializing the bot
return &TelegramBot{servicePrefix: servicePrefix}, nil
}

// Initialize the bot with the initial token
bot, err := tgbotapi.NewBotAPI(initialConfig.Token)
if err != nil {
return nil, err
logger.WarnWithPrefix(servicePrefix, "Failed to initialize Telegram bot: %v", err)
// Return a TelegramBot instance without initializing the bot
return &TelegramBot{servicePrefix: servicePrefix}, nil
}

adapter := &TelegramBot{
Bot: bot,
UserID: initialConfig.UserID,
Bot: bot,
UserID: initialConfig.UserID,
servicePrefix: servicePrefix,
}

// Listen for configuration updates in a separate goroutine
Expand All @@ -41,11 +55,17 @@ func NewNotifierAdapter(ctx context.Context, storageAdapter ports.StoragePort) (
adapter.mu.Lock()
adapter.UserID = newConfig.UserID
// Update the bot API instance with the new token
updatedBot, err := tgbotapi.NewBotAPI(newConfig.Token)
if err == nil {
adapter.Bot = updatedBot
if newConfig.Token != "" {
updatedBot, err := tgbotapi.NewBotAPI(newConfig.Token)
if err == nil {
adapter.Bot = updatedBot
logger.InfoWithPrefix(servicePrefix, "Telegram bot configuration updated successfully.")
} else {
logger.ErrorWithPrefix(servicePrefix, "Failed to update Telegram bot: %v", err)
}
} else {
logger.ErrorWithPrefix("Notifier", "Failed to update Telegram bot: %v", err)
logger.WarnWithPrefix(servicePrefix, "Received incomplete Telegram configuration. Notifications will be disabled.")
adapter.Bot = nil // Disable notifications if the new config is invalid
}
adapter.mu.Unlock()
}
Expand All @@ -59,10 +79,17 @@ func (tb *TelegramBot) SendNotification(message string) error {
tb.mu.RLock()
defer tb.mu.RUnlock()

// Check if the bot is initialized
if tb.Bot == nil {
logger.WarnWithPrefix(tb.servicePrefix, "Telegram bot is not initialized. Skipping notification.")
return nil
}

msg := tgbotapi.NewMessage(tb.UserID, message)
_, err := tb.Bot.Send(msg)
if err != nil {
return fmt.Errorf("failed to send Telegram message: %w", err)
}
logger.DebugWithPrefix(tb.servicePrefix, "Notification sent successfully.")
return nil
}