Skip to content

Commit

Permalink
feat: add telegram
Browse files Browse the repository at this point in the history
  • Loading branch information
CumpsD committed Jan 30, 2024
1 parent 3a85e55 commit 4d8a7f1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
47 changes: 31 additions & 16 deletions chainflip-insights/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace ChainflipInsights
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
Expand All @@ -18,6 +17,9 @@ namespace ChainflipInsights
using ChainflipInsights.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Telegram.Bot;
using Telegram.Bot.Types;
using File = System.IO.File;

public class Bot
{
Expand Down Expand Up @@ -66,41 +68,44 @@ public class Bot
};

private readonly ILogger<Bot> _logger;
private readonly DiscordSocketClient _client;
private readonly DiscordSocketClient _discordClient;
private readonly TelegramBotClient _telegramClient;
private readonly BotConfiguration _configuration;
private readonly IHttpClientFactory _httpClientFactory;

public Bot(
ILogger<Bot> logger,
IOptions<BotConfiguration> options,
IHttpClientFactory httpClientFactory,
DiscordSocketClient client)
DiscordSocketClient discordClient,
TelegramBotClient telegramClient)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException(nameof(httpClientFactory));
_client = client ?? throw new ArgumentNullException(nameof(client));
_discordClient = discordClient ?? throw new ArgumentNullException(nameof(discordClient));
_telegramClient = telegramClient ?? throw new ArgumentNullException(nameof(telegramClient));
_configuration = options.Value ?? throw new ArgumentNullException(nameof(options));
}

public async Task RunAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Running Bot...");

await _client.LoginAsync(
await _discordClient.LoginAsync(
TokenType.Bot,
_configuration.Token);

await _client.StartAsync();
await _discordClient.StartAsync();

// Start a loop fetching Swap Info
await ProvideSwapInfo(cancellationToken);

cancellationToken.WaitHandle.WaitOne();

if (_client.ConnectionState != ConnectionState.Disconnected)
if (_discordClient.ConnectionState != ConnectionState.Disconnected)
{
await _client.LogoutAsync();
await _client.StopAsync();
await _discordClient.LogoutAsync();
await _discordClient.StopAsync();
}
}

Expand Down Expand Up @@ -207,20 +212,30 @@ private async Task AnnounceSwap(
var dollarString = "0.00";

var time = DateTimeOffset.Parse(swap.SwapScheduledBlockTimestamp);

var text =
$"{GetEmoji(swap.DepositValueUsd)} Swapped " +
$"**{Math.Round(swapInput, 8).ToString(inputString)} {swap.SourceAsset}** (*${swap.DepositValueUsd.ToString(dollarString)}*) → " +
$"**{Math.Round(swapOutput, 8).ToString(outputString)} {swap.DestinationAsset}** (*${swap.EgressValueUsd.ToString(dollarString)}*) " +
// $"in **{HumanTime(swapTime)}** " +
$"// **[view swap on explorer]({_configuration.ExplorerUrl}{swap.Id})**";

if (_client.ConnectionState == ConnectionState.Connected)
if (_discordClient.ConnectionState == ConnectionState.Connected)
{
var infoChannel = (ITextChannel)_client.GetChannel(_configuration.SwapInfoChannelId.Value);
var infoChannel = (ITextChannel)_discordClient.GetChannel(_configuration.SwapInfoChannelId.Value);

await infoChannel.SendMessageAsync(
$"{GetEmoji(swap.DepositValueUsd)} Swapped " +
$"**{Math.Round(swapInput, 8).ToString(inputString)} {swap.SourceAsset}** (*${swap.DepositValueUsd.ToString(dollarString)}*) → " +
$"**{Math.Round(swapOutput, 8).ToString(outputString)} {swap.DestinationAsset}** (*${swap.EgressValueUsd.ToString(dollarString)}*) " +
// $"in **{HumanTime(swapTime)}** " +
$"// **[view swap on explorer]({_configuration.ExplorerUrl}{swap.Id})**",
text,
flags: MessageFlags.SuppressEmbeds);
}

var message = await _telegramClient.SendTextMessageAsync(
new ChatId(_configuration.TelegramSwapInfoChannelId.Value),
text,
disableNotification: true,
allowSendingWithoutReply: true,
cancellationToken: cancellationToken);

_logger.LogInformation(
"Swap {IngressAmount} {IngressTicker} to {EgressAmount} {EgressTicker} at {SwapTime} -> {ExplorerUrl}",
Math.Round(swapInput, 8).ToString(inputString),
Expand Down
14 changes: 14 additions & 0 deletions chainflip-insights/Configuration/BotConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public string? Token
get; init;
}

[Required]
[NotNull]
public string? TelegramToken
{
get; init;
}

[Required]
[NotNull]
public string? GraphUrl
Expand Down Expand Up @@ -56,5 +63,12 @@ public string? LastSwapIdLocation
{
get; init;
}

[Required]
[NotNull]
public long? TelegramSwapInfoChannelId
{
get; init;
}
}
}
5 changes: 5 additions & 0 deletions chainflip-insights/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Telegram.Bot;

public class Program
{
Expand Down Expand Up @@ -169,6 +170,10 @@ private static AutofacServiceProvider ConfigureServices(
})
.SingleInstance();

builder
.Register(_ => new TelegramBotClient(botConfiguration.TelegramToken))
.SingleInstance();

builder
.RegisterType<Bot>()
.SingleInstance();
Expand Down
1 change: 1 addition & 0 deletions chainflip-insights/chainflip-insights.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup>
</Project>

0 comments on commit 4d8a7f1

Please sign in to comment.