예제 #1
0
        private async void Bot_OnMessage(object sender, TBot.Args.MessageEventArgs e)
        {
            try
            {
                if (e.Message is null)
                {
                    return;
                }

                switch (e.Message.Type)
                {
                case TBot.Types.Enums.MessageType.Text:
                    if (string.IsNullOrWhiteSpace(e.Message.Text))
                    {
                        return;
                    }
                    var message = new BotTextMessage
                    {
                        Id   = e.Message.MessageId.ToString(),
                        Date = e.Message.Date,
                        Text = e.Message.Text,
                        Chat = new TelegramBotChat
                        {
                            Id        = e.Message.Chat.Id.ToString(),
                            ChatType  = (BotChatType)e.Message.Chat.Type,
                            Name      = e.Message.Chat.Title,
                            Title     = e.Message.Chat.Title,
                            Username  = e.Message.Chat.Username,
                            FirstName = e.Message.Chat.FirstName,
                            LastName  = e.Message.Chat.LastName
                        },
                        User = new TelegramBotUser
                        {
                            Id        = e.Message.From.Id.ToString(),
                            Name      = e.Message.From.Username,
                            FirstName = e.Message.From.FirstName,
                            LastName  = e.Message.From.LastName
                        }
                    };
                    TextMessageReceived?.Invoke(this, new EventArgs <BotTextMessage>(message));
                    break;

                case TBot.Types.Enums.MessageType.Photo:
                    foreach (var photo in e.Message.Photo)
                    {
                        var photoFileInfo = await _bot.GetFileAsync(photo.FileId);

                        var photoMessage = new BotPhotoMessage
                        {
                            Id          = e.Message.MessageId.ToString(),
                            Date        = e.Message.Date,
                            PhotoWidth  = photo.Width,
                            PhotoHeight = photo.Height,
                            PhotoId     = photo.FileId,
                            PhotoSize   = photo.FileSize,
                            PhotoName   = photoFileInfo.FilePath,
                            PhotoStream = new Lazy <Stream>(() =>
                            {
                                var ms = new MemoryStream();
                                _bot.DownloadFileAsync(photoFileInfo.FilePath, ms).WaitAsync();
                                return(ms);
                            }),
                            Chat = new TelegramBotChat
                            {
                                Id        = e.Message.Chat.Id.ToString(),
                                ChatType  = (BotChatType)e.Message.Chat.Type,
                                Name      = e.Message.Chat.Title,
                                Title     = e.Message.Chat.Title,
                                Username  = e.Message.Chat.Username,
                                FirstName = e.Message.Chat.FirstName,
                                LastName  = e.Message.Chat.LastName
                            },
                            User = new TelegramBotUser
                            {
                                Id        = e.Message.From.Id.ToString(),
                                Name      = e.Message.From.Username,
                                FirstName = e.Message.From.FirstName,
                                LastName  = e.Message.From.LastName
                            }
                        };
                        PhotoMessageReceived?.Invoke(this, new EventArgs <BotPhotoMessage>(photoMessage));
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                Core.Log.Write(ex);
            }
        }
예제 #2
0
        private void Bot_OnMessage(object sender, TBot.Args.MessageEventArgs e)
        {
            if (e.Message == null)
            {
                return;
            }

            switch (e.Message.Type)
            {
            case TBot.Types.Enums.MessageType.TextMessage:
                if (string.IsNullOrWhiteSpace(e.Message.Text))
                {
                    return;
                }
                var message = new BotTextMessage
                {
                    Id   = e.Message.MessageId.ToString(),
                    Date = e.Message.Date,
                    Text = e.Message.Text,
                    Chat = new TelegramBotChat
                    {
                        Id        = e.Message.Chat.Id.ToString(),
                        ChatType  = (BotChatType)e.Message.Chat.Type,
                        Name      = e.Message.Chat.Title,
                        Title     = e.Message.Chat.Title,
                        Username  = e.Message.Chat.Username,
                        FirstName = e.Message.Chat.FirstName,
                        LastName  = e.Message.Chat.LastName
                    },
                    User = new TelegramBotUser
                    {
                        Id        = e.Message.From.Id.ToString(),
                        Name      = e.Message.From.Username,
                        FirstName = e.Message.From.FirstName,
                        LastName  = e.Message.From.LastName
                    }
                };
                TextMessageReceived?.Invoke(this, new EventArgs <BotTextMessage>(message));
                break;

            case TBot.Types.Enums.MessageType.PhotoMessage:
                foreach (var photo in e.Message.Photo)
                {
                    var photoMessage = new BotPhotoMessage
                    {
                        Id          = e.Message.MessageId.ToString(),
                        Date        = e.Message.Date,
                        PhotoWidth  = photo.Width,
                        PhotoHeight = photo.Height,
                        PhotoId     = photo.FileId,
                        PhotoSize   = photo.FileSize,
                        PhotoName   = photo.FilePath,
                        PhotoStream = new Lazy <Stream>(() =>
                        {
                            var file = _bot.GetFileAsync(photo.FileId).WaitAndResults();
                            return(file.FileStream);
                        }),
                        Chat = new TelegramBotChat
                        {
                            Id        = e.Message.Chat.Id.ToString(),
                            ChatType  = (BotChatType)e.Message.Chat.Type,
                            Name      = e.Message.Chat.Title,
                            Title     = e.Message.Chat.Title,
                            Username  = e.Message.Chat.Username,
                            FirstName = e.Message.Chat.FirstName,
                            LastName  = e.Message.Chat.LastName
                        },
                        User = new TelegramBotUser
                        {
                            Id        = e.Message.From.Id.ToString(),
                            Name      = e.Message.From.Username,
                            FirstName = e.Message.From.FirstName,
                            LastName  = e.Message.From.LastName
                        }
                    };
                    PhotoMessageReceived?.Invoke(this, new EventArgs <BotPhotoMessage>(photoMessage));
                }
                break;
            }
        }