mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Implemented image fetching from RSS Item
This commit is contained in:
@@ -39,8 +39,12 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
_logger.LogDebug($"Message for {message.ChatId.Identifier} dequeued.");
|
_logger.LogDebug($"Message for {message.ChatId.Identifier} dequeued.");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _bot.BotClient.SendTextMessageAsync(message.ChatId, message.Text, message.ParseMode,
|
if (string.IsNullOrWhiteSpace(message.ImageUrl))
|
||||||
message.DisableWebPagePreview);
|
await _bot.BotClient.SendTextMessageAsync(message.ChatId, message.Text, message.ParseMode,
|
||||||
|
message.DisableWebPagePreview);
|
||||||
|
else
|
||||||
|
await _bot.BotClient.SendPhotoAsync(message.ChatId, message.ImageUrl, message.Text,
|
||||||
|
message.ParseMode, message.DisableWebPagePreview);
|
||||||
}
|
}
|
||||||
catch (ApiRequestException e)
|
catch (ApiRequestException e)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Linq;
|
||||||
using CodeHollow.FeedReader;
|
using CodeHollow.FeedReader;
|
||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
using Kruzya.TelegramBot.RichSiteSummary.Data;
|
using Kruzya.TelegramBot.RichSiteSummary.Data;
|
||||||
@@ -14,6 +19,10 @@ using Telegram.Bot.Types;
|
|||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
using Feed = Kruzya.TelegramBot.RichSiteSummary.Data.Feed;
|
using Feed = Kruzya.TelegramBot.RichSiteSummary.Data.Feed;
|
||||||
|
|
||||||
|
using CodeHollow.FeedReader.Feeds;
|
||||||
|
using CodeHollow.FeedReader.Feeds.Itunes;
|
||||||
|
using CodeHollow.FeedReader.Feeds.MediaRSS;
|
||||||
|
|
||||||
namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -123,6 +132,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
|
|
||||||
Post feedPost;
|
Post feedPost;
|
||||||
var newPosts = new List<Post>();
|
var newPosts = new List<Post>();
|
||||||
|
var postsImages = new Dictionary<Post, string>();
|
||||||
foreach (var post in parsedFeed.Items)
|
foreach (var post in parsedFeed.Items)
|
||||||
{
|
{
|
||||||
feedPost = await dbContext.Posts.ByFeedAndUrl(post.Link, feed);
|
feedPost = await dbContext.Posts.ByFeedAndUrl(post.Link, feed);
|
||||||
@@ -137,7 +147,8 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
feedPost.Title = post.Title;
|
feedPost.Title = post.Title;
|
||||||
feedPost.Url = post.Link;
|
feedPost.Url = post.Link;
|
||||||
feedPost.PostedAt = post.PublishingDate.GetValueOrDefault(DateTime.Now);
|
feedPost.PostedAt = post.PublishingDate.GetValueOrDefault(DateTime.Now);
|
||||||
|
|
||||||
|
postsImages.Add(feedPost, await FetchImageUrl(post));
|
||||||
newPosts.Add(feedPost);
|
newPosts.Add(feedPost);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +163,7 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
var message = new UserMessage()
|
var message = new UserMessage()
|
||||||
{
|
{
|
||||||
ChatId = new ChatId(subscriber.SubscriberId), DisableWebPagePreview = true,
|
ChatId = new ChatId(subscriber.SubscriberId), DisableWebPagePreview = true,
|
||||||
ParseMode = ParseMode.Html, Text = text
|
ParseMode = ParseMode.Html, Text = text, ImageUrl = postsImages.GetValueOrDefault(post)
|
||||||
};
|
};
|
||||||
|
|
||||||
_queue.Enqueue(message);
|
_queue.Enqueue(message);
|
||||||
@@ -174,7 +185,80 @@ namespace Kruzya.TelegramBot.RichSiteSummary.Service
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Image parsing
|
||||||
|
|
||||||
|
private async Task<string> FetchImageUrl(FeedItem item)
|
||||||
|
{
|
||||||
|
var feedItem = item.SpecificItem;
|
||||||
|
if (feedItem is Rss20FeedItem)
|
||||||
|
{
|
||||||
|
var enclosure = ((Rss20FeedItem)feedItem).Enclosure;
|
||||||
|
if (enclosure != null && IsValidImage(enclosure.MediaType))
|
||||||
|
{
|
||||||
|
return enclosure.Url;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return await FetchImageUrl(item.Description);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<string> FetchImageUrl(string description)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(description))
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
using var httpClientHandler = new HttpClientHandler() { AllowAutoRedirect = true };
|
||||||
|
using var httpClient = new HttpClient(httpClientHandler);
|
||||||
|
|
||||||
|
var imgRegEx = new Regex(@"<img[^>]+>");
|
||||||
|
var srcRegEx = new Regex("src=\"([^\"]+)\"");
|
||||||
|
var matches = imgRegEx.Matches(description);
|
||||||
|
|
||||||
|
var img = "";
|
||||||
|
foreach (Match match in matches)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var srcData = srcRegEx.Match(match.Value);
|
||||||
|
var content = srcData.Groups[1].Value;
|
||||||
|
|
||||||
|
var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, content));
|
||||||
|
if (response.StatusCode == HttpStatusCode.OK &&
|
||||||
|
IsValidImage(response.Content.Headers.ContentType.MediaType))
|
||||||
|
{
|
||||||
|
img = content;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError($"Caused error when fetching image for RSS post: {e.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return img;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if image is valid for Telegram Bot API.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="contentType">Content-type for received content</param>
|
||||||
|
/// <returns>True, if Telegram maybe can "use" this image, false if not.</returns>
|
||||||
|
private static bool IsValidImage(string contentType)
|
||||||
|
{
|
||||||
|
return new string[]
|
||||||
|
{
|
||||||
|
"image/jpeg",
|
||||||
|
"image/bmp",
|
||||||
|
"image/png"
|
||||||
|
}.Contains(contentType);
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,5 +9,6 @@ namespace Kruzya.TelegramBot.RichSiteSummary
|
|||||||
public string Text;
|
public string Text;
|
||||||
public ParseMode ParseMode;
|
public ParseMode ParseMode;
|
||||||
public bool DisableWebPagePreview;
|
public bool DisableWebPagePreview;
|
||||||
|
public string ImageUrl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user