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