Ejemplo n.º 1
0
        private async Task <Message> _extractMessage(Telegram.Bot.Types.Message tgMessage)
        {
            Logger.LogTrace("Message received");
            var conversation = _extractConversation(tgMessage.Chat);
            var person       = _extractPerson(tgMessage.From);

            var attachments = new List <Attachment>();
            var at          = await _extractAttachment(tgMessage);

            if (at != null)
            {
                attachments.Add(at);
            }

            if (tgMessage.Entities != null)
            {
                attachments.AddRange(from entity in tgMessage.Entities
                                     where entity.Type == MessageEntityType.TextLink
                                     select new LinkAttachment(entity.Url));
            }

            // Just forwarded message
            if (tgMessage.ForwardFrom != null)
            {
                var fwdPerson  = _extractPerson(tgMessage.ForwardFrom);
                var fwdMessage = new Message(conversation, fwdPerson, tgMessage.Text, attachments: attachments);
                return(new Message(conversation, person, forwardedMessages: new[] { fwdMessage }));
            }

            // Reply to
            Message[] forwarded = null;
            if (tgMessage.ReplyToMessage != null)
            {
                forwarded = new[]
                {
                    await _extractMessage(tgMessage.ReplyToMessage)
                };
            }

            var text = tgMessage.Text ?? tgMessage.Caption;

            // Remove bot name from command
            if (!string.IsNullOrEmpty(text) && text.StartsWith("/") && !string.IsNullOrEmpty(BotUserName))
            {
                text = text.Split($"@{BotUserName}", 2)[0];
            }

            return(new Message(conversation, person, text, forwarded, attachments));
        }
Ejemplo n.º 2
0
        /**
         * Extracts attachment from the message
         */
        private async Task <Attachment> _extractAttachment(Telegram.Bot.Types.Message tgMessage)
        {
            if (tgMessage.Audio != null)
            {
                var file = await BotClient.GetFileAsync(tgMessage.Audio.FileId);

                return(new AudioAttachment(_getDownloadUrl(file), tgMessage.Audio, tgMessage.Caption,
                                           fileSize: file.FileSize,
                                           mimeType: tgMessage.Audio.MimeType, title: tgMessage.Audio.Title,
                                           performer: tgMessage.Audio.Performer,
                                           duration: Convert.ToUInt64(tgMessage.Audio.Duration)));
            }

            if (tgMessage.Document != null)
            {
                var file = await BotClient.GetFileAsync(tgMessage.Document.FileId);

                return(new FileAttachment(_getDownloadUrl(file), tgMessage.Document,
                                          tgMessage.Caption, tgMessage.Document.FileName, file.FileSize, tgMessage.Document.MimeType));
            }

            if (tgMessage.Animation != null)
            {
                var file = await BotClient.GetFileAsync(tgMessage.Animation.FileId);

                return(new AnimationAttachment(_getDownloadUrl(file), tgMessage.Animation, tgMessage.Caption,
                                               tgMessage.Animation.FileName, file.FileSize, tgMessage.Animation.MimeType,
                                               Convert.ToUInt64(tgMessage.Animation.Duration),
                                               tgMessage.Animation.Width, tgMessage.Animation.Height));
            }

            if (tgMessage.Game != null)
            {
                throw new UnsupportedAttachmentException("game");
            }

            if (tgMessage.Photo != null)
            {
                var photo = tgMessage.Photo.MaxBy(ph => ph.Width).First();
                var file  = await BotClient.GetFileAsync(photo.FileId);

                return(new PhotoAttachment(_getDownloadUrl(file), photo, tgMessage.Caption, fileSize: file.FileSize,
                                           mimeType: MimeTypesMap.GetMimeType(file.FilePath)));
            }

            if (tgMessage.Sticker != null)
            {
                var file = await BotClient.GetFileAsync(tgMessage.Sticker.FileId);

                return(new StickerAttachment(_getDownloadUrl(file), tgMessage.Sticker, fileSize: file.FileSize,
                                             mimeType: "image/webp"));
            }

            if (tgMessage.Video != null)
            {
                var file = await BotClient.GetFileAsync(tgMessage.Video.FileId);

                return(new VideoAttachment(_getDownloadUrl(file), tgMessage.Video, tgMessage.Caption,
                                           fileSize: file.FileSize,
                                           mimeType: tgMessage.Video.MimeType, duration: Convert.ToUInt64(tgMessage.Video.Duration),
                                           width: tgMessage.Video.Width,
                                           height: tgMessage.Video.Height));
            }

            if (tgMessage.Voice != null)
            {
                var file = await BotClient.GetFileAsync(tgMessage.Voice.FileId);

                return(new VoiceAttachment(_getDownloadUrl(file), tgMessage.Voice, tgMessage.Caption,
                                           fileSize: file.FileSize,
                                           mimeType: tgMessage.Voice.MimeType, duration: Convert.ToUInt64(tgMessage.Voice.Duration)));
            }

            if (tgMessage.VideoNote != null)
            {
                // TODO: Mark video note somehow
                var file = await BotClient.GetFileAsync(tgMessage.VideoNote.FileId);

                return(new VideoAttachment(_getDownloadUrl(file), tgMessage.VideoNote, tgMessage.Caption,
                                           fileSize: file.FileSize,
                                           mimeType: "video/mp4", duration: Convert.ToUInt64(tgMessage.VideoNote.Duration),
                                           width: tgMessage.VideoNote.Length,
                                           height: tgMessage.VideoNote.Length));
            }

            if (tgMessage.Contact != null)
            {
                return(new ContactAttachment(tgMessage.Contact.Vcard, tgMessage.Contact));
            }
            if (tgMessage.Location != null)
            {
                return(new PlaceAttachment(tgMessage.Location.Latitude, tgMessage.Location.Longitude,
                                           tgMessage.Location));
            }
            if (tgMessage.Venue != null)
            {
                return(new PlaceAttachment(tgMessage.Venue.Location.Latitude, tgMessage.Venue.Location.Longitude,
                                           tgMessage.Venue.Title, tgMessage.Venue.Address, tgMessage.Venue));
            }

            return(null);
        }