Beispiel #1
0
        private void HandlerError(Result result, BotEventArgs botEventArgs)
        {
            LoggerHolder.Instance.Error(result.ToString());
            BotTextMessage errorMessage = new BotTextMessage("Something went wrong.");

            errorMessage.Send(_apiProvider, botEventArgs.Sender);
            if (_sendErrorLogToUser)
            {
                BotTextMessage errorlogMessage = new BotTextMessage(result.ToString());
                errorlogMessage.Send(_apiProvider, botEventArgs.Sender);
            }
        }
Beispiel #2
0
        private async Task HandlerError(BotException exception, BotEventArgs botEventArgs)
        {
            LoggerHolder.Instance.Error(exception.Message);

            if (_sendErrorLogToUser)
            {
                var errorLogMessage = new BotTextMessage(exception.Message);
                await errorLogMessage.SendAsync(_apiProvider, botEventArgs.SenderInfo);
            }
            else
            {
                var errorMessage = new BotTextMessage("Something went wrong.");
                await errorMessage.SendAsync(_apiProvider, botEventArgs.SenderInfo);
            }
        }
Beispiel #3
0
        private void ClientOnOnMessageReceived(NewMessage obj)
        {
            try
            {
                _client.ConversationLookup.TryGetValue(obj.channel, out var conversation);
                _client.UserLookup.TryGetValue(obj.user, out var user);

                var chatType = BotChatType.Channel;
                var chatId   = obj.channel;
                var chatName = obj.channel;

                if (conversation is Channel cnn)
                {
                    chatType = cnn.is_group ? BotChatType.Group : BotChatType.Channel;
                    chatName = cnn.name;
                }
                else if (conversation is DirectMessageConversation dmc)
                {
                    chatType = BotChatType.Private;
                    if (_client.UserLookup.TryGetValue(dmc.user, out var privateUser))
                    {
                        chatName = privateUser.name;
                    }
                    else
                    {
                        chatName = dmc.user;
                    }
                }

                var message = new BotTextMessage
                {
                    Id   = obj.id.ToString(),
                    Date = obj.ts,
                    Text = obj.text,
                    Chat = new BotChat
                    {
                        ChatType = chatType,
                        Id       = chatId,
                        Name     = chatName
                    },
                    User = new SlackBotUser
                    {
                        Id        = user?.id ?? obj.user,
                        Name      = user?.name ?? obj.user,
                        Color     = user?.color,
                        Presence  = user?.presence,
                        Email     = user?.profile?.email,
                        FirstName = user?.profile?.first_name,
                        LastName  = user?.profile?.last_name,
                        RealName  = user?.profile?.real_name,
                        Skype     = user?.profile?.skype,
                        Phone     = user?.profile?.phone
                    }
                };
                TextMessageReceived?.Invoke(this, new EventArgs <BotTextMessage>(message));
            }
            catch (Exception ex)
            {
                Core.Log.Write(ex);
            }
        }
Beispiel #4
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);
            }
        }
Beispiel #5
0
        public Task <Result <IBotMessage> > Execute(CommandArgumentContainer args)
        {
            IBotMessage message = new BotTextMessage("Pong!");

            return(Task.FromResult(Result.Ok(message)));
        }
Beispiel #6
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;
            }
        }