Files
telegram-bot/modules/ContentStore/Handler/Store.cs
T

166 lines
5.3 KiB
C#
Raw 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;
private readonly Dictionary<string, StoreItem> _storeItemMap = new()
{
{ "cat", new StoreItem("cat", "Котэ", 40) },
{ "dog", new StoreItem("dog", "Пёсель", 40) },
{ "boobs", new StoreItem("boobs", "Сиськи", 60) },
{ "anim_boobs", new StoreItem("anim_boobs", "Сиськи движущиеся", 120) }
};
public Store(IServiceProvider provider)
{
_reputation = provider.GetService<IReputation>();
_provider = provider;
}
[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 _storeItemMap)
{
var btn = item.Value.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 (!_storeItemMap.TryGetValue(purchaseType, out var storeItem)
|| !TryGetMediaService(purchaseType, out var mediaService))
{
await AnswerQuery(query.Id, "Unknown purchasable.");
return;
}
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)
{
var serviceType = name switch
{
"boobs" => typeof(OBoobsService),
"cat" => typeof(CatApiService),
"dog" => typeof(DogApiService),
"anim_boobs" => typeof(AnimBoobsService),
_ => null
};
if (serviceType == null)
{
mediaService = null;
return false;
}
mediaService = _provider.GetService(serviceType) as IRandomMediaService;
return mediaService != null;
}
private async Task AnswerQuery(string id, string? message = null)
{
await Bot.AnswerCallbackQueryAsync(id, message);
}
}