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.
This commit is contained in:
2025-11-24 00:43:58 +07:00
parent 220ca66546
commit 372d19f840
2 changed files with 33 additions and 31 deletions

View File

@@ -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);
}

View File

@@ -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;
}