Implement bidirectional customer conversations with customer-based grouping and order tagging
This commit is contained in:
parent
3f4789730c
commit
027a3fd0c4
@ -88,6 +88,21 @@ public static class ServiceCollectionExtensions
|
|||||||
return new RetryPolicyHandler(logger, options.MaxRetryAttempts);
|
return new RetryPolicyHandler(logger, options.MaxRetryAttempts);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
services.AddHttpClient<IMessageService, MessageService>((serviceProvider, client) =>
|
||||||
|
{
|
||||||
|
var options = serviceProvider.GetRequiredService<IOptions<LittleShopClientOptions>>().Value;
|
||||||
|
client.BaseAddress = new Uri(options.BaseUrl);
|
||||||
|
client.Timeout = TimeSpan.FromSeconds(options.TimeoutSeconds);
|
||||||
|
client.DefaultRequestHeaders.Add("Accept", "application/json");
|
||||||
|
})
|
||||||
|
.AddHttpMessageHandler<ErrorHandlingMiddleware>()
|
||||||
|
.AddHttpMessageHandler(serviceProvider =>
|
||||||
|
{
|
||||||
|
var logger = serviceProvider.GetRequiredService<ILogger<RetryPolicyHandler>>();
|
||||||
|
var options = serviceProvider.GetRequiredService<IOptions<LittleShopClientOptions>>().Value;
|
||||||
|
return new RetryPolicyHandler(logger, options.MaxRetryAttempts);
|
||||||
|
});
|
||||||
|
|
||||||
// Register the main client
|
// Register the main client
|
||||||
services.AddScoped<ILittleShopClient, LittleShopClient>();
|
services.AddScoped<ILittleShopClient, LittleShopClient>();
|
||||||
|
|
||||||
|
|||||||
@ -10,6 +10,7 @@ public interface ILittleShopClient
|
|||||||
ICatalogService Catalog { get; }
|
ICatalogService Catalog { get; }
|
||||||
IOrderService Orders { get; }
|
IOrderService Orders { get; }
|
||||||
ICustomerService Customers { get; }
|
ICustomerService Customers { get; }
|
||||||
|
IMessageService Messages { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LittleShopClient : ILittleShopClient
|
public class LittleShopClient : ILittleShopClient
|
||||||
@ -18,16 +19,19 @@ public class LittleShopClient : ILittleShopClient
|
|||||||
public ICatalogService Catalog { get; }
|
public ICatalogService Catalog { get; }
|
||||||
public IOrderService Orders { get; }
|
public IOrderService Orders { get; }
|
||||||
public ICustomerService Customers { get; }
|
public ICustomerService Customers { get; }
|
||||||
|
public IMessageService Messages { get; }
|
||||||
|
|
||||||
public LittleShopClient(
|
public LittleShopClient(
|
||||||
IAuthenticationService authenticationService,
|
IAuthenticationService authenticationService,
|
||||||
ICatalogService catalogService,
|
ICatalogService catalogService,
|
||||||
IOrderService orderService,
|
IOrderService orderService,
|
||||||
ICustomerService customerService)
|
ICustomerService customerService,
|
||||||
|
IMessageService messageService)
|
||||||
{
|
{
|
||||||
Authentication = authenticationService;
|
Authentication = authenticationService;
|
||||||
Catalog = catalogService;
|
Catalog = catalogService;
|
||||||
Orders = orderService;
|
Orders = orderService;
|
||||||
Customers = customerService;
|
Customers = customerService;
|
||||||
|
Messages = messageService;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
24
LittleShop.Client/Models/CustomerMessage.cs
Normal file
24
LittleShop.Client/Models/CustomerMessage.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
namespace LittleShop.Client.Models;
|
||||||
|
|
||||||
|
public class CustomerMessage
|
||||||
|
{
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public Guid CustomerId { get; set; }
|
||||||
|
public long TelegramUserId { get; set; }
|
||||||
|
public string Subject { get; set; } = string.Empty;
|
||||||
|
public string Content { get; set; } = string.Empty;
|
||||||
|
public MessageType Type { get; set; }
|
||||||
|
public bool IsUrgent { get; set; }
|
||||||
|
public string? OrderReference { get; set; }
|
||||||
|
public DateTime CreatedAt { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum MessageType
|
||||||
|
{
|
||||||
|
OrderUpdate = 0,
|
||||||
|
PaymentReminder = 1,
|
||||||
|
ShippingInfo = 2,
|
||||||
|
CustomerService = 3,
|
||||||
|
Marketing = 4,
|
||||||
|
SystemAlert = 5
|
||||||
|
}
|
||||||
11
LittleShop.Client/Services/IMessageService.cs
Normal file
11
LittleShop.Client/Services/IMessageService.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using LittleShop.Client.Models;
|
||||||
|
|
||||||
|
namespace LittleShop.Client.Services;
|
||||||
|
|
||||||
|
public interface IMessageService
|
||||||
|
{
|
||||||
|
Task<List<CustomerMessage>> GetPendingMessagesAsync(string platform = "Telegram");
|
||||||
|
Task<bool> MarkMessageAsSentAsync(Guid messageId, string? platformMessageId = null);
|
||||||
|
Task<bool> MarkMessageAsFailedAsync(Guid messageId, string reason);
|
||||||
|
Task<bool> CreateCustomerMessageAsync(object messageData);
|
||||||
|
}
|
||||||
87
LittleShop.Client/Services/MessageService.cs
Normal file
87
LittleShop.Client/Services/MessageService.cs
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
using System.Net.Http.Json;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using LittleShop.Client.Models;
|
||||||
|
|
||||||
|
namespace LittleShop.Client.Services;
|
||||||
|
|
||||||
|
public class MessageService : IMessageService
|
||||||
|
{
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
private readonly ILogger<MessageService> _logger;
|
||||||
|
|
||||||
|
public MessageService(HttpClient httpClient, ILogger<MessageService> logger)
|
||||||
|
{
|
||||||
|
_httpClient = httpClient;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<List<CustomerMessage>> GetPendingMessagesAsync(string platform = "Telegram")
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = await _httpClient.GetAsync($"api/bot/messages/pending?platform={platform}");
|
||||||
|
|
||||||
|
if (response.IsSuccessStatusCode)
|
||||||
|
{
|
||||||
|
var messages = await response.Content.ReadFromJsonAsync<List<CustomerMessage>>();
|
||||||
|
return messages ?? new List<CustomerMessage>();
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogWarning("Failed to get pending messages: {StatusCode}", response.StatusCode);
|
||||||
|
return new List<CustomerMessage>();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error getting pending messages");
|
||||||
|
return new List<CustomerMessage>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> MarkMessageAsSentAsync(Guid messageId, string? platformMessageId = null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var url = $"api/bot/messages/{messageId}/mark-sent";
|
||||||
|
if (!string.IsNullOrEmpty(platformMessageId))
|
||||||
|
{
|
||||||
|
url += $"?platformMessageId={platformMessageId}";
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = await _httpClient.PostAsync(url, null);
|
||||||
|
return response.IsSuccessStatusCode;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error marking message {MessageId} as sent", messageId);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> MarkMessageAsFailedAsync(Guid messageId, string reason)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = await _httpClient.PostAsJsonAsync($"api/bot/messages/{messageId}/mark-failed", reason);
|
||||||
|
return response.IsSuccessStatusCode;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error marking message {MessageId} as failed", messageId);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> CreateCustomerMessageAsync(object messageData)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var response = await _httpClient.PostAsJsonAsync("api/bot/messages/customer-create", messageData);
|
||||||
|
return response.IsSuccessStatusCode;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error creating customer message");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
72
LittleShop/Areas/Admin/Controllers/MessagesController.cs
Normal file
72
LittleShop/Areas/Admin/Controllers/MessagesController.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using LittleShop.DTOs;
|
||||||
|
using LittleShop.Services;
|
||||||
|
using LittleShop.Models;
|
||||||
|
|
||||||
|
namespace LittleShop.Areas.Admin.Controllers;
|
||||||
|
|
||||||
|
[Area("Admin")]
|
||||||
|
[Authorize(Policy = "AdminOnly")]
|
||||||
|
public class MessagesController : Controller
|
||||||
|
{
|
||||||
|
private readonly ICustomerMessageService _messageService;
|
||||||
|
private readonly ILogger<MessagesController> _logger;
|
||||||
|
|
||||||
|
public MessagesController(ICustomerMessageService messageService, ILogger<MessagesController> logger)
|
||||||
|
{
|
||||||
|
_messageService = messageService;
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> Index()
|
||||||
|
{
|
||||||
|
var threads = await _messageService.GetActiveThreadsAsync();
|
||||||
|
return View(threads);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> Customer(Guid id)
|
||||||
|
{
|
||||||
|
var conversation = await _messageService.GetMessageThreadAsync(id);
|
||||||
|
if (conversation == null)
|
||||||
|
{
|
||||||
|
return NotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return View(conversation);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<IActionResult> Reply(Guid customerId, string content, bool isUrgent = false)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var createMessageDto = new CreateCustomerMessageDto
|
||||||
|
{
|
||||||
|
CustomerId = customerId,
|
||||||
|
Type = MessageType.CustomerService,
|
||||||
|
Subject = "Support Reply", // Simple subject for admin replies
|
||||||
|
Content = content,
|
||||||
|
IsUrgent = isUrgent,
|
||||||
|
Priority = isUrgent ? 1 : 5
|
||||||
|
};
|
||||||
|
|
||||||
|
var message = await _messageService.CreateMessageAsync(createMessageDto);
|
||||||
|
if (message != null)
|
||||||
|
{
|
||||||
|
TempData["Success"] = "Reply sent successfully!";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
TempData["Error"] = "Failed to send reply.";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error sending reply");
|
||||||
|
TempData["Error"] = "An error occurred sending the reply.";
|
||||||
|
}
|
||||||
|
|
||||||
|
return RedirectToAction("Customer", new { id = customerId });
|
||||||
|
}
|
||||||
|
}
|
||||||
149
LittleShop/Areas/Admin/Views/Messages/Customer.cshtml
Normal file
149
LittleShop/Areas/Admin/Views/Messages/Customer.cshtml
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
@model LittleShop.DTOs.MessageThreadDto
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = $"Conversation with {Model.CustomerName}";
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||||
|
<h2><i class="fas fa-comments"></i> Conversation with @Model.CustomerName</h2>
|
||||||
|
<a href="@Url.Action("Index")" class="btn btn-secondary">
|
||||||
|
<i class="fas fa-arrow-left"></i> Back to Messages
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if (TempData["Success"] != null)
|
||||||
|
{
|
||||||
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||||
|
@TempData["Success"]
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
@if (TempData["Error"] != null)
|
||||||
|
{
|
||||||
|
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
||||||
|
@TempData["Error"]
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-8">
|
||||||
|
<!-- Conversation Thread -->
|
||||||
|
<div class="card mb-4">
|
||||||
|
<div class="card-header">
|
||||||
|
<h5>Conversation with @Model.CustomerName</h5>
|
||||||
|
<small class="text-muted">
|
||||||
|
@Model.MessageCount messages since @Model.StartedAt.ToString("MMM dd, yyyy")
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
<div class="card-body" style="max-height: 500px; overflow-y: auto;">
|
||||||
|
@foreach (var message in Model.Messages.OrderBy(m => m.CreatedAt))
|
||||||
|
{
|
||||||
|
<div class="mb-3 @(message.Direction == 0 ? "ms-4" : "me-4")">
|
||||||
|
<div class="d-flex @(message.Direction == 0 ? "justify-content-end" : "justify-content-start")">
|
||||||
|
<div class="card @(message.Direction == 0 ? "bg-primary text-white" : "bg-light") @(message.Direction == 0 ? "ms-auto" : "me-auto")" style="max-width: 70%;">
|
||||||
|
<div class="card-body p-3">
|
||||||
|
<div class="d-flex justify-content-between align-items-start mb-2">
|
||||||
|
<strong class="@(message.Direction == 0 ? "text-white" : "text-primary")">
|
||||||
|
@if (message.Direction == 0)
|
||||||
|
{
|
||||||
|
<i class="fas fa-user-tie"></i>
|
||||||
|
@:Admin
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<i class="fas fa-user"></i>
|
||||||
|
@Model.CustomerName
|
||||||
|
}
|
||||||
|
</strong>
|
||||||
|
<small class="@(message.Direction == 0 ? "text-white-50" : "text-muted")">
|
||||||
|
@message.CreatedAt.ToString("MMM dd, HH:mm")
|
||||||
|
</small>
|
||||||
|
</div>
|
||||||
|
<div class="@(message.Direction == 0 ? "text-white" : "text-dark")">
|
||||||
|
@Html.Raw(message.Content.Replace("\n", "<br>"))
|
||||||
|
</div>
|
||||||
|
@if (!string.IsNullOrEmpty(message.OrderReference))
|
||||||
|
{
|
||||||
|
<div class="mt-2">
|
||||||
|
<a href="@Url.Action("Details", "Orders", new { id = message.OrderId })"
|
||||||
|
class="badge @(message.Direction == 0 ? "bg-light text-primary" : "bg-primary text-white") text-decoration-none">
|
||||||
|
<i class="fas fa-box"></i> Order @message.OrderReference
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
@if (message.IsUrgent)
|
||||||
|
{
|
||||||
|
<span class="badge bg-danger mt-2">Urgent</span>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="col-md-4">
|
||||||
|
<!-- Customer Info -->
|
||||||
|
<div class="card mb-4">
|
||||||
|
<div class="card-header">
|
||||||
|
<h6><i class="fas fa-user"></i> Customer Information</h6>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<p><strong>Name:</strong> @Model.CustomerName</p>
|
||||||
|
<p><strong>Messages:</strong> @Model.MessageCount</p>
|
||||||
|
<p><strong>First Contact:</strong> @Model.StartedAt.ToString("MMM dd, yyyy HH:mm")</p>
|
||||||
|
<p><strong>Last Message:</strong> @Model.LastMessageAt.ToString("MMM dd, yyyy HH:mm")</p>
|
||||||
|
@if (Model.HasUnreadMessages)
|
||||||
|
{
|
||||||
|
<div class="alert alert-warning p-2">
|
||||||
|
<i class="fas fa-exclamation-triangle"></i> Has unread messages
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
@if (Model.RequiresResponse)
|
||||||
|
{
|
||||||
|
<div class="alert alert-danger p-2">
|
||||||
|
<i class="fas fa-reply"></i> Requires response
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Reply Form -->
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<h6><i class="fas fa-reply"></i> Send Reply</h6>
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<form method="post" action="@Url.Action("Reply")">
|
||||||
|
<input type="hidden" name="customerId" value="@Model.CustomerId" />
|
||||||
|
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="content" class="form-label">Message</label>
|
||||||
|
<textarea class="form-control" id="content" name="content" rows="4" required placeholder="Type your reply here..."></textarea>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="mb-3 form-check">
|
||||||
|
<input type="checkbox" class="form-check-input" id="isUrgent" name="isUrgent" value="true">
|
||||||
|
<label class="form-check-label" for="isUrgent">
|
||||||
|
Mark as urgent
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button type="submit" class="btn btn-primary">
|
||||||
|
<i class="fas fa-paper-plane"></i> Send Reply
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
86
LittleShop/Areas/Admin/Views/Messages/Index.cshtml
Normal file
86
LittleShop/Areas/Admin/Views/Messages/Index.cshtml
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
@model IEnumerable<LittleShop.DTOs.MessageThreadDto>
|
||||||
|
|
||||||
|
@{
|
||||||
|
ViewData["Title"] = "Customer Messages";
|
||||||
|
}
|
||||||
|
|
||||||
|
<div class="container-fluid">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||||
|
<h2><i class="fas fa-comments"></i> Customer Messages</h2>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
@if (!Model.Any())
|
||||||
|
{
|
||||||
|
<div class="alert alert-info">
|
||||||
|
<i class="fas fa-info-circle"></i> No customer messages yet.
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
<h5>Active Conversations</h5>
|
||||||
|
</div>
|
||||||
|
<div class="card-body p-0">
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-striped table-hover mb-0">
|
||||||
|
<thead class="table-dark">
|
||||||
|
<tr>
|
||||||
|
<th>Customer</th>
|
||||||
|
<th>Subject</th>
|
||||||
|
<th>Last Message</th>
|
||||||
|
<th>Messages</th>
|
||||||
|
<th>Status</th>
|
||||||
|
<th>Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@foreach (var thread in Model.OrderByDescending(t => t.LastMessageAt))
|
||||||
|
{
|
||||||
|
<tr class="@(thread.HasUnreadMessages ? "table-warning" : "")">
|
||||||
|
<td>
|
||||||
|
<strong>@thread.CustomerName</strong>
|
||||||
|
<br><small class="text-muted">Customer conversation</small>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<span class="text-muted">Latest activity</span>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
@thread.LastMessageAt.ToString("MMM dd, HH:mm")
|
||||||
|
<br><small class="text-muted">Started: @thread.StartedAt.ToString("MMM dd")</small>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<span class="badge bg-primary">@thread.MessageCount</span>
|
||||||
|
@if (thread.HasUnreadMessages)
|
||||||
|
{
|
||||||
|
<span class="badge bg-warning ms-1">Unread</span>
|
||||||
|
}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
@if (thread.RequiresResponse)
|
||||||
|
{
|
||||||
|
<span class="badge bg-danger">Needs Response</span>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<span class="badge bg-success">Up to date</span>
|
||||||
|
}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<a href="@Url.Action("Customer", new { id = thread.CustomerId })" class="btn btn-sm btn-primary">
|
||||||
|
<i class="fas fa-comments"></i> View Conversation
|
||||||
|
</a>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@ -12,8 +12,11 @@
|
|||||||
<div class="col-auto">
|
<div class="col-auto">
|
||||||
@if (Model.Customer != null)
|
@if (Model.Customer != null)
|
||||||
{
|
{
|
||||||
<button class="btn btn-success" onclick="showMessageModal('@Model.Id', '@Model.Customer.DisplayName')">
|
<a href="@Url.Action("Customer", "Messages", new { area = "Admin", id = Model.Customer.Id })" class="btn btn-success">
|
||||||
<i class="fas fa-comment"></i> Message Customer
|
<i class="fas fa-comments"></i> View Conversation
|
||||||
|
</a>
|
||||||
|
<button type="button" class="btn btn-primary" onclick="showMessageModal('@Model.Id', '@Model.Customer.DisplayName')">
|
||||||
|
<i class="fas fa-comment"></i> Send Order Update
|
||||||
</button>
|
</button>
|
||||||
}
|
}
|
||||||
<a href="@Url.Action("Edit", new { id = Model.Id })" class="btn btn-primary">
|
<a href="@Url.Action("Edit", new { id = Model.Id })" class="btn btn-primary">
|
||||||
|
|||||||
@ -40,6 +40,11 @@
|
|||||||
<i class="fas fa-shopping-cart"></i> Orders
|
<i class="fas fa-shopping-cart"></i> Orders
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="@Url.Action("Index", "Messages", new { area = "Admin" })">
|
||||||
|
<i class="fas fa-comments"></i> Messages
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
<li class="nav-item">
|
<li class="nav-item">
|
||||||
<a class="nav-link" href="@Url.Action("Index", "ShippingRates", new { area = "Admin" })">
|
<a class="nav-link" href="@Url.Action("Index", "ShippingRates", new { area = "Admin" })">
|
||||||
<i class="fas fa-truck"></i> Shipping
|
<i class="fas fa-truck"></i> Shipping
|
||||||
|
|||||||
@ -72,6 +72,44 @@ public class BotMessagesController : ControllerBase
|
|||||||
|
|
||||||
return Ok(message);
|
return Ok(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("customer-create")]
|
||||||
|
public async Task<ActionResult> CreateCustomerMessage([FromBody] CreateCustomerMessageFromTelegramDto dto)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Create CustomerToAdmin message directly
|
||||||
|
var message = new CustomerMessage
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(),
|
||||||
|
CustomerId = dto.CustomerId,
|
||||||
|
OrderId = dto.OrderId,
|
||||||
|
Direction = MessageDirection.CustomerToAdmin,
|
||||||
|
Type = dto.Type,
|
||||||
|
Subject = dto.Subject,
|
||||||
|
Content = dto.Content,
|
||||||
|
Status = MessageStatus.Read, // Customer messages are immediately "read" by system
|
||||||
|
Platform = "Telegram",
|
||||||
|
Priority = dto.Priority,
|
||||||
|
IsUrgent = dto.IsUrgent,
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
ThreadId = Guid.NewGuid() // Will be updated if part of existing thread
|
||||||
|
};
|
||||||
|
|
||||||
|
// Save directly to database (bypass the regular CreateMessageAsync which is for AdminToCustomer)
|
||||||
|
var result = await _messageService.CreateCustomerToAdminMessageAsync(message);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return BadRequest("Failed to create customer message");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest($"Error creating customer message: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEMPORARY DTO FOR TESTING
|
// TEMPORARY DTO FOR TESTING
|
||||||
@ -85,3 +123,15 @@ public class CreateTestMessageDto
|
|||||||
public int Priority { get; set; } = 5;
|
public int Priority { get; set; } = 5;
|
||||||
public bool IsUrgent { get; set; } = false;
|
public bool IsUrgent { get; set; } = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DTO for customer messages from Telegram
|
||||||
|
public class CreateCustomerMessageFromTelegramDto
|
||||||
|
{
|
||||||
|
public Guid CustomerId { get; set; }
|
||||||
|
public Guid? OrderId { get; set; }
|
||||||
|
public MessageType Type { get; set; }
|
||||||
|
public string Subject { get; set; } = string.Empty;
|
||||||
|
public string Content { get; set; } = string.Empty;
|
||||||
|
public int Priority { get; set; } = 5;
|
||||||
|
public bool IsUrgent { get; set; } = false;
|
||||||
|
}
|
||||||
@ -30,18 +30,8 @@ public class CustomerMessageService : ICustomerMessageService
|
|||||||
message.Status = MessageStatus.Pending;
|
message.Status = MessageStatus.Pending;
|
||||||
message.Platform = "Telegram";
|
message.Platform = "Telegram";
|
||||||
|
|
||||||
// Generate thread ID if this is a new conversation
|
// Use customer-based threading - all messages for a customer are in one conversation
|
||||||
if (message.ParentMessageId == null)
|
message.ThreadId = message.CustomerId;
|
||||||
{
|
|
||||||
message.ThreadId = Guid.NewGuid();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Get parent message's thread ID
|
|
||||||
var parentMessage = await _context.CustomerMessages
|
|
||||||
.FirstOrDefaultAsync(m => m.Id == message.ParentMessageId);
|
|
||||||
message.ThreadId = parentMessage?.ThreadId ?? Guid.NewGuid();
|
|
||||||
}
|
|
||||||
|
|
||||||
_context.CustomerMessages.Add(message);
|
_context.CustomerMessages.Add(message);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
@ -161,28 +151,29 @@ public class CustomerMessageService : ICustomerMessageService
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<MessageThreadDto?> GetMessageThreadAsync(Guid threadId)
|
public async Task<MessageThreadDto?> GetMessageThreadAsync(Guid customerId)
|
||||||
{
|
{
|
||||||
|
// Get all messages for this customer (customer-based conversation)
|
||||||
var messages = await _context.CustomerMessages
|
var messages = await _context.CustomerMessages
|
||||||
.Include(m => m.Customer)
|
.Include(m => m.Customer)
|
||||||
.Include(m => m.Order)
|
.Include(m => m.Order)
|
||||||
.Include(m => m.AdminUser)
|
.Include(m => m.AdminUser)
|
||||||
.Where(m => m.ThreadId == threadId)
|
.Where(m => m.CustomerId == customerId && !m.IsArchived)
|
||||||
.OrderBy(m => m.CreatedAt)
|
.OrderBy(m => m.CreatedAt)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
if (!messages.Any()) return null;
|
if (!messages.Any()) return null;
|
||||||
|
|
||||||
var firstMessage = messages.First();
|
var customer = messages.First().Customer;
|
||||||
var thread = new MessageThreadDto
|
var thread = new MessageThreadDto
|
||||||
{
|
{
|
||||||
ThreadId = threadId,
|
ThreadId = customerId, // Use CustomerId as thread identifier
|
||||||
Subject = firstMessage.Subject,
|
Subject = customer?.DisplayName ?? "Unknown Customer", // Customer name as subject
|
||||||
CustomerId = firstMessage.CustomerId,
|
CustomerId = customerId,
|
||||||
CustomerName = firstMessage.Customer?.DisplayName ?? "Unknown",
|
CustomerName = customer?.DisplayName ?? "Unknown",
|
||||||
OrderId = firstMessage.OrderId,
|
OrderId = null, // No single order - conversation spans multiple orders
|
||||||
OrderReference = firstMessage.Order?.Id.ToString().Substring(0, 8),
|
OrderReference = null, // No single order reference
|
||||||
StartedAt = firstMessage.CreatedAt,
|
StartedAt = messages.Min(m => m.CreatedAt),
|
||||||
LastMessageAt = messages.Max(m => m.CreatedAt),
|
LastMessageAt = messages.Max(m => m.CreatedAt),
|
||||||
MessageCount = messages.Count,
|
MessageCount = messages.Count,
|
||||||
HasUnreadMessages = messages.Any(m => m.Direction == MessageDirection.CustomerToAdmin && m.Status != MessageStatus.Read),
|
HasUnreadMessages = messages.Any(m => m.Direction == MessageDirection.CustomerToAdmin && m.Status != MessageStatus.Read),
|
||||||
@ -195,19 +186,20 @@ public class CustomerMessageService : ICustomerMessageService
|
|||||||
|
|
||||||
public async Task<IEnumerable<MessageThreadDto>> GetActiveThreadsAsync()
|
public async Task<IEnumerable<MessageThreadDto>> GetActiveThreadsAsync()
|
||||||
{
|
{
|
||||||
|
// Group by Customer instead of ThreadId for customer-based conversations
|
||||||
var threads = await _context.CustomerMessages
|
var threads = await _context.CustomerMessages
|
||||||
.Include(m => m.Customer)
|
.Include(m => m.Customer)
|
||||||
.Include(m => m.Order)
|
.Include(m => m.Order)
|
||||||
.Where(m => !m.IsArchived)
|
.Where(m => !m.IsArchived)
|
||||||
.GroupBy(m => m.ThreadId)
|
.GroupBy(m => m.CustomerId)
|
||||||
.Select(g => new MessageThreadDto
|
.Select(g => new MessageThreadDto
|
||||||
{
|
{
|
||||||
ThreadId = g.Key ?? Guid.Empty,
|
ThreadId = g.First().CustomerId, // Use CustomerId as conversation identifier
|
||||||
Subject = g.First().Subject,
|
Subject = g.First().Customer != null ? g.First().Customer.DisplayName : "Unknown Customer",
|
||||||
CustomerId = g.First().CustomerId,
|
CustomerId = g.First().CustomerId,
|
||||||
CustomerName = g.First().Customer != null ? g.First().Customer.DisplayName : "Unknown",
|
CustomerName = g.First().Customer != null ? g.First().Customer.DisplayName : "Unknown",
|
||||||
OrderId = g.First().OrderId,
|
OrderId = null, // No single order - will show multiple orders in thread
|
||||||
OrderReference = g.First().Order != null ? g.First().Order.Id.ToString().Substring(0, 8) : null,
|
OrderReference = null, // No single order reference
|
||||||
StartedAt = g.Min(m => m.CreatedAt),
|
StartedAt = g.Min(m => m.CreatedAt),
|
||||||
LastMessageAt = g.Max(m => m.CreatedAt),
|
LastMessageAt = g.Max(m => m.CreatedAt),
|
||||||
MessageCount = g.Count(),
|
MessageCount = g.Count(),
|
||||||
@ -230,4 +222,26 @@ public class CustomerMessageService : ICustomerMessageService
|
|||||||
{
|
{
|
||||||
return await _context.Orders.AnyAsync(o => o.Id == orderId && o.CustomerId == customerId);
|
return await _context.Orders.AnyAsync(o => o.Id == orderId && o.CustomerId == customerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> CreateCustomerToAdminMessageAsync(CustomerMessage message)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Use customer-based threading for all messages
|
||||||
|
message.ThreadId = message.CustomerId;
|
||||||
|
|
||||||
|
_context.CustomerMessages.Add(message);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("Created customer message {MessageId} from customer {CustomerId}",
|
||||||
|
message.Id, message.CustomerId);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error creating customer message from customer {CustomerId}", message.CustomerId);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -17,4 +17,5 @@ public interface ICustomerMessageService
|
|||||||
Task<IEnumerable<MessageThreadDto>> GetActiveThreadsAsync();
|
Task<IEnumerable<MessageThreadDto>> GetActiveThreadsAsync();
|
||||||
Task<bool> ValidateCustomerExistsAsync(Guid customerId);
|
Task<bool> ValidateCustomerExistsAsync(Guid customerId);
|
||||||
Task<bool> ValidateOrderBelongsToCustomerAsync(Guid orderId, Guid customerId);
|
Task<bool> ValidateOrderBelongsToCustomerAsync(Guid orderId, Guid customerId);
|
||||||
|
Task<bool> CreateCustomerToAdminMessageAsync(CustomerMessage message);
|
||||||
}
|
}
|
||||||
Binary file not shown.
Binary file not shown.
@ -124,10 +124,18 @@ namespace TeleBot.Handlers
|
|||||||
await HandleHelp(bot, callbackQuery.Message);
|
await HandleHelp(bot, callbackQuery.Message);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "support":
|
||||||
|
await HandleSupportCallback(bot, callbackQuery, session);
|
||||||
|
break;
|
||||||
|
|
||||||
case "noop":
|
case "noop":
|
||||||
// No operation - used for display-only buttons
|
// No operation - used for display-only buttons
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "cancel_support":
|
||||||
|
await HandleCancelSupport(bot, callbackQuery, session);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
_logger.LogWarning("Unknown callback action: {Action}", action);
|
_logger.LogWarning("Unknown callback action: {Action}", action);
|
||||||
break;
|
break;
|
||||||
@ -592,5 +600,36 @@ namespace TeleBot.Handlers
|
|||||||
replyMarkup: MenuBuilder.MainMenu()
|
replyMarkup: MenuBuilder.MainMenu()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task HandleSupportCallback(ITelegramBotClient bot, CallbackQuery callbackQuery, UserSession session)
|
||||||
|
{
|
||||||
|
session.State = SessionState.CustomerSupport;
|
||||||
|
|
||||||
|
await bot.EditMessageTextAsync(
|
||||||
|
callbackQuery.Message!.Chat.Id,
|
||||||
|
callbackQuery.Message.MessageId,
|
||||||
|
"🎧 *Customer Support*\n\n" +
|
||||||
|
"You can now send a message to our support team. Simply type your message and we'll respond as soon as possible.\n\n" +
|
||||||
|
"_Type your message below, or use the Cancel button to return to the main menu._",
|
||||||
|
parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown,
|
||||||
|
replyMarkup: MenuBuilder.SupportMenu()
|
||||||
|
);
|
||||||
|
|
||||||
|
await bot.AnswerCallbackQueryAsync(callbackQuery.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task HandleCancelSupport(ITelegramBotClient bot, CallbackQuery callbackQuery, UserSession session)
|
||||||
|
{
|
||||||
|
session.State = SessionState.MainMenu;
|
||||||
|
|
||||||
|
await bot.EditMessageTextAsync(
|
||||||
|
callbackQuery.Message!.Chat.Id,
|
||||||
|
callbackQuery.Message.MessageId,
|
||||||
|
"Customer support cancelled. How can I help you today?",
|
||||||
|
replyMarkup: MenuBuilder.MainMenu()
|
||||||
|
);
|
||||||
|
|
||||||
|
await bot.AnswerCallbackQueryAsync(callbackQuery.Id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,6 +84,14 @@ namespace TeleBot.Handlers
|
|||||||
await HandleClearCommand(bot, message, session);
|
await HandleClearCommand(bot, message, session);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case "/support":
|
||||||
|
await HandleSupportCommand(bot, message, session);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "/cancel":
|
||||||
|
await HandleCancelCommand(bot, message, session);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
await bot.SendTextMessageAsync(
|
await bot.SendTextMessageAsync(
|
||||||
message.Chat.Id,
|
message.Chat.Id,
|
||||||
@ -297,5 +305,30 @@ namespace TeleBot.Handlers
|
|||||||
replyMarkup: MenuBuilder.MainMenu()
|
replyMarkup: MenuBuilder.MainMenu()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task HandleSupportCommand(ITelegramBotClient bot, Message message, Models.UserSession session)
|
||||||
|
{
|
||||||
|
session.State = Models.SessionState.CustomerSupport;
|
||||||
|
|
||||||
|
await bot.SendTextMessageAsync(
|
||||||
|
message.Chat.Id,
|
||||||
|
"🎧 *Customer Support*\n\n" +
|
||||||
|
"You can now send a message to our support team. Simply type your message and we'll respond as soon as possible.\n\n" +
|
||||||
|
"_Type your message below, or use /cancel to return to the main menu._",
|
||||||
|
parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown,
|
||||||
|
replyMarkup: MenuBuilder.SupportMenu()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task HandleCancelCommand(ITelegramBotClient bot, Message message, Models.UserSession session)
|
||||||
|
{
|
||||||
|
session.State = Models.SessionState.MainMenu;
|
||||||
|
|
||||||
|
await bot.SendTextMessageAsync(
|
||||||
|
message.Chat.Id,
|
||||||
|
"Operation cancelled. How can I help you today?",
|
||||||
|
replyMarkup: MenuBuilder.MainMenu()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -18,13 +18,16 @@ namespace TeleBot.Handlers
|
|||||||
{
|
{
|
||||||
private readonly ISessionManager _sessionManager;
|
private readonly ISessionManager _sessionManager;
|
||||||
private readonly ILogger<MessageHandler> _logger;
|
private readonly ILogger<MessageHandler> _logger;
|
||||||
|
private readonly ILittleShopService _shopService;
|
||||||
|
|
||||||
public MessageHandler(
|
public MessageHandler(
|
||||||
ISessionManager sessionManager,
|
ISessionManager sessionManager,
|
||||||
ILogger<MessageHandler> logger)
|
ILogger<MessageHandler> logger,
|
||||||
|
ILittleShopService shopService)
|
||||||
{
|
{
|
||||||
_sessionManager = sessionManager;
|
_sessionManager = sessionManager;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
_shopService = shopService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task HandleMessageAsync(ITelegramBotClient bot, Message message)
|
public async Task HandleMessageAsync(ITelegramBotClient bot, Message message)
|
||||||
@ -41,6 +44,11 @@ namespace TeleBot.Handlers
|
|||||||
{
|
{
|
||||||
await HandleCheckoutInput(bot, message, session);
|
await HandleCheckoutInput(bot, message, session);
|
||||||
}
|
}
|
||||||
|
// Handle customer support messages
|
||||||
|
else if (session.State == SessionState.CustomerSupport)
|
||||||
|
{
|
||||||
|
await HandleCustomerSupportMessage(bot, message, session);
|
||||||
|
}
|
||||||
else if (message.Text.StartsWith("/pgpkey "))
|
else if (message.Text.StartsWith("/pgpkey "))
|
||||||
{
|
{
|
||||||
// Handle PGP key input
|
// Handle PGP key input
|
||||||
@ -221,5 +229,53 @@ namespace TeleBot.Handlers
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task HandleCustomerSupportMessage(ITelegramBotClient bot, Message message, UserSession session)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Send the customer's message to the business
|
||||||
|
var success = await _shopService.SendCustomerMessageAsync(
|
||||||
|
message.From!.Id,
|
||||||
|
message.From.Username ?? "",
|
||||||
|
$"{message.From.FirstName} {message.From.LastName}".Trim(),
|
||||||
|
message.From.FirstName ?? "",
|
||||||
|
message.From.LastName ?? "",
|
||||||
|
"Customer Message", // Simple subject
|
||||||
|
message.Text!
|
||||||
|
);
|
||||||
|
|
||||||
|
if (success)
|
||||||
|
{
|
||||||
|
await bot.SendTextMessageAsync(
|
||||||
|
message.Chat.Id,
|
||||||
|
"✅ *Message sent to support team*\n\n" +
|
||||||
|
"We've received your message and will respond as soon as possible.\n\n" +
|
||||||
|
"You can continue shopping or send additional messages.",
|
||||||
|
parseMode: Telegram.Bot.Types.Enums.ParseMode.Markdown,
|
||||||
|
replyMarkup: MenuBuilder.MainMenu()
|
||||||
|
);
|
||||||
|
|
||||||
|
session.State = SessionState.MainMenu;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await bot.SendTextMessageAsync(
|
||||||
|
message.Chat.Id,
|
||||||
|
"❌ Sorry, there was an error sending your message. Please try again.",
|
||||||
|
replyMarkup: MenuBuilder.SupportMenu()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error handling customer support message");
|
||||||
|
await bot.SendTextMessageAsync(
|
||||||
|
message.Chat.Id,
|
||||||
|
"❌ An error occurred. Please try again or use /cancel to return to the menu.",
|
||||||
|
replyMarkup: MenuBuilder.SupportMenu()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,6 +97,7 @@ namespace TeleBot.Models
|
|||||||
ViewingOrders,
|
ViewingOrders,
|
||||||
ViewingOrder,
|
ViewingOrder,
|
||||||
PrivacySettings,
|
PrivacySettings,
|
||||||
|
CustomerSupport,
|
||||||
Help
|
Help
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,6 +83,11 @@ builder.Services.AddHttpClient<BotManagerService>();
|
|||||||
builder.Services.AddSingleton<BotManagerService>();
|
builder.Services.AddSingleton<BotManagerService>();
|
||||||
builder.Services.AddHostedService<BotManagerService>();
|
builder.Services.AddHostedService<BotManagerService>();
|
||||||
|
|
||||||
|
// Message Delivery Service - Single instance
|
||||||
|
builder.Services.AddSingleton<MessageDeliveryService>();
|
||||||
|
builder.Services.AddSingleton<IMessageDeliveryService>(sp => sp.GetRequiredService<MessageDeliveryService>());
|
||||||
|
builder.Services.AddHostedService<MessageDeliveryService>(sp => sp.GetRequiredService<MessageDeliveryService>());
|
||||||
|
|
||||||
// Bot Service
|
// Bot Service
|
||||||
builder.Services.AddHostedService<TelegramBotService>();
|
builder.Services.AddHostedService<TelegramBotService>();
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ namespace TeleBot.Services
|
|||||||
Task<List<CustomerMessage>?> GetPendingMessagesAsync();
|
Task<List<CustomerMessage>?> GetPendingMessagesAsync();
|
||||||
Task<bool> MarkMessageAsSentAsync(Guid messageId, string? platformMessageId = null);
|
Task<bool> MarkMessageAsSentAsync(Guid messageId, string? platformMessageId = null);
|
||||||
Task<bool> MarkMessageAsFailedAsync(Guid messageId, string reason);
|
Task<bool> MarkMessageAsFailedAsync(Guid messageId, string reason);
|
||||||
|
Task<bool> SendCustomerMessageAsync(long telegramUserId, string telegramUsername, string displayName, string firstName, string lastName, string subject, string content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class LittleShopService : ILittleShopService
|
public class LittleShopService : ILittleShopService
|
||||||
@ -331,30 +332,22 @@ namespace TeleBot.Services
|
|||||||
if (!await AuthenticateAsync())
|
if (!await AuthenticateAsync())
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
// Call API to get pending messages for Telegram platform
|
// Use the client messages service
|
||||||
var response = await _client.HttpClient.GetAsync("api/messages/pending?platform=Telegram");
|
var messages = await _client.Messages.GetPendingMessagesAsync("Telegram");
|
||||||
|
|
||||||
if (response.IsSuccessStatusCode)
|
// Convert to TeleBot CustomerMessage format
|
||||||
|
return messages.Select(m => new CustomerMessage
|
||||||
{
|
{
|
||||||
var messages = await response.Content.ReadFromJsonAsync<List<PendingMessageDto>>();
|
Id = m.Id,
|
||||||
|
CustomerId = m.CustomerId,
|
||||||
// Convert to simplified CustomerMessage format
|
TelegramUserId = m.TelegramUserId,
|
||||||
return messages?.Select(m => new CustomerMessage
|
Subject = m.Subject,
|
||||||
{
|
Content = m.Content,
|
||||||
Id = m.Id,
|
Type = (MessageType)m.Type,
|
||||||
CustomerId = m.CustomerId,
|
IsUrgent = m.IsUrgent,
|
||||||
TelegramUserId = m.TelegramUserId,
|
OrderReference = m.OrderReference,
|
||||||
Subject = m.Subject,
|
CreatedAt = m.CreatedAt
|
||||||
Content = m.Content,
|
}).ToList();
|
||||||
Type = (MessageType)m.Type,
|
|
||||||
IsUrgent = m.IsUrgent,
|
|
||||||
OrderReference = m.OrderReference,
|
|
||||||
CreatedAt = m.CreatedAt
|
|
||||||
}).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.LogWarning("Failed to get pending messages: {StatusCode}", response.StatusCode);
|
|
||||||
return new List<CustomerMessage>();
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -370,14 +363,7 @@ namespace TeleBot.Services
|
|||||||
if (!await AuthenticateAsync())
|
if (!await AuthenticateAsync())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var url = $"api/messages/{messageId}/mark-sent";
|
return await _client.Messages.MarkMessageAsSentAsync(messageId, platformMessageId);
|
||||||
if (!string.IsNullOrEmpty(platformMessageId))
|
|
||||||
{
|
|
||||||
url += $"?platformMessageId={platformMessageId}";
|
|
||||||
}
|
|
||||||
|
|
||||||
var response = await _client.HttpClient.PostAsync(url, null);
|
|
||||||
return response.IsSuccessStatusCode;
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -393,8 +379,7 @@ namespace TeleBot.Services
|
|||||||
if (!await AuthenticateAsync())
|
if (!await AuthenticateAsync())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var response = await _client.HttpClient.PostAsJsonAsync($"api/messages/{messageId}/mark-failed", reason);
|
return await _client.Messages.MarkMessageAsFailedAsync(messageId, reason);
|
||||||
return response.IsSuccessStatusCode;
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -418,5 +403,52 @@ namespace TeleBot.Services
|
|||||||
_ => 0 // Default to BTC
|
_ => 0 // Default to BTC
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<bool> SendCustomerMessageAsync(long telegramUserId, string telegramUsername, string displayName, string firstName, string lastName, string subject, string content)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!await AuthenticateAsync())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// First, get or create the customer
|
||||||
|
var customer = await _client.Customers.GetOrCreateCustomerAsync(new CreateCustomerRequest
|
||||||
|
{
|
||||||
|
TelegramUserId = telegramUserId,
|
||||||
|
TelegramUsername = telegramUsername,
|
||||||
|
TelegramDisplayName = displayName,
|
||||||
|
TelegramFirstName = firstName,
|
||||||
|
TelegramLastName = lastName,
|
||||||
|
AllowOrderUpdates = true,
|
||||||
|
AllowMarketing = false
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!customer.IsSuccess || customer.Data == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Failed to get or create customer for support message");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the customer message
|
||||||
|
var messageData = new
|
||||||
|
{
|
||||||
|
CustomerId = customer.Data.Id,
|
||||||
|
Type = 3, // CustomerService
|
||||||
|
Subject = subject,
|
||||||
|
Content = content,
|
||||||
|
Direction = 1, // CustomerToAdmin
|
||||||
|
Priority = 5,
|
||||||
|
IsUrgent = false
|
||||||
|
};
|
||||||
|
|
||||||
|
var response = await _client.Messages.CreateCustomerMessageAsync(messageData);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error sending customer message");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -13,6 +13,7 @@ namespace TeleBot.Services
|
|||||||
public interface IMessageDeliveryService
|
public interface IMessageDeliveryService
|
||||||
{
|
{
|
||||||
Task DeliverPendingMessagesAsync(ITelegramBotClient bot);
|
Task DeliverPendingMessagesAsync(ITelegramBotClient bot);
|
||||||
|
void SetBotClient(ITelegramBotClient botClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MessageDeliveryService : IMessageDeliveryService, IHostedService
|
public class MessageDeliveryService : IMessageDeliveryService, IHostedService
|
||||||
@ -22,6 +23,7 @@ namespace TeleBot.Services
|
|||||||
private readonly IConfiguration _configuration;
|
private readonly IConfiguration _configuration;
|
||||||
private Timer? _deliveryTimer;
|
private Timer? _deliveryTimer;
|
||||||
private readonly int _pollIntervalSeconds;
|
private readonly int _pollIntervalSeconds;
|
||||||
|
private ITelegramBotClient? _botClient;
|
||||||
|
|
||||||
public MessageDeliveryService(
|
public MessageDeliveryService(
|
||||||
ILittleShopService shopService,
|
ILittleShopService shopService,
|
||||||
@ -34,6 +36,12 @@ namespace TeleBot.Services
|
|||||||
_pollIntervalSeconds = configuration.GetValue<int>("MessageDelivery:PollIntervalSeconds", 30);
|
_pollIntervalSeconds = configuration.GetValue<int>("MessageDelivery:PollIntervalSeconds", 30);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void SetBotClient(ITelegramBotClient botClient)
|
||||||
|
{
|
||||||
|
_botClient = botClient;
|
||||||
|
_logger.LogInformation("Bot client set for message delivery service");
|
||||||
|
}
|
||||||
|
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Starting Message Delivery Service - polling every {Interval} seconds", _pollIntervalSeconds);
|
_logger.LogInformation("Starting Message Delivery Service - polling every {Interval} seconds", _pollIntervalSeconds);
|
||||||
@ -55,12 +63,16 @@ namespace TeleBot.Services
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// This will be called by the timer, but we need bot instance
|
_logger.LogInformation("=== Checking for pending messages to deliver ===");
|
||||||
// For now, just log that we're checking
|
|
||||||
_logger.LogDebug("Checking for pending messages to deliver...");
|
|
||||||
|
|
||||||
// Note: We'll need to integrate this with the bot instance
|
if (_botClient != null)
|
||||||
// This is a placeholder for the polling mechanism
|
{
|
||||||
|
await DeliverPendingMessagesAsync(_botClient);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Bot client not available for message delivery");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@ -77,7 +89,7 @@ namespace TeleBot.Services
|
|||||||
|
|
||||||
if (pendingMessages?.Any() != true)
|
if (pendingMessages?.Any() != true)
|
||||||
{
|
{
|
||||||
_logger.LogDebug("No pending messages to deliver");
|
_logger.LogInformation("No pending messages to deliver");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ namespace TeleBot
|
|||||||
private readonly ICommandHandler _commandHandler;
|
private readonly ICommandHandler _commandHandler;
|
||||||
private readonly ICallbackHandler _callbackHandler;
|
private readonly ICallbackHandler _callbackHandler;
|
||||||
private readonly IMessageHandler _messageHandler;
|
private readonly IMessageHandler _messageHandler;
|
||||||
|
private readonly IMessageDeliveryService _messageDeliveryService;
|
||||||
private ITelegramBotClient? _botClient;
|
private ITelegramBotClient? _botClient;
|
||||||
private CancellationTokenSource? _cancellationTokenSource;
|
private CancellationTokenSource? _cancellationTokenSource;
|
||||||
|
|
||||||
@ -32,7 +33,8 @@ namespace TeleBot
|
|||||||
IServiceProvider serviceProvider,
|
IServiceProvider serviceProvider,
|
||||||
ICommandHandler commandHandler,
|
ICommandHandler commandHandler,
|
||||||
ICallbackHandler callbackHandler,
|
ICallbackHandler callbackHandler,
|
||||||
IMessageHandler messageHandler)
|
IMessageHandler messageHandler,
|
||||||
|
IMessageDeliveryService messageDeliveryService)
|
||||||
{
|
{
|
||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
@ -40,6 +42,7 @@ namespace TeleBot
|
|||||||
_commandHandler = commandHandler;
|
_commandHandler = commandHandler;
|
||||||
_callbackHandler = callbackHandler;
|
_callbackHandler = callbackHandler;
|
||||||
_messageHandler = messageHandler;
|
_messageHandler = messageHandler;
|
||||||
|
_messageDeliveryService = messageDeliveryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
@ -69,6 +72,12 @@ namespace TeleBot
|
|||||||
|
|
||||||
var me = await _botClient.GetMeAsync(cancellationToken);
|
var me = await _botClient.GetMeAsync(cancellationToken);
|
||||||
_logger.LogInformation("Bot started: @{Username} ({Id})", me.Username, me.Id);
|
_logger.LogInformation("Bot started: @{Username} ({Id})", me.Username, me.Id);
|
||||||
|
|
||||||
|
// Set bot client for message delivery service
|
||||||
|
if (_messageDeliveryService is MessageDeliveryService deliveryService)
|
||||||
|
{
|
||||||
|
deliveryService.SetBotClient(_botClient);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
|||||||
@ -16,6 +16,7 @@ namespace TeleBot.UI
|
|||||||
new[] { InlineKeyboardButton.WithCallbackData("🛍️ Browse Products", "browse") },
|
new[] { InlineKeyboardButton.WithCallbackData("🛍️ Browse Products", "browse") },
|
||||||
new[] { InlineKeyboardButton.WithCallbackData("🛒 View Cart", "cart") },
|
new[] { InlineKeyboardButton.WithCallbackData("🛒 View Cart", "cart") },
|
||||||
new[] { InlineKeyboardButton.WithCallbackData("📦 My Orders", "orders") },
|
new[] { InlineKeyboardButton.WithCallbackData("📦 My Orders", "orders") },
|
||||||
|
new[] { InlineKeyboardButton.WithCallbackData("🎧 Customer Support", "support") },
|
||||||
new[] { InlineKeyboardButton.WithCallbackData("🔒 Privacy Settings", "privacy") },
|
new[] { InlineKeyboardButton.WithCallbackData("🔒 Privacy Settings", "privacy") },
|
||||||
new[] { InlineKeyboardButton.WithCallbackData("❓ Help", "help") }
|
new[] { InlineKeyboardButton.WithCallbackData("❓ Help", "help") }
|
||||||
});
|
});
|
||||||
@ -277,6 +278,19 @@ namespace TeleBot.UI
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static InlineKeyboardMarkup SupportMenu()
|
||||||
|
{
|
||||||
|
var markup = new InlineKeyboardMarkup(new[]
|
||||||
|
{
|
||||||
|
new[]
|
||||||
|
{
|
||||||
|
InlineKeyboardButton.WithCallbackData("❌ Cancel", "cancel_support")
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return markup;
|
||||||
|
}
|
||||||
|
|
||||||
private static string GetOrderStatusEmoji(int status)
|
private static string GetOrderStatusEmoji(int status)
|
||||||
{
|
{
|
||||||
return status switch
|
return status switch
|
||||||
|
|||||||
@ -256,9 +256,11 @@ namespace TeleBot.UI
|
|||||||
"/browse - Browse products\n" +
|
"/browse - Browse products\n" +
|
||||||
"/cart - View shopping cart\n" +
|
"/cart - View shopping cart\n" +
|
||||||
"/orders - View your orders\n" +
|
"/orders - View your orders\n" +
|
||||||
|
"/support - Contact customer support\n" +
|
||||||
"/privacy - Privacy settings\n" +
|
"/privacy - Privacy settings\n" +
|
||||||
"/pgpkey - Set PGP public key\n" +
|
"/pgpkey - Set PGP public key\n" +
|
||||||
"/ephemeral - Toggle ephemeral mode\n" +
|
"/ephemeral - Toggle ephemeral mode\n" +
|
||||||
|
"/cancel - Cancel current operation\n" +
|
||||||
"/delete - Delete all your data\n" +
|
"/delete - Delete all your data\n" +
|
||||||
"/tor - Get Tor onion address\n" +
|
"/tor - Get Tor onion address\n" +
|
||||||
"/help - Show this help message\n\n" +
|
"/help - Show this help message\n\n" +
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user