Files
telegram-bot/modules/ContentStore/Service/AnimalAsAService.cs
T

24 lines
825 B
C#

using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.Model;
namespace West.TelegramBot.ContentStore.Service;
public abstract class AnimalAsAService : IRandomMediaService
{
private readonly HttpClient _httpClient;
public abstract StoreItem StoreItem { get; }
protected AnimalAsAService(IHttpClientFactory factory, Uri baseAddress)
{
_httpClient = factory.CreateClient();
_httpClient.BaseAddress = baseAddress;
}
public async Task<RandomMedia> GetRandomMediaAsync(Chat chat, User user)
{
var resp = await _httpClient.GetAsync("images/search");
var catImage = JsonConvert.DeserializeObject<List<AnimalImage>>(await resp.Content.ReadAsStringAsync())![0];
return new RandomMedia(new InputFileUrl(catImage.Url));
}
}