Skip to content

Commit

Permalink
[#4] Use Telegram.Bot.Types.Update (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaGrom authored Sep 6, 2020
1 parent 756e6da commit d788016
Show file tree
Hide file tree
Showing 30 changed files with 28 additions and 178 deletions.
2 changes: 1 addition & 1 deletion Telegrom.Core/Contexts/IUpdateContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core.Contexts
{
Expand Down
1 change: 1 addition & 0 deletions Telegrom.Core/Contexts/SessionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Autofac;
using Microsoft.Extensions.Logging;
using Telegram.Bot.Requests.Abstractions;
using Telegram.Bot.Types;
using Telegrom.Core.MessageBus;
using Telegrom.Core.TelegramModel;

Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/Contexts/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Autofac;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core.Contexts
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/Contexts/UpdateContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core.Contexts
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/IIdentityService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/ISessionStateAttributesRemover.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/ITelegramUpdateReceiver.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Threading;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/IUpdateService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/IWakeUpService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/MessageBus/IIncomingUpdateQueueReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core.MessageBus
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/MessageBus/IIncomingUpdateQueueWriter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core.MessageBus
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Core/MessageBus/IUpdateDispatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using Telegrom.Core.TelegramModel;
using Telegram.Bot.Types;

namespace Telegrom.Core.MessageBus
{
Expand Down
23 changes: 0 additions & 23 deletions Telegrom.Core/TelegramModel/CallbackQuery.cs

This file was deleted.

23 changes: 0 additions & 23 deletions Telegrom.Core/TelegramModel/Message.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Telegrom.Core/TelegramModel/Update.cs

This file was deleted.

25 changes: 0 additions & 25 deletions Telegrom.Core/TelegramModel/User.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Telegrom.Database/IdentityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Telegram.Bot.Types;
using Telegrom.Core;
using Telegrom.Core.TelegramModel;
using Telegrom.Database.Model;

namespace Telegrom.Database
Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Database/SessionStateAttributesRemover.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Telegram.Bot.Types;
using Telegrom.Core;
using Telegrom.Core.TelegramModel;

namespace Telegrom.Database
{
Expand Down
8 changes: 4 additions & 4 deletions Telegrom.Database/UpdateService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using Telegrom.Core;
using Telegrom.Core.TelegramModel;
using Telegrom.Database.Model;

namespace Telegrom.Database
Expand All @@ -21,7 +21,7 @@ public UpdateService(User user, DatabaseContext context)
public async Task SaveUpdateAsync(Update update, CancellationToken cancellationToken)
{
var sessionUpdate = await _context.SessionUpdates
.FindAsync(new object[] { _user.Id, update.UpdateId }, cancellationToken);
.FindAsync(new object[] { _user.Id, update.Id }, cancellationToken);

if (sessionUpdate != null)
{
Expand All @@ -32,7 +32,7 @@ public async Task SaveUpdateAsync(Update update, CancellationToken cancellationT
await _context.SessionUpdates.AddAsync(new SessionUpdate
{
IdentityId = _user.Id,
UpdateId = update.UpdateId,
UpdateId = update.Id,
UpdateType = update.GetType().AssemblyQualifiedName,
Update = JsonConvert.SerializeObject(update)
}, cancellationToken);
Expand All @@ -44,7 +44,7 @@ await _context.SessionUpdates.AddAsync(new SessionUpdate
public async Task MakeUpdateProcessedAsync(Update update, CancellationToken cancellationToken)
{
var sessionUpdate = await _context.SessionUpdates
.FindAsync(new object[] { _user.Id, update.UpdateId }, cancellationToken);
.FindAsync(new object[] { _user.Id, update.Id }, cancellationToken);

sessionUpdate.Processed = true;

Expand Down
2 changes: 1 addition & 1 deletion Telegrom.Database/WakeUpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using Telegrom.Core;
using Telegrom.Core.TelegramModel;

namespace Telegrom.Database
{
Expand Down
35 changes: 0 additions & 35 deletions Telegrom.TelegramService/TelegramMappingProfile.cs

This file was deleted.

6 changes: 0 additions & 6 deletions Telegrom.TelegramService/TelegramServiceModule.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Net;
using Autofac;
using AutoMapper;
using MihaZupan;
using Telegram.Bot;
using Telegrom.Core;
using Telegrom.Core.MessageBus;

namespace Telegrom.TelegramService
{
Expand Down Expand Up @@ -52,10 +50,6 @@ protected override void Load(ContainerBuilder builder)
.SingleInstance();
}

builder.RegisterType<TelegramMappingProfile>()
.As<Profile>()
.InstancePerLifetimeScope();

builder.RegisterType<TelegramUpdateReceiver>()
.As<ITelegramUpdateReceiver>()
.SingleInstance();
Expand Down
19 changes: 2 additions & 17 deletions Telegrom.TelegramService/TelegramUpdateReceiver.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
using System;
using System.Threading;
using AutoMapper;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types;
using Telegrom.Core;
using Telegrom.Core.TelegramModel;

namespace Telegrom.TelegramService
{
public class TelegramUpdateReceiver : ITelegramUpdateReceiver
{
private readonly ITelegramBotClient _telegramBotClient;
private readonly IMapper _mapper;
private readonly ILogger<TelegramUpdateReceiver> _logger;
private Action<Update, CancellationToken> _updateReceivedHandler;

public TelegramUpdateReceiver(ITelegramBotClient telegramBotClient,
IMapper mapper,
ILogger<TelegramUpdateReceiver> logger)
{
_telegramBotClient = telegramBotClient;
_mapper = mapper;
_logger = logger;

var me = _telegramBotClient.GetMeAsync().GetAwaiter().GetResult();
Expand All @@ -45,17 +40,7 @@ private void BotOnUpdateReceived(object sender, UpdateEventArgs e)
var update = e.Update;
_logger.LogInformation("Get update {@update}", update);

Update telegramUpdate = update.Type switch
{
UpdateType.Message => _mapper.Map<Message>(update),
UpdateType.CallbackQuery => _mapper.Map<CallbackQuery>(update),
_ => null
};

if (telegramUpdate != null)
{
_updateReceivedHandler?.Invoke(telegramUpdate, default);
}
_updateReceivedHandler?.Invoke(update, default);
}

public void SetUpdateReceivedHandler(Action<Update, CancellationToken> handler)
Expand Down
1 change: 0 additions & 1 deletion Telegrom.TelegramService/Telegrom.TelegramService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

<ItemGroup>
<PackageReference Include="Autofac" Version="5.2.0" />
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="HttpToSocks5Proxy" Version="1.4.0" />
<PackageReference Include="Telegram.Bot" Version="15.7.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion Telegrom/IncomingUpdateQueue.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegrom.Core.MessageBus;
using Telegrom.Core.TelegramModel;

namespace Telegrom
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom/InternalBotService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Telegram.Bot.Types;
using Telegrom.Core;
using Telegrom.Core.Contexts;
using Telegrom.Core.MessageBus;
using Telegrom.Core.TelegramModel;

namespace Telegrom
{
Expand Down
2 changes: 1 addition & 1 deletion Telegrom/SessionIncomingUpdateQueue.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Telegrom.Core.MessageBus;
using Telegrom.Core.TelegramModel;

namespace Telegrom
{
Expand Down
Loading

0 comments on commit d788016

Please sign in to comment.