using System.Text.Json; using JobsMedical.Web.Models; namespace JobsMedical.Web.Services.Scraping; /// /// Bale (Iranian messenger) source via its Telegram-compatible Bot API getUpdates. Enabled + /// bot token come from admin settings (DB). The bot must be a member of the channels it reads. /// public class BaleListingSource : IListingSource { private const string BaseUrl = "https://tapi.bale.ai"; private readonly ScrapeHttpClients _clients; private readonly ILogger _log; public BaleListingSource(ScrapeHttpClients clients, ILogger log) { _clients = clients; _log = log; } public string Name => "بله"; public async Task> FetchAsync(AppSetting s, CancellationToken ct = default) { if (!s.BaleEnabled || string.IsNullOrWhiteSpace(s.BaleBotToken)) return Array.Empty(); try { var client = _clients.For(s); var body = await client.GetStringAsync($"{BaseUrl}/bot{s.BaleBotToken}/getUpdates", ct); using var doc = JsonDocument.Parse(body); if (!doc.RootElement.TryGetProperty("result", out var result) || result.ValueKind != JsonValueKind.Array) return Array.Empty(); var items = new List(); foreach (var update in result.EnumerateArray()) { var text = TextOf(update, "channel_post") ?? TextOf(update, "message"); if (!string.IsNullOrWhiteSpace(text) && text!.Trim().Length >= 15) items.Add(new ScrapedItem("بله", text.Trim())); } return items; } catch (Exception ex) { _log.LogWarning(ex, "Bale fetch failed."); return Array.Empty(); } } private static string? TextOf(JsonElement update, string key) => update.TryGetProperty(key, out var m) && m.TryGetProperty("text", out var t) && t.ValueKind == JsonValueKind.String ? t.GetString() : null; }