From 372d19f840a2c8fc75935eda7776f085677604b3 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Mon, 24 Nov 2025 00:43:58 +0700 Subject: [PATCH] Refactor welcome message handling to UserService for Telegram channel updates - Moved the welcome message logic from UserController to UserService to centralize user notification handling. - Improved error logging for message sending failures, ensuring better traceability of issues. - Enhanced user experience by maintaining the detailed setup information and friendly greeting in the welcome message. --- .../Controllers/UserController.cs | 31 ----------------- src/Managing.Application/Users/UserService.cs | 33 +++++++++++++++++++ 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/Managing.Api/Controllers/UserController.cs b/src/Managing.Api/Controllers/UserController.cs index a6d504b7..9ac7739a 100644 --- a/src/Managing.Api/Controllers/UserController.cs +++ b/src/Managing.Api/Controllers/UserController.cs @@ -106,37 +106,6 @@ public class UserController : BaseController { var user = await GetUser(); var updatedUser = await _userService.UpdateTelegramChannel(user, telegramChannel); - - // Send welcome message to the newly configured telegram channel - if (!string.IsNullOrEmpty(telegramChannel)) - { - try - { - var welcomeMessage = $"🎉 **Welcome to Kaigen Notifications!**\n\n" + - $"Hello {user.AgentName}! 👋\n\n" + - $"Your Telegram channel has been successfully configured and you're now connected to the Kaigen notification system.\n\n" + - $"📋 **Your Setup Details:**\n" + - $"• 🎯 **Agent Name:** {user.AgentName}\n" + - $"• 📡 **Channel ID:** {telegramChannel}\n" + - $"• ⏰ **Connected:** {DateTime.UtcNow:MMM dd, yyyy • HH:mm:ss} UTC\n\n" + - $"🔔 **What You'll Receive:**\n" + - $"• 📈 Real-time position opens and closes\n" + - $"• 💰 Trading activity updates\n" + - $"• 🤖 Strategy and configuration changes\n" + - $"• ⚠️ Important system alerts\n\n" + - $"✨ **You're all set!** Start receiving your trading notifications right away. If you have any questions, feel free to reach out.\n\n" + - $"Happy trading! 🚀"; - - await _webhookService.SendMessage(welcomeMessage, telegramChannel); - } - catch (Exception ex) - { - // Log the error but don't fail the update operation - Console.WriteLine( - $"Failed to send welcome message to telegram channel {telegramChannel}: {ex.Message}"); - } - } - return Ok(updatedUser); } diff --git a/src/Managing.Application/Users/UserService.cs b/src/Managing.Application/Users/UserService.cs index 20d24ed2..3c0bdeaf 100644 --- a/src/Managing.Application/Users/UserService.cs +++ b/src/Managing.Application/Users/UserService.cs @@ -20,6 +20,7 @@ public class UserService : IUserService private readonly ICacheService _cacheService; private readonly IGrainFactory? _grainFactory; private readonly IWhitelistService _whitelistService; + private readonly IWebhookService _webhookService; private readonly string[] _authorizedAddresses; public UserService( @@ -30,6 +31,7 @@ public class UserService : IUserService ICacheService cacheService, IGrainFactory? grainFactory, IWhitelistService whitelistService, + IWebhookService webhookService, IConfiguration configuration) { _evmManager = evmManager; @@ -39,6 +41,7 @@ public class UserService : IUserService _cacheService = cacheService; _grainFactory = grainFactory; _whitelistService = whitelistService; + _webhookService = webhookService; var authorizedAddressesString = configuration["AUTHORIZED_ADDRESSES"] ?? string.Empty; _authorizedAddresses = string.IsNullOrEmpty(authorizedAddressesString) @@ -302,6 +305,36 @@ public class UserService : IUserService // Update the telegram channel on the provided user object user.TelegramChannel = telegramChannel; await _userRepository.SaveOrUpdateUserAsync(user); + + // Send welcome message to the newly configured telegram channel + if (!string.IsNullOrEmpty(telegramChannel)) + { + try + { + var welcomeMessage = $"🎉 **Welcome to Kaigen Notifications!**\n\n" + + $"Hello {user.AgentName}! 👋\n\n" + + $"Your Telegram channel has been successfully configured and you're now connected to the Kaigen notification system.\n\n" + + $"📋 **Your Setup Details:**\n" + + $"• 🎯 **Agent Name:** {user.AgentName}\n" + + $"• 📡 **Channel ID:** {telegramChannel}\n" + + $"• ⏰ **Connected:** {DateTime.UtcNow:MMM dd, yyyy • HH:mm:ss} UTC\n\n" + + $"🔔 **What You'll Receive:**\n" + + $"• 📈 Real-time position opens and closes\n" + + $"• 💰 Trading activity updates\n" + + $"• 🤖 Strategy and configuration changes\n" + + $"• ⚠️ Important system alerts\n\n" + + $"✨ **You're all set!** Start receiving your trading notifications right away. If you have any questions, feel free to reach out.\n\n" + + $"Happy trading! 🚀"; + + await _webhookService.SendMessage(welcomeMessage, telegramChannel); + } + catch (Exception ex) + { + // Log the error but don't fail the update operation + _logger.LogWarning(ex, "Failed to send welcome message to telegram channel {TelegramChannel}", telegramChannel); + } + } + return user; }