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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user