2022-03-15 16:59:24 +00:00
|
|
|
|
namespace ASC.TelegramService;
|
2022-03-01 07:56:21 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
[Singletone(Additional = typeof(TelegramHandlerExtension))]
|
|
|
|
|
public class TelegramHandler
|
|
|
|
|
{
|
|
|
|
|
private readonly Dictionary<int, TenantTgClient> _clients;
|
|
|
|
|
private readonly CommandModule _command;
|
|
|
|
|
private readonly ILog _log;
|
|
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
|
|
|
|
|
|
|
|
public TelegramHandler(CommandModule command, IOptionsMonitor<ILog> option, IServiceScopeFactory scopeFactory)
|
2020-09-24 14:30:27 +00:00
|
|
|
|
{
|
2022-02-17 11:16:52 +00:00
|
|
|
|
_command = command;
|
|
|
|
|
_log = option.CurrentValue;
|
|
|
|
|
_scopeFactory = scopeFactory;
|
|
|
|
|
_clients = new Dictionary<int, TenantTgClient>();
|
|
|
|
|
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
|
|
|
|
|
}
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-03-01 07:56:21 +00:00
|
|
|
|
public Task SendMessage(NotifyMessage msg)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(msg.Reciever)) return Task.CompletedTask;
|
|
|
|
|
if (!_clients.ContainsKey(msg.TenantId)) return Task.CompletedTask;
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-03-01 07:56:21 +00:00
|
|
|
|
return InternalSendMessage(msg);
|
|
|
|
|
}
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
private async Task InternalSendMessage(NotifyMessage msg)
|
|
|
|
|
{
|
|
|
|
|
var scope = _scopeFactory.CreateScope();
|
2022-03-01 07:56:21 +00:00
|
|
|
|
var cachedTelegramDao = scope.ServiceProvider.GetService<IOptionsSnapshot<CachedTelegramDao>>().Value;
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-03-01 07:56:21 +00:00
|
|
|
|
var client = _clients[msg.TenantId].Client;
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-03-01 07:56:21 +00:00
|
|
|
|
var tgUser = cachedTelegramDao.GetUser(Guid.Parse(msg.Reciever), msg.TenantId);
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
if (tgUser == null)
|
2020-09-24 14:30:27 +00:00
|
|
|
|
{
|
2022-03-01 07:56:21 +00:00
|
|
|
|
_log.DebugFormat("Couldn't find telegramId for user '{0}'", msg.Reciever);
|
2022-02-17 11:16:52 +00:00
|
|
|
|
return;
|
2020-09-24 14:30:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
var chat = await client.GetChatAsync(tgUser.TelegramUserId);
|
|
|
|
|
await client.SendTextMessageAsync(chat, msg.Content, Telegram.Bot.Types.Enums.ParseMode.Markdown);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
2020-09-24 14:30:27 +00:00
|
|
|
|
{
|
2022-03-01 07:56:21 +00:00
|
|
|
|
_log.DebugFormat("Couldn't send message for user '{0}' got an '{1}'", msg.Reciever, e.Message);
|
2020-09-24 14:30:27 +00:00
|
|
|
|
}
|
2022-02-17 11:16:52 +00:00
|
|
|
|
}
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
public void DisableClient(int tenantId)
|
|
|
|
|
{
|
|
|
|
|
if (!_clients.ContainsKey(tenantId)) return;
|
2020-09-25 11:13:43 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
var client = _clients[tenantId];
|
|
|
|
|
client.Client.StopReceiving();
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
_clients.Remove(tenantId);
|
|
|
|
|
}
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-28 13:11:54 +00:00
|
|
|
|
public void CreateOrUpdateClientForTenant(int tenantId, string token, int tokenLifespan, string proxy, bool startTelegramService, CancellationToken stoppingToken, bool force = false)
|
2022-02-17 11:16:52 +00:00
|
|
|
|
{
|
|
|
|
|
var scope = _scopeFactory.CreateScope();
|
|
|
|
|
var telegramHelper = scope.ServiceProvider.GetService<TelegramHelper>();
|
|
|
|
|
var newClient = telegramHelper.InitClient(token, proxy);
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
if (_clients.TryGetValue(tenantId, out var client))
|
|
|
|
|
{
|
|
|
|
|
client.TokenLifeSpan = tokenLifespan;
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
if (token != client.Token || proxy != client.Proxy)
|
2020-09-24 14:30:27 +00:00
|
|
|
|
{
|
2022-03-01 07:56:21 +00:00
|
|
|
|
if (startTelegramService)
|
2020-09-24 14:30:27 +00:00
|
|
|
|
{
|
2020-09-25 11:13:43 +00:00
|
|
|
|
if (!telegramHelper.TestingClient(newClient)) return;
|
2020-09-24 14:30:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
client.Client.StopReceiving();
|
|
|
|
|
|
2022-02-28 13:11:54 +00:00
|
|
|
|
BindClient(newClient, tenantId, stoppingToken);
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
client.Client = newClient;
|
|
|
|
|
client.Token = token;
|
|
|
|
|
client.Proxy = proxy;
|
2020-09-24 14:30:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-17 11:16:52 +00:00
|
|
|
|
else
|
2020-09-24 14:30:27 +00:00
|
|
|
|
{
|
2022-02-17 11:16:52 +00:00
|
|
|
|
if (!force && startTelegramService)
|
|
|
|
|
{
|
|
|
|
|
if (!telegramHelper.TestingClient(newClient)) return;
|
|
|
|
|
}
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-28 13:11:54 +00:00
|
|
|
|
BindClient(newClient, tenantId, stoppingToken);
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
_clients.Add(tenantId, new TenantTgClient()
|
|
|
|
|
{
|
|
|
|
|
Token = token,
|
|
|
|
|
Client = newClient,
|
|
|
|
|
Proxy = proxy,
|
|
|
|
|
TenantId = tenantId,
|
|
|
|
|
TokenLifeSpan = tokenLifespan
|
|
|
|
|
});
|
2020-09-24 14:30:27 +00:00
|
|
|
|
}
|
2022-02-17 11:16:52 +00:00
|
|
|
|
}
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
public void RegisterUser(string userId, int tenantId, string token)
|
|
|
|
|
{
|
|
|
|
|
if (!_clients.ContainsKey(tenantId)) return;
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-02-17 11:16:52 +00:00
|
|
|
|
var userKey = UserKey(userId, tenantId);
|
|
|
|
|
var dateExpires = DateTimeOffset.Now.AddMinutes(_clients[tenantId].TokenLifeSpan);
|
|
|
|
|
MemoryCache.Default.Set(token, userKey, dateExpires);
|
|
|
|
|
}
|
2020-09-24 14:30:27 +00:00
|
|
|
|
|
2022-03-01 07:56:21 +00:00
|
|
|
|
private Task OnMessage(object sender, MessageEventArgs e, TelegramBotClient client, int tenantId)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(e.Message.Text) || e.Message.Text[0] != '/') return Task.CompletedTask;
|
|
|
|
|
return InternalOnMessage(sender, e, client, tenantId);
|
|
|
|
|
}
|
2022-02-17 11:16:52 +00:00
|
|
|
|
|
|
|
|
|
private async Task InternalOnMessage(object sender, MessageEventArgs e, TelegramBotClient client, int tenantId)
|
|
|
|
|
{
|
|
|
|
|
await _command.HandleCommand(e.Message, client, tenantId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-02-28 13:11:54 +00:00
|
|
|
|
private void BindClient(TelegramBotClient client, int tenantId, CancellationToken stoppingToken)
|
2022-02-17 11:16:52 +00:00
|
|
|
|
{
|
|
|
|
|
client.OnMessage += async (sender, e) => { await OnMessage(sender, e, client, tenantId); };
|
2022-02-28 13:11:54 +00:00
|
|
|
|
client.StartReceiving(cancellationToken: stoppingToken);
|
2022-02-17 11:16:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string UserKey(string userId, int tenantId)
|
|
|
|
|
{
|
|
|
|
|
return string.Format("{0}:{1}", userId, tenantId);
|
|
|
|
|
}
|
2022-03-01 07:56:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class TelegramHandlerExtension
|
|
|
|
|
{
|
|
|
|
|
public static void Register(DIHelper services)
|
|
|
|
|
{
|
|
|
|
|
services.TryAdd<TelegramHelper>();
|
2020-09-24 14:30:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|