DocSpace-buildtools/common/services/ASC.TelegramService/TelegramHandler.cs

246 lines
8.0 KiB
C#
Raw Normal View History

2022-03-15 18:00:53 +00:00
// (c) Copyright Ascensio System SIA 2010-2022
//
// This program is a free software product.
// You can redistribute it and/or modify it under the terms
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
// any third-party rights.
//
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
//
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
//
// The interactive user interfaces in modified source and object code versions of the Program must
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
//
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
// trademark law for use of our trademarks.
//
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
namespace ASC.TelegramService;
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;
2022-05-26 09:01:54 +00:00
private readonly ILogger<TelegramHandler> _log;
2022-02-17 11:16:52 +00:00
private readonly IServiceScopeFactory _scopeFactory;
2022-05-24 15:41:28 +00:00
private readonly IDistributedCache _distributedCache;
2022-02-17 11:16:52 +00:00
2022-05-23 09:21:04 +00:00
public TelegramHandler(IDistributedCache distributedCache,
CommandModule command,
ILogger<TelegramHandler> logger,
2022-05-23 09:21:04 +00:00
IServiceScopeFactory scopeFactory)
2020-09-24 14:30:27 +00:00
{
2022-02-17 11:16:52 +00:00
_command = command;
_log = logger;
2022-02-17 11:16:52 +00:00
_scopeFactory = scopeFactory;
_clients = new Dictionary<int, TenantTgClient>();
2022-05-23 09:21:04 +00:00
_distributedCache = distributedCache;
2022-02-17 11:16:52 +00:00
ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
}
2020-09-24 14:30:27 +00:00
2022-05-17 15:27:34 +00:00
public async Task SendMessage(NotifyMessage msg)
{
2022-03-17 15:01:39 +00:00
if (string.IsNullOrEmpty(msg.Reciever))
{
2022-05-24 15:41:28 +00:00
return;
2022-03-17 15:01:39 +00:00
}
if (!_clients.ContainsKey(msg.TenantId))
{
2022-05-24 15:41:28 +00:00
return;
2022-03-17 15:01:39 +00:00
}
2020-09-24 14:30:27 +00:00
2022-02-17 11:16:52 +00:00
var scope = _scopeFactory.CreateScope();
2022-05-17 18:57:18 +00:00
var telegramDao = scope.ServiceProvider.GetService<TelegramDao>();
2020-09-24 14:30:27 +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-05-17 18:57:18 +00:00
var tgUser = telegramDao.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-05-13 11:35:39 +00:00
_log.DebugCouldntFind(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);
2022-05-17 15:27:34 +00:00
await client.SendTextMessageAsync(chat, msg.Content, ParseMode.MarkdownV2);
2022-02-17 11:16:52 +00:00
}
catch (Exception e)
2020-09-24 14:30:27 +00:00
{
2022-05-13 11:35:39 +00:00
_log.DebugCouldntSend(msg.Reciever, e);
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)
{
2022-03-17 15:01:39 +00:00
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];
2022-05-17 15:27:34 +00:00
if (client.CancellationTokenSource != null)
{
client.CancellationTokenSource.Cancel();
client.CancellationTokenSource.Dispose();
client.CancellationTokenSource = null;
}
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-04-14 19:42:15 +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
{
if (startTelegramService)
2020-09-24 14:30:27 +00:00
{
2022-03-17 15:01:39 +00:00
if (!telegramHelper.TestingClient(newClient))
{
return;
}
2020-09-24 14:30:27 +00:00
}
2022-05-17 15:27:34 +00:00
if (client.CancellationTokenSource != null)
{
client.CancellationTokenSource.Cancel();
client.CancellationTokenSource.Dispose();
client.CancellationTokenSource = null;
}
2022-02-17 11:16:52 +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)
{
2022-03-17 15:01:39 +00:00
if (!telegramHelper.TestingClient(newClient))
{
return;
}
2022-02-17 11:16:52 +00:00
}
2020-09-24 14:30:27 +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)
{
2022-03-17 15:01:39 +00:00
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);
2022-05-23 09:21:04 +00:00
_distributedCache.SetString(token, userKey, new DistributedCacheEntryOptions
2022-05-24 15:41:28 +00:00
{
AbsoluteExpiration = dateExpires
2022-05-23 09:21:04 +00:00
});
2022-02-17 11:16:52 +00:00
}
2020-09-24 14:30:27 +00:00
2022-05-17 15:27:34 +00:00
private void BindClient(TelegramBotClient client, int tenantId, CancellationToken cancellationToken)
{
2022-05-17 15:27:34 +00:00
var cts = new CancellationTokenSource();
2022-03-17 15:01:39 +00:00
2022-05-17 15:27:34 +00:00
_clients[tenantId].CancellationTokenSource = cts;
var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cts.Token, cancellationToken);
client.StartReceiving(updateHandler: (botClient, exception, cancellationToken) => HandleUpdateAsync(botClient, exception, cancellationToken, tenantId),
errorHandler: HandleErrorAsync,
cancellationToken: linkedCts.Token);
2022-02-17 11:16:52 +00:00
}
2020-09-24 14:30:27 +00:00
async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken, int tenantId)
{
2022-05-24 15:41:28 +00:00
if (update.Type != UpdateType.Message)
{
return;
}
2022-05-24 15:41:28 +00:00
if (update.Message.Type != MessageType.Text)
{
return;
}
2022-05-24 15:41:28 +00:00
if (String.IsNullOrEmpty(update.Message.Text) || update.Message.Text[0] != '/')
2022-03-17 15:01:39 +00:00
{
2022-05-24 15:41:28 +00:00
return;
2022-03-17 15:01:39 +00:00
}
2022-05-17 15:27:34 +00:00
await _command.HandleCommand(update.Message, botClient, tenantId);
}
2022-02-17 11:16:52 +00:00
2022-05-17 15:27:34 +00:00
Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
2022-02-17 11:16:52 +00:00
{
2022-05-17 15:27:34 +00:00
String errorMessage;
2022-02-17 11:16:52 +00:00
2022-05-17 15:27:34 +00:00
if (exception is ApiRequestException)
{
errorMessage = String.Format("Telegram API Error:\n[{0}]\n{1}", ((ApiRequestException)exception).ErrorCode, ((ApiRequestException)exception).Message);
2022-05-17 15:27:34 +00:00
}
else
{
errorMessage = exception.ToString();
}
2022-02-17 11:16:52 +00:00
2022-05-17 15:27:34 +00:00
_log.Error(errorMessage);
return Task.CompletedTask;
2022-02-17 11:16:52 +00:00
}
private string UserKey(string userId, int tenantId)
{
return string.Format("{0}:{1}", userId, tenantId);
}
}
public static class TelegramHandlerExtension
{
public static void Register(DIHelper services)
{
services.TryAdd<TelegramHelper>();
2020-09-24 14:30:27 +00:00
}
}