Files
telegram-bot/modules/ContentStore/Handler/Store.cs
T
2022-02-16 18:30:50 +02:00

148 lines
4.7 KiB
C#

#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;
private readonly ContentStore _store;
public Store(IServiceProvider provider, ContentStore store)
{
_reputation = provider.GetService<IReputation>();
_provider = provider;
_store = store;
}
[Command(InChat.Public, "store", CommandParseMode.Both)]
public async Task HandleStore()
{
if (_reputation == null)
{
await Bot.SendTextMessageAsync(Chat.Id, "Reputation is unavailable.");
return;
}
var rowList = new List<List<InlineKeyboardButton>>();
var counter = 0;
var list = new List<InlineKeyboardButton>();
foreach (var item in _provider.GetServices<IRandomMediaService>()
.OrderBy(s => s.StoreItem.Order))
{
var btn = item.StoreItem.ToButton(From.Id);
if ((counter & 1) == 0) // 0 - even, 1 - odd
{
list = new List<InlineKeyboardButton>
{
btn
};
}
else
{
list.Add(btn);
rowList.Add(list);
}
counter++;
}
await Bot.SendTextMessageAsync(Chat.Id, "Чего пожелаете?", replyMarkup: new InlineKeyboardMarkup(rowList));
}
[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 = "Спасибо за покупку";
if (!TryGetMediaService(purchaseType, out var mediaService))
{
await AnswerQuery(query.Id, "Unknown purchasable.");
return;
}
var storeItem = mediaService.StoreItem;
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);
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)
{
mediaService = _provider.GetServices<IRandomMediaService>()
.FirstOrDefault(s => s.StoreItem.Id == name);
return mediaService != null;
}
private async Task AnswerQuery(string id, string? message = null)
{
await Bot.AnswerCallbackQueryAsync(id, message);
}
}