- Add admin PWA push notifications for order management
- Integrate TeleBot customer messaging service
- Add push notification endpoints and VAPID key support
- Implement order status notifications throughout workflow
- Add notification UI components in admin panel
- Create TeleBotMessagingService for customer updates
- Add WebPush configuration to appsettings
- Fix compilation issues (BotStatus, BotContacts DbSet)
- Add comprehensive testing documentation
Features:
- Real-time admin notifications for new orders and status changes
- Customer order progress updates via TeleBot
- Graceful failure handling for notification services
- Test endpoints for notification system validation
🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
218 lines
8.1 KiB
C#
218 lines
8.1 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using LittleShop.Data;
|
|
using LittleShop.Enums;
|
|
using LittleShop.Models;
|
|
|
|
namespace LittleShop.Services;
|
|
|
|
public class TeleBotMessagingService : ITeleBotMessagingService
|
|
{
|
|
private readonly LittleShopContext _context;
|
|
private readonly IConfiguration _configuration;
|
|
private readonly ILogger<TeleBotMessagingService> _logger;
|
|
private readonly HttpClient _httpClient;
|
|
private readonly string? _teleBotApiUrl;
|
|
private readonly string? _teleBotApiKey;
|
|
|
|
public TeleBotMessagingService(
|
|
LittleShopContext context,
|
|
IConfiguration configuration,
|
|
ILogger<TeleBotMessagingService> logger,
|
|
HttpClient httpClient)
|
|
{
|
|
_context = context;
|
|
_configuration = configuration;
|
|
_logger = logger;
|
|
_httpClient = httpClient;
|
|
|
|
_teleBotApiUrl = _configuration["TeleBot:ApiUrl"];
|
|
_teleBotApiKey = _configuration["TeleBot:ApiKey"];
|
|
}
|
|
|
|
public async Task<bool> SendOrderStatusUpdateAsync(Guid orderId, OrderStatus newStatus)
|
|
{
|
|
return newStatus switch
|
|
{
|
|
OrderStatus.PaymentReceived => await SendPaymentConfirmedAsync(orderId),
|
|
OrderStatus.Accepted => await SendOrderAcceptedAsync(orderId),
|
|
OrderStatus.Packing => await SendOrderPackingAsync(orderId),
|
|
OrderStatus.Dispatched => await SendOrderDispatchedAsync(orderId),
|
|
OrderStatus.Delivered => await SendOrderDeliveredAsync(orderId),
|
|
OrderStatus.OnHold => await SendOrderOnHoldAsync(orderId),
|
|
_ => false
|
|
};
|
|
}
|
|
|
|
public async Task<bool> SendPaymentConfirmedAsync(Guid orderId)
|
|
{
|
|
var order = await GetOrderWithCustomerAsync(orderId);
|
|
if (order?.Customer == null) return false;
|
|
|
|
var message = $"💰 *Payment Confirmed!*\n\n" +
|
|
$"Your order #{orderId.ToString()[..8]} has been paid successfully. " +
|
|
$"We'll start processing it shortly.\n\n" +
|
|
$"📦 Total: £{order.TotalAmount:F2}\n" +
|
|
$"⏱️ Expected processing: Within 24 hours";
|
|
|
|
return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message);
|
|
}
|
|
|
|
public async Task<bool> SendOrderAcceptedAsync(Guid orderId)
|
|
{
|
|
var order = await GetOrderWithCustomerAsync(orderId);
|
|
if (order?.Customer == null) return false;
|
|
|
|
var message = $"✅ *Order Accepted!*\n\n" +
|
|
$"Great news! Your order #{orderId.ToString()[..8]} has been accepted " +
|
|
$"and is being prepared for packing.\n\n" +
|
|
$"⏱️ Expected packing: Within 24 hours\n" +
|
|
$"🚚 We'll notify you when it's dispatched";
|
|
|
|
return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message);
|
|
}
|
|
|
|
public async Task<bool> SendOrderPackingAsync(Guid orderId)
|
|
{
|
|
var order = await GetOrderWithCustomerAsync(orderId);
|
|
if (order?.Customer == null) return false;
|
|
|
|
var message = $"📦 *Being Packed!*\n\n" +
|
|
$"Your order #{orderId.ToString()[..8]} is currently being packed with care.\n\n" +
|
|
$"🚚 We'll send tracking details once dispatched.\n" +
|
|
$"⏱️ Expected dispatch: Later today";
|
|
|
|
return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message);
|
|
}
|
|
|
|
public async Task<bool> SendOrderDispatchedAsync(Guid orderId, string? trackingNumber = null)
|
|
{
|
|
var order = await GetOrderWithCustomerAsync(orderId);
|
|
if (order?.Customer == null) return false;
|
|
|
|
var trackingInfo = !string.IsNullOrEmpty(trackingNumber)
|
|
? $"📍 Tracking: `{trackingNumber}`\n"
|
|
: "";
|
|
|
|
var message = $"🚚 *Order Dispatched!*\n\n" +
|
|
$"Your order #{orderId.ToString()[..8]} is on its way!\n\n" +
|
|
$"{trackingInfo}" +
|
|
$"⏱️ Estimated delivery: 1-3 working days\n" +
|
|
$"📍 Track your package for real-time updates";
|
|
|
|
return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message);
|
|
}
|
|
|
|
public async Task<bool> SendOrderDeliveredAsync(Guid orderId)
|
|
{
|
|
var order = await GetOrderWithCustomerAsync(orderId);
|
|
if (order?.Customer == null) return false;
|
|
|
|
var message = $"🎉 *Order Delivered!*\n\n" +
|
|
$"Your order #{orderId.ToString()[..8]} has been delivered successfully!\n\n" +
|
|
$"⭐ Please consider leaving a review using the /review command.\n" +
|
|
$"🛒 Thank you for choosing us for your order!";
|
|
|
|
return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message);
|
|
}
|
|
|
|
public async Task<bool> SendOrderOnHoldAsync(Guid orderId, string? reason = null)
|
|
{
|
|
var order = await GetOrderWithCustomerAsync(orderId);
|
|
if (order?.Customer == null) return false;
|
|
|
|
var reasonText = !string.IsNullOrEmpty(reason)
|
|
? $"\n\n📝 Reason: {reason}"
|
|
: "";
|
|
|
|
var message = $"⏸️ *Order On Hold*\n\n" +
|
|
$"Your order #{orderId.ToString()[..8]} has been temporarily put on hold.{reasonText}\n\n" +
|
|
$"💬 Please contact support if you have any questions.\n" +
|
|
$"⏱️ We'll resolve this as quickly as possible";
|
|
|
|
return await SendTeleBotMessageAsync(order.Customer.TelegramUserId, message);
|
|
}
|
|
|
|
public async Task<bool> SendTestMessageAsync(Guid customerId, string message)
|
|
{
|
|
var customer = await _context.Customers.FindAsync(customerId);
|
|
if (customer == null) return false;
|
|
|
|
var testMessage = $"🧪 *Test Message*\n\n{message}";
|
|
return await SendTeleBotMessageAsync(customer.TelegramUserId, testMessage);
|
|
}
|
|
|
|
public async Task<bool> IsAvailableAsync()
|
|
{
|
|
if (string.IsNullOrEmpty(_teleBotApiUrl) || string.IsNullOrEmpty(_teleBotApiKey))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
var response = await _httpClient.GetAsync($"{_teleBotApiUrl}/health");
|
|
return response.IsSuccessStatusCode;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private async Task<Order?> GetOrderWithCustomerAsync(Guid orderId)
|
|
{
|
|
return await _context.Orders
|
|
.Include(o => o.Customer)
|
|
.FirstOrDefaultAsync(o => o.Id == orderId);
|
|
}
|
|
|
|
private async Task<bool> SendTeleBotMessageAsync(long telegramUserId, string message)
|
|
{
|
|
if (!await IsAvailableAsync())
|
|
{
|
|
_logger.LogWarning("TeleBot API not available, skipping message to user {UserId}", telegramUserId);
|
|
return false;
|
|
}
|
|
|
|
try
|
|
{
|
|
var requestData = new
|
|
{
|
|
userId = telegramUserId,
|
|
message = message,
|
|
parseMode = "Markdown"
|
|
};
|
|
|
|
var json = JsonSerializer.Serialize(requestData);
|
|
var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
// Add API key header
|
|
_httpClient.DefaultRequestHeaders.Clear();
|
|
_httpClient.DefaultRequestHeaders.Add("X-API-Key", _teleBotApiKey);
|
|
|
|
var response = await _httpClient.PostAsync($"{_teleBotApiUrl}/api/messages/send", content);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
_logger.LogInformation("Successfully sent TeleBot message to user {UserId}", telegramUserId);
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
var responseContent = await response.Content.ReadAsStringAsync();
|
|
_logger.LogWarning("Failed to send TeleBot message to user {UserId}: {StatusCode} - {Response}",
|
|
telegramUserId, response.StatusCode, responseContent);
|
|
return false;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error sending TeleBot message to user {UserId}", telegramUserId);
|
|
return false;
|
|
}
|
|
}
|
|
} |