Files
telegram-bot/modules/ChatQuotes/QuoteGeneratorService.cs
T

38 lines
1.1 KiB
C#
Raw Normal View History

2022-01-21 00:10:27 +02:00
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));
}
}