예제 #1
0
        /// <summary>
        /// Отправить сообщение
        /// </summary>
        /// <param name="botMessage"></param>
        /// <param name="EditMessageId"></param>
        /// <param name="ReplyToMessageId"></param>
        /// <returns></returns>
        protected async Task <Message> SendMessage(BotMessage botMessage, int EditMessageId = 0, int ReplyToMessageId = 0, bool DisableWeb = true)
        {
            try
            {
                if (botMessage != null && this.Update.CallbackQuery != null && this.CallBackQueryId != null)
                {
                    await AnswerCallback(botMessage.CallBackTitleText);
                }

                if (botMessage != null && EditMessageId != 0)
                {
                    return(await TelegramClient.EditMessageTextAsync(this.ChatId, EditMessageId, botMessage.TextMessage, ParseMode.Html, true, botMessage.MessageReplyMarkup));
                }

                if (botMessage != null && botMessage.TextMessage != null)
                {
                    return(await TelegramClient.SendTextMessageAsync(this.ChatId, botMessage.TextMessage, ParseMode.Html, DisableWeb, false, ReplyToMessageId, botMessage.MessageReplyMarkup));
                }

                else
                {
                    return(null);
                }
            }

            catch (Exception e)
            {
                // await TelegramClient.SendTextMessageAsync(this.ChatId, botMessage.TextMessage, ParseMode.Html, false, false, ReplyToMessageId, botMessage.MessageReplyMarkup);

                return(null);
            }
        }
예제 #2
0
        /// <summary>
        /// Изменить сообщение
        /// </summary>
        /// <param name="botMessage"></param>
        /// <returns></returns>
        protected async Task <Message> EditMessage(BotMessage botMessage)
        {
            try
            {
                if (botMessage != null && this.Update.CallbackQuery != null && this.CallBackQueryId != null)
                {
                    await AnswerCallback(botMessage.CallBackTitleText);
                }

                if (botMessage != null && botMessage.TextMessage != null)
                {
                    return(await TelegramClient.EditMessageTextAsync(this.ChatId, this.MessageId, botMessage.TextMessage, ParseMode.Html, true, botMessage.MessageReplyMarkup));
                }



                else
                {
                    return(null);
                }
            }

            catch
            {
                return(null);
                // return await TelegramClient.SendTextMessageAsync(this.ChatId, botMessage.TextMessage, ParseMode.Html, false, false, 0, botMessage.MessageReplyMarkup);
            }
        }
        private void Flush(Update update, SubscriberRecord subscriberRecord)
        {
            var currentSessionImages = ItemsQueueRepo.GetCurrentSessionImages(subscriberRecord).ToArray();

            if (!currentSessionImages.Any())
            {
                TelegramClient.SendTextMessageAsync(subscriberRecord.ChatId, "چیزی برای تبدیل به پی دی اف وجود ندارد.",
                                                    replyMarkup: Keyboards.FlushMarkup);
                return;
            }

            TelegramClient.SendTextMessageAsync(subscriberRecord.ChatId, $"در حال بارگزاری { currentSessionImages.Length } فایل",
                                                replyMarkup: Keyboards.FlushMarkup);
            var progreessMsgId = TelegramClient.SendTextMessageAsync(subscriberRecord.ChatId, GetProgressString(0, 10)).Result.MessageId;

            var memStream = new MemoryStream();

            using (var pdfWriter = new PdfWriter(memStream))
                using (var pdfDoc = new PdfDocument(pdfWriter))
                    using (var document = new iText.Layout.Document(pdfDoc, new PageSize(0, 0)))
                    {
                        for (var i = 0; i < currentSessionImages.Length; i++)
                        {
                            var currentSessionImage = currentSessionImages[i];

                            switch (currentSessionImage.ItemType)
                            {
                            case ItemRecord.ItemTypes.IMAGE:
                            {
                                var imageStream = new MemoryStream();
                                var result      = TelegramClient
                                                  .GetInfoAndDownloadFileAsync(currentSessionImage.FileId, imageStream).Result;

                                var image = new Image(ImageDataFactory.Create(imageStream.ToArray()));
                                pdfDoc.AddNewPage(new PageSize(image.GetImageWidth(), image.GetImageHeight()));
                                image.SetFixedPosition(pdfDoc.GetNumberOfPages(), 0, 0);
                                document.Add(image);
                            }
                            break;

                            case ItemRecord.ItemTypes.PDF_FILE:
                            {
                                try
                                {
                                    var memoryStream = new MemoryStream();
                                    var result       = TelegramClient.GetInfoAndDownloadFileAsync(currentSessionImage.FileId, memoryStream).Result;

                                    var bytes      = memoryStream.ToArray();
                                    var tempStream = new MemoryStream(bytes);

                                    using (var pdfReader = new PdfReader(tempStream))
                                    {
                                        using (var newPdfDoc = new PdfDocument(pdfReader))
                                        {
                                            newPdfDoc.CopyPagesTo(1,
                                                                  newPdfDoc.GetNumberOfPages(),
                                                                  pdfDoc);
                                        }
                                    }

                                    tempStream.Close();
                                    memoryStream.Close();
                                }
                                catch (Exception exception)
                                {
                                    var baseException = exception.GetBaseException();
                                    Logger.LogError(baseException, "Read Pdf File Failed: {Message} -> {StackTrace}", baseException.Message, baseException.StackTrace);
                                }
                            }
                            break;

                            default:
                                break;
                            }

                            TelegramClient.EditMessageTextAsync(subscriberRecord.ChatId, progreessMsgId,
                                                                GetProgressString(i + 1, currentSessionImages.Length));
                        }
                    }


            var resultEdit = TelegramClient.EditMessageTextAsync(subscriberRecord.ChatId, progreessMsgId, "در حال آپلود فایل...").Result;

            SendPdf(subscriberRecord, memStream);
            memStream.Close();
            ItemsQueueRepo.ClearCurrentSessionImages(subscriberRecord);
        }