コード例 #1
0
    public async Task SendEpicGamesBroadcaster(long chatId)
    {
        var games = await GetFreeGamesParsed();

        var freeGames = games.FirstOrDefault();

        if (freeGames == null)
        {
            return;
        }

        var productUrl   = freeGames.ProductUrl;
        var productTitle = freeGames.ProductTitle;

        var chat = await _botClient.GetChatAsync(chatId);

        if (chat.LinkedChatId != null)
        {
            chatId = chat.LinkedChatId.Value;
        }

        var isHistoryExist = await _rssService.IsHistoryExist(chatId, productUrl);

        if (isHistoryExist)
        {
            Log.Information(
                "Seem EpicGames with Title: '{Title}' already sent to ChatId: {ChannelId}",
                productTitle,
                chatId
                );
        }
        else
        {
            await _botClient.SendPhotoAsync(
                chatId : chatId,
                photo : freeGames.Images.ToString(),
                caption : freeGames.Detail,
                parseMode : ParseMode.Html
                );

            await _rssService.SaveRssHistoryAsync(
                new RssHistory()
            {
                ChatId      = chatId,
                Title       = productTitle,
                Url         = productUrl,
                PublishDate = DateTime.UtcNow,
                Author      = "EpicGames Free",
                CreatedAt   = DateTime.UtcNow,
                RssSource   = "https://store.epicgames.com"
            }
                );
        }
    }
コード例 #2
0
    public async Task ExecuteUrlAsync(
        long chatId,
        string rssUrl
        )
    {
        Log.Information(
            "Reading feed from {ChatId}. Url: {RssUrl}",
            chatId,
            rssUrl
            );

        // var rssFeeds = await FeedReader.ReadAsync(rssUrl, autoRedirect: false);
        var rssFeeds = await RssFeedUtil.OpenSyndicationFeed(rssUrl);

        // var rssTitle = rssFeeds.Title;

        var rssTitle = rssFeeds.Title.Text;
        var rssFeed  = rssFeeds.Items.FirstOrDefault();

        var rssFeedTitle      = rssFeed.Title.Text.Trim();
        var rssPublishDate    = rssFeed.PublishDate.Year.Equals(0001) ? rssFeed.LastUpdatedTime : rssFeed.PublishDate;
        var rssPublishDateStr = rssPublishDate.ToString("yyyy-MM-dd HH:mm:ss");
        var rssFeedAuthor     = rssFeed.Authors.FirstOrDefault()?.Name;
        var rssFeedLink       = rssFeed.Links.FirstOrDefault()?.Uri.ToString();

        Log.Debug(
            "Getting last history for {ChatId} url {RssUrl}",
            chatId,
            rssUrl
            );

        Log.Debug("CurrentArticleDate: {Date}", rssPublishDate);
        Log.Debug("Prepare sending article to ChatId {ChatId}", chatId);

        var isExist = await _rssService.IsHistoryExist(chatId, rssFeedLink);

        if (isExist)
        {
            Log.Information(
                "Last article from feed '{RssUrl}' has sent to {ChatId}",
                rssUrl,
                chatId
                );

            return;
        }

        Log.Information(
            "Sending article from feed {RssUrl} to {ChatId}",
            rssUrl,
            chatId
            );

        var category    = rssFeed.Categories.Select(syndicationCategory => syndicationCategory.Name).ToList().MkJoin(", ");
        var htmlMessage = HtmlMessage.Empty;

        var disableWebPagePreview = false;

        if (rssUrl.IsGithubReleaseUrl())
        {
            var listUrlAssetsList = await _octokitApiService.GetLatestReleaseAssetsList(rssUrl);

            if (listUrlAssetsList != null)
            {
                htmlMessage.Br()
                .Append(listUrlAssetsList)
                .Br()
                .Br();
            }
            else
            {
                htmlMessage.Br();
            }

            htmlMessage.Text("#github #release");

            disableWebPagePreview = true;
        }
        else
        {
            htmlMessage.TextBr($"{rssTitle} - {rssFeedTitle}");

            htmlMessage.TextBr($"{rssFeedLink}");

            if (category.IsNotNullOrEmpty())
            {
                htmlMessage.Text($"\nTags: {category}");
            }
        }

        try
        {
            await _botClient.SendTextMessageAsync(
                chatId : chatId,
                text : htmlMessage.ToString(),
                disableWebPagePreview : disableWebPagePreview,
                parseMode : ParseMode.Html
                );

            Log.Debug(
                "Writing to RSS History for ChatId: {ChatId}, Rss: {UrlFeed}",
                chatId,
                rssUrl
                );

            await _rssService.SaveRssHistoryAsync(
                new RssHistory
            {
                Url         = rssFeedLink,
                RssSource   = rssUrl,
                ChatId      = chatId,
                Title       = rssFeedTitle,
                PublishDate = rssPublishDate,
                Author      = rssFeedAuthor ?? "N/A",
                CreatedAt   = DateTime.Now
            }
                );
        }
        catch (Exception ex)
        {
            Log.Error(
                ex.Demystify(),
                "RSS Broadcaster Error at ChatId {ChatId}. Url: {Url}",
                chatId,
                rssUrl
                );

            if (ex.Message.ContainsListStr(
                    "blocked",
                    "not found",
                    "deactivated"
                    ))
            {
                UnregisterRssFeed(chatId, rssUrl);
            }
        }
    }