示例#1
0
    public async Task <UserProfilePhotos> GetUserProfilePhotosAsync(
        long userId,
        bool evictBefore = false
        )
    {
        var userProfilePhotos = await _cacheService.GetOrSetAsync(
            cacheKey : GetCacheKey(userId),
            evictBefore : evictBefore,
            action : async() => {
            var userProfilePhotos = await _botClient.GetUserProfilePhotosAsync(userId);
            return(userProfilePhotos);
        }
            );

        return(userProfilePhotos);
    }
        private async Task SendPicture(long chatId, PlayerCountViewModel player, string msg)
        {
            UserProfilePhotos userProfilePhotos = null;

            try
            {
                userProfilePhotos = await _client.GetUserProfilePhotosAsync(player.UserId);
            }
            catch (Exception e)
            {
                var defaultConsoleColor = Console.BackgroundColor;
                Console.BackgroundColor = ConsoleColor.Red;
                Console.WriteLine(@"Exception creating picture, YearWinnerJob: " + e.Message);
                Console.BackgroundColor = defaultConsoleColor;
            }

            await using var winnerImage = await GetWinnerImage(userProfilePhotos);

            await _client.TrySendPhotoAsync(chatId, new InputOnlineFile(winnerImage), msg, ParseMode.Html);
        }
        public async Task <byte[]> GetPhotoAsync(int userId)
        {
            var photos = await _telegramClient.GetUserProfilePhotosAsync(userId, 0, 1);

            var photoId = photos.Photos.FirstOrDefault()?.FirstOrDefault()?.FileId;

            if (photoId == null)
            {
                return(null);
            }

            var file = await _telegramClient.GetFileAsync(photoId);

            using (var stream = new MemoryStream())
            {
                await _telegramClient.DownloadFileAsync(file.FilePath, stream);

                var bytes = stream.ToArray();
                return(bytes);
            }
        }
        public async Task Execute(params long[] chatIds)
        {
            for (long i = 0; i < chatIds.Length; i++)
            {
                try
                {
                    var monthWinner = await _repository.GetWinnerForMonthAsync(chatIds[i], DateTime.Today);

                    if (monthWinner != null)
                    {
                        var mention = monthWinner.GetUserMention();
                        var message = $"{Messages.MonthWinner}{Environment.NewLine}\u269C {mention} \u269C{Environment.NewLine}{Messages.Congrats}";

                        UserProfilePhotos userProfilePhotos = null;

                        try
                        {
                            userProfilePhotos = await _client.GetUserProfilePhotosAsync(monthWinner.UserId);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }

                        using var winnerImage = await GetWinnerImage(userProfilePhotos);

                        await _client.TrySendPhotoAsync(chatIds[i], new InputOnlineFile(winnerImage), message, ParseMode.Markdown);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(@"Exception when executing MonthWinnerJob: " + e.Message);
                    continue;
                }
            }
        }