chore: init commit from GitHub
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
using System.Text;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace TelegramBot.Commands.CommandMessages;
|
||||
|
||||
public class ChangelogCommandMessage : CommandMessage
|
||||
{
|
||||
public const string CommandName = "/changelog";
|
||||
public override async Task InvokeFromMessage(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||||
{
|
||||
var text = new StringBuilder();
|
||||
foreach (var log in Program.Config.Changelog)
|
||||
{
|
||||
text.Append($"Нововведения от <b>{log.Date}</b>:\n\n{log.Caption}\n\n");
|
||||
}
|
||||
await botClient.SendTextMessageAsync(update.Message?.Chat!, text.ToString(), cancellationToken: cancellationToken, parseMode: ParseMode.Html);
|
||||
}
|
||||
}
|
||||
12
TelegramBot/Commands/CommandMessages/CommandMessage.cs
Normal file
12
TelegramBot/Commands/CommandMessages/CommandMessage.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace TelegramBot.Commands.CommandMessages;
|
||||
|
||||
public abstract class CommandMessage : IMessage
|
||||
{
|
||||
public abstract Task InvokeFromMessage(ITelegramBotClient botClient, Update update,
|
||||
CancellationToken cancellationToken);
|
||||
}
|
||||
20
TelegramBot/Commands/CommandMessages/StartCommandMessage.cs
Normal file
20
TelegramBot/Commands/CommandMessages/StartCommandMessage.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace TelegramBot.Commands.CommandMessages;
|
||||
|
||||
public class StartCommandMessage : CommandMessage
|
||||
{
|
||||
public const string CommandName = "/start";
|
||||
public override async Task InvokeFromMessage(ITelegramBotClient botClient,
|
||||
Update update,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var text = Program.Config.StartMessage;
|
||||
text += $"\nBuild assembly version: {FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion}";
|
||||
text += $"\n\nНововведения по комманде /changelog";
|
||||
await botClient.SendTextMessageAsync(update.Message?.Chat!, text, cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
12
TelegramBot/Commands/ErrorMessage.cs
Normal file
12
TelegramBot/Commands/ErrorMessage.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace TelegramBot.Commands;
|
||||
|
||||
public class ErrorMessage : IMessage
|
||||
{
|
||||
public async Task InvokeFromMessage(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||||
{
|
||||
await botClient.SendTextMessageAsync(update.Message?.Chat!, "Ошибка! Команда не найдена", cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
11
TelegramBot/Commands/IMessage.cs
Normal file
11
TelegramBot/Commands/IMessage.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace TelegramBot.Commands;
|
||||
|
||||
public interface IMessage
|
||||
{
|
||||
public Task InvokeFromMessage(ITelegramBotClient botClient,
|
||||
Update update,
|
||||
CancellationToken cancellationToken);
|
||||
}
|
||||
98
TelegramBot/Commands/LinkMessage.cs
Normal file
98
TelegramBot/Commands/LinkMessage.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot.Types.InlineQueryResults;
|
||||
using TelegramBot.DTOs;
|
||||
|
||||
namespace TelegramBot.Commands;
|
||||
|
||||
public class LinkMessage : IMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// Create message from API
|
||||
/// </summary>
|
||||
/// <param name="botClient"></param>
|
||||
/// <param name="inputUrl"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
async Task<(string? message, string? preview)> GetMessage(ITelegramBotClient botClient, string inputUrl,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var client = new HttpClient();
|
||||
var res = await client.GetAsync($"{Program.Config.ApiPath}/link/getService?link=" + inputUrl,
|
||||
cancellationToken);
|
||||
var typeRes = await res.Content.ReadFromJsonAsync<GetLink>(cancellationToken: cancellationToken);
|
||||
if (!res.IsSuccessStatusCode)
|
||||
{
|
||||
return (string.Empty, $"Произошла ошибка при обработке запроса\n{res.StatusCode.ToString()}");
|
||||
}
|
||||
|
||||
var message = new StringBuilder();
|
||||
var preview = new StringBuilder();
|
||||
foreach (var service in typeRes?.service.Another()!)
|
||||
{
|
||||
res = await client.PostAsJsonAsync($"{Program.Config.ApiPath}/link/fromLink", new
|
||||
{
|
||||
link = inputUrl,
|
||||
service,
|
||||
}, cancellationToken: cancellationToken);
|
||||
if (!res.IsSuccessStatusCode)
|
||||
{
|
||||
preview.AppendLine($"❌ {service}\n");
|
||||
message.AppendLine(
|
||||
$"❌ {service}: 🤨👨🏿🦰😡😮💨😰 {(await res.Content.ReadFromJsonAsync<ErrorDto>(cancellationToken: cancellationToken))?.title}");
|
||||
message.AppendLine();
|
||||
continue;
|
||||
}
|
||||
|
||||
var apiRes = await res.Content.ReadFromJsonAsync<LinkDto>(cancellationToken: cancellationToken);
|
||||
preview.AppendLine($"✅ {service}\n");
|
||||
message.AppendLine($"✅ {service} - {apiRes?.link}");
|
||||
message.AppendLine();
|
||||
}
|
||||
|
||||
return (message.ToString(), preview.ToString());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Ошибка:\n--------------------------");
|
||||
Console.WriteLine(e);
|
||||
return (e.ToString(), "Произошла ошибка при обработке запроса\nдетали в сообщении");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task InvokeFromMessage(ITelegramBotClient botClient, Update update,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var result = (await GetMessage(botClient, update.Message?.Text!, cancellationToken)).message;
|
||||
await botClient.SendTextMessageAsync(update.Message?.Chat!, string.IsNullOrEmpty(result) ? "Ошибка🤔:\nНе удалось определить сервис исходной ссылки.\nДоступные сервисы для конвертации:\nTidal\nYandex\nSpotify" : result, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
public async Task InvokeFromInlineQuery(ITelegramBotClient botClient, Update update,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if(string.IsNullOrEmpty(update.InlineQuery!.Query))
|
||||
return;
|
||||
|
||||
var message = await new LinkMessage().GetMessage(botClient, update.InlineQuery!.Query, cancellationToken);
|
||||
if(message.message != null)
|
||||
try
|
||||
{
|
||||
await botClient.AnswerInlineQueryAsync(update.InlineQuery.Id,
|
||||
new[]
|
||||
{
|
||||
new InlineQueryResultArticle("0", message.preview!,
|
||||
new InputTextMessageContent(message.message))
|
||||
}, cancellationToken: cancellationToken);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Failed to send answer to {update.Message?.Chat.Username}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user