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:
@@ -106,37 +106,6 @@ public class UserController : BaseController
|
|||||||
{
|
{
|
||||||
var user = await GetUser();
|
var user = await GetUser();
|
||||||
var updatedUser = await _userService.UpdateTelegramChannel(user, telegramChannel);
|
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);
|
return Ok(updatedUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ public class UserService : IUserService
|
|||||||
private readonly ICacheService _cacheService;
|
private readonly ICacheService _cacheService;
|
||||||
private readonly IGrainFactory? _grainFactory;
|
private readonly IGrainFactory? _grainFactory;
|
||||||
private readonly IWhitelistService _whitelistService;
|
private readonly IWhitelistService _whitelistService;
|
||||||
|
private readonly IWebhookService _webhookService;
|
||||||
private readonly string[] _authorizedAddresses;
|
private readonly string[] _authorizedAddresses;
|
||||||
|
|
||||||
public UserService(
|
public UserService(
|
||||||
@@ -30,6 +31,7 @@ public class UserService : IUserService
|
|||||||
ICacheService cacheService,
|
ICacheService cacheService,
|
||||||
IGrainFactory? grainFactory,
|
IGrainFactory? grainFactory,
|
||||||
IWhitelistService whitelistService,
|
IWhitelistService whitelistService,
|
||||||
|
IWebhookService webhookService,
|
||||||
IConfiguration configuration)
|
IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_evmManager = evmManager;
|
_evmManager = evmManager;
|
||||||
@@ -39,6 +41,7 @@ public class UserService : IUserService
|
|||||||
_cacheService = cacheService;
|
_cacheService = cacheService;
|
||||||
_grainFactory = grainFactory;
|
_grainFactory = grainFactory;
|
||||||
_whitelistService = whitelistService;
|
_whitelistService = whitelistService;
|
||||||
|
_webhookService = webhookService;
|
||||||
|
|
||||||
var authorizedAddressesString = configuration["AUTHORIZED_ADDRESSES"] ?? string.Empty;
|
var authorizedAddressesString = configuration["AUTHORIZED_ADDRESSES"] ?? string.Empty;
|
||||||
_authorizedAddresses = string.IsNullOrEmpty(authorizedAddressesString)
|
_authorizedAddresses = string.IsNullOrEmpty(authorizedAddressesString)
|
||||||
@@ -302,6 +305,36 @@ public class UserService : IUserService
|
|||||||
// Update the telegram channel on the provided user object
|
// Update the telegram channel on the provided user object
|
||||||
user.TelegramChannel = telegramChannel;
|
user.TelegramChannel = telegramChannel;
|
||||||
await _userRepository.SaveOrUpdateUserAsync(user);
|
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;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user