コード例 #1
0
        private static async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs)
        {
            var message = messageEventArgs.Message;

            if (message == null || message.Type != MessageType.Text)
            {
                return;
            }

            var userFrom = message.From;

            switch (message.Text.Split(' ').First())
            {
            case "/start":

                var loadUser = _userManagementService.CheckUserRegistration(userFrom.Id.ToString());

                if (loadUser == null)
                {
                    UserRegistrationModel model = new UserRegistrationModel()
                    {
                        ChatId   = userFrom.Id.ToString(),
                        UserName = userFrom.Username
                    };

                    var registeredUser = _userManagementService.SignUpApplicationUser(model);

                    await TelegramBotEndpoint.SendTextMessageAsync(
                        message.Chat.Id,
                        $"Hello {registeredUser.UserName}, you are signed up!");

                    break;
                }
                else
                {
                    await TelegramBotEndpoint.SendTextMessageAsync(
                        message.Chat.Id,
                        $"Hello {loadUser.UserName}, welcome back!");

                    break;
                }

            case "/getBook":

                if (_bookManagementService.CheckQuantityGet(userFrom.Id))
                {
                    byte[] bytes = _bookManagementService.GetBook();

                    await TelegramBotEndpoint
                    .SendDocumentAsync(
                        new ChatId(messageEventArgs.Message.Chat.Id),
                        new InputOnlineFile(new MemoryStream(bytes), "Book"));

                    _bookManagementService.AddBookRequestRecord(userFrom.Id);
                }
                else
                {
                    await TelegramBotEndpoint
                    .SendTextMessageAsync(
                        new ChatId(messageEventArgs.Message.Chat.Id),
                        $"You've already got 3 books today, try tomorrow");
                }

                break;
            }
        }