mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using West.TelegramBot.ChatQuotes.Model.Quote;
|
|
using Message = Telegram.Bot.Types.Message;
|
|
|
|
namespace West.TelegramBot.ChatQuotes;
|
|
|
|
public class QuoteGeneratorService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly MediaTypeHeaderValue _mediaType;
|
|
|
|
public QuoteGeneratorService(IConfiguration configuration)
|
|
{
|
|
_httpClient = new HttpClient
|
|
{
|
|
BaseAddress = new Uri(configuration.GetValue<string>("QuoteApiUrl"))
|
|
};
|
|
|
|
_mediaType = MediaTypeHeaderValue.Parse("application/json");
|
|
}
|
|
|
|
public async Task<Stream?> GenerateQuoteImage(Message msg)
|
|
{
|
|
var request = new Request(msg);
|
|
var content = new ByteArrayContent(Encoding.UTF8.GetBytes(request.ToJson().ToCharArray()))
|
|
{
|
|
Headers =
|
|
{
|
|
ContentType = _mediaType
|
|
}
|
|
};
|
|
|
|
var resp = await _httpClient.PostAsync("/generate", content);
|
|
var quoteResponse = JsonConvert.DeserializeObject<Response>(await resp.Content.ReadAsStringAsync());
|
|
|
|
return quoteResponse.Ok ? new MemoryStream(Convert.FromBase64String(quoteResponse.Result.Image)) : null;
|
|
}
|
|
} |