Skip to content

Commit

Permalink
send telegram config update notification (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomendezroyo authored Dec 11, 2024
1 parent bd38164 commit fb0e6da
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
logger.Fatal("Failed to initialize relaysAllowedAdapter: %v", err)
}

apiAdapter := api.NewAPIAdapter(ctx, storageAdapter, relaysUsedAdapter, relaysAllowedAdapter, networkConfig.CORS)
apiAdapter := api.NewAPIAdapter(ctx, storageAdapter, notifierAdapter, relaysUsedAdapter, relaysAllowedAdapter, networkConfig.CORS)
proxyApiAdapter := proxyapi.NewProxyAPIAdapter(networkConfig.CORS, networkConfig.LidoKeysApiUrl)

// Initialize API services
Expand Down
17 changes: 9 additions & 8 deletions internal/adapters/api/api_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ func (h *APIHandler) GetRouter() http.Handler {
}

// NewAPIAdapter initializes the APIHandler and sets up routes with CORS enabled
func NewAPIAdapter(ctx context.Context, storagePort ports.StoragePort, relaysUsedPort ports.RelaysUsedPort, relaysAllowedPort ports.RelaysAllowedPort, allowedOrigins []string) *APIHandler {
func NewAPIAdapter(ctx context.Context, storagePort ports.StoragePort, notifierPort ports.NotifierPort, relaysUsedPort ports.RelaysUsedPort, relaysAllowedPort ports.RelaysAllowedPort, allowedOrigins []string) *APIHandler {
h := &APIHandler{
ctx: ctx,
StoragePort: storagePort,
NotifierPort: notifierPort,
RelaysUsedPort: relaysUsedPort,
RelaysAllowedPort: relaysAllowedPort,
Router: mux.NewRouter(),
Expand Down Expand Up @@ -169,19 +170,19 @@ func (h *APIHandler) UpdateTelegramConfig(w http.ResponseWriter, r *http.Request
return
}

// send test notification to verify the chat id exists
if err := h.NotifierPort.SendNotification("🙋 Test notification"); err != nil {
logger.ErrorWithPrefix("API", "Failed to send test notification: %v", err)
writeErrorResponse(w, "Failed to send test notification", http.StatusInternalServerError)
return
}

if err := h.StoragePort.SaveTelegramConfig(domain.TelegramConfig(req)); err != nil {
logger.ErrorWithPrefix("API", "Failed to update Telegram configuration: %v", err)
writeErrorResponse(w, "Failed to update Telegram configuration", http.StatusInternalServerError)
return
}

// send update notification to verify the chat id exists
if err := h.NotifierPort.SendNotification("🔑 Updated telegram configuration successfully"); err != nil {
logger.ErrorWithPrefix("API", "Failed to send test notification: %v", err)
writeErrorResponse(w, "Failed to send test notification", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
}

Expand Down
9 changes: 0 additions & 9 deletions internal/adapters/notifier/notifier_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,6 @@ func NewNotifierAdapter(ctx context.Context, storageAdapter ports.StoragePort) (
if err == nil {
adapter.Bot = updatedBot
logger.InfoWithPrefix(servicePrefix, "Telegram bot configuration updated successfully.")

// Send a test notification to confirm the new configuration works
testMessage := "🔑 Updated telegram configuration successfully"
testErr := adapter.SendNotification(testMessage)
if testErr != nil {
logger.ErrorWithPrefix(servicePrefix, "Failed to send test notification after configuration update: %v", testErr)
} else {
logger.InfoWithPrefix(servicePrefix, "Test notification sent successfully.")
}
} else {
logger.ErrorWithPrefix(servicePrefix, "Failed to update Telegram bot: %v", err)
adapter.Bot = nil // Disable notifications on failure
Expand Down

0 comments on commit fb0e6da

Please sign in to comment.