Files

131 lines
4.3 KiB
C#
Raw Permalink Normal View History

2022-02-16 17:07:30 +02:00
#nullable enable
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.Core.Service;
using Microsoft.Extensions.DependencyInjection;
using Telegram.Bot;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;
using Telegram.Bot.Types.ReplyMarkups;
using West.TelegramBot.ContentStore.Model;
using West.TelegramBot.ContentStore.Service;
namespace West.TelegramBot.ContentStore.Handler;
public class Store : BotEventHandler
{
private readonly IReputation? _reputation;
private readonly IServiceProvider _provider;
2022-02-16 18:08:02 +02:00
private readonly ContentStore _store;
public Store(IServiceProvider provider, ContentStore store)
2022-02-16 17:07:30 +02:00
{
_reputation = provider.GetService<IReputation>();
_provider = provider;
2022-02-16 18:08:02 +02:00
_store = store;
2022-02-16 17:07:30 +02:00
}
[Command(InChat.Public, "store", CommandParseMode.Both)]
public async Task HandleStore()
{
if (_reputation == null)
{
await Bot.SendTextMessageAsync(Chat.Id, "Reputation is unavailable.");
return;
}
await Bot.SendTextMessageAsync(Chat.Id, "Чего пожелаете?", replyMarkup: new InlineKeyboardMarkup(
_provider.GetServices<IRandomMediaService>()
.OrderBy(x => x.StoreItem.Order)
.Select(x => x.StoreItem.ToButton(From.Id))
.Chunk(2)
.ToList()
)
);
2022-02-16 17:07:30 +02:00
}
[Update(InChat.Public, UpdateFlag.CallbackQuery)]
public async Task HandleStoreCallback()
{
var query = CallbackQuery;
var queryData = query.Data;
if (queryData == null || !queryData.StartsWith("store|") || _reputation == null)
{
return;
}
var paramList = queryData.Split('|');
if (!long.TryParse(paramList[1], out var senderId))
{
return;
}
if (senderId != From.Id)
{
await AnswerQuery(query.Id, "Keyboard called by another user.");
return;
}
var answer = "Unknown action.";
if (paramList[2] == "purchase")
{
var purchaseType = paramList[3];
answer = "Спасибо за покупку";
2022-02-16 18:08:02 +02:00
if (!TryGetMediaService(purchaseType, out var mediaService))
2022-02-16 17:07:30 +02:00
{
await AnswerQuery(query.Id, "Unknown purchasable.");
return;
}
2022-02-16 18:08:02 +02:00
var storeItem = mediaService.StoreItem;
2022-02-16 17:07:30 +02:00
var messageId = query.Message!.MessageId;
if (await _reputation.GetUserReputationAsync(Chat, From) < storeItem.Price)
{
await Bot.EditMessageTextAsync(
Chat.Id,
messageId,
$"{From.ToHtml()}, продолжай работать и тебе обязательно хватит на покупку.",
ParseMode.Html);
await AnswerQuery(query.Id);
return;
}
await _reputation.IncrementReputationAsync(Chat, From, -storeItem.Price);
await Bot.EditMessageTextAsync(Chat.Id,
messageId,
$"{From.ToHtml()} купил \"{storeItem.Name}\".",
ParseMode.Html);
2022-02-16 17:07:30 +02:00
var randomMedia = await mediaService!.GetRandomMediaAsync();
var file = new InputOnlineFile(randomMedia.Url);
switch (randomMedia.Type)
{
case InputMediaType.Photo:
await Bot.SendPhotoAsync(Chat.Id, file, randomMedia.Caption, replyToMessageId: messageId);
break;
case InputMediaType.Video:
await Bot.SendVideoAsync(Chat.Id, file, caption: randomMedia.Caption, replyToMessageId: messageId);
break;
}
}
await AnswerQuery(query.Id, answer);
}
private bool TryGetMediaService(string name, out IRandomMediaService? mediaService)
{
2022-02-16 18:08:02 +02:00
mediaService = _provider.GetServices<IRandomMediaService>()
.FirstOrDefault(s => s.StoreItem.Id == name);
2022-02-16 17:07:30 +02:00
return mediaService != null;
}
private async Task AnswerQuery(string id, string? message = null)
{
await Bot.AnswerCallbackQueryAsync(id, message);
}
}