Esempio n. 1
0
        public async Task ProcessMessageAsync(Message inputMessage)
        {
            try
            {
                IMessage resultMessage = null;

                if (inputMessage == null || string.IsNullOrEmpty(inputMessage.Text))
                {
                    resultMessage = BaseTgMessageHandler.GetDefaultResponse(inputMessage.Chat.Id);
                }

                _logger.Info($"Incoming message from {inputMessage.Chat.Id}. Message content: \"{inputMessage.Text}\"");
                var user = DefineUser(inputMessage.Chat);
                _historyLogger.LogHistory(inputMessage.Chat.Id, DateTime.Now, false, inputMessage.Text);

                var userInput = inputMessage.Text.Split("|||", System.StringSplitOptions.RemoveEmptyEntries);

                var handler = _myMessageHandlers.FirstOrDefault(han => han.UsedUserInput == userInput.FirstOrDefault()) ??
                              _myMessageHandlers.FirstOrDefault(han => han.UsedChatState == user.State);

                resultMessage = handler != null
                    ? handler.GetResponseTo(inputMessage, user)
                    : BaseTgMessageHandler.GetDefaultResponse(inputMessage.Chat.Id);

                await _messageSender.SendMessageToUser(resultMessage);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"Exception happened in TelegramMessageRouter. {ex}");
            }
        }
Esempio n. 2
0
        public async Task SendMessageToUser(IMessage message)
        {
            if (message == null)
            {
                return;
            }
            _logger.Info($"Messaging chat {message.ChatId} with text \"{message.Text}\"");

            _history.LogHistory(message.ChatId, DateTime.Now, true, message.Text);

            var tgMessage = message as TelegramUserMessage;

            if (tgMessage != null)
            {
                if (tgMessage.File != null)
                {
                    await _botClient.SendDocumentAsync(tgMessage.ChatId,
                                                       tgMessage.File,
                                                       tgMessage.Text,
                                                       Telegram.Bot.Types.Enums.ParseMode.Markdown,
                                                       replyMarkup : tgMessage.ReplyMarkup);
                }
                else
                {
                    await _botClient.SendTextMessageAsync(
                        tgMessage.ChatId,
                        tgMessage.Text,
                        Telegram.Bot.Types.Enums.ParseMode.Markdown,
                        replyMarkup : tgMessage.ReplyMarkup);
                }
                return;
            }

            var notificationMessage = message as NotificationMesasge;

            if (notificationMessage != null)
            {
                await _botClient.SendTextMessageAsync(
                    notificationMessage.ChatId,
                    notificationMessage.Text,
                    Telegram.Bot.Types.Enums.ParseMode.Markdown);
            }
        }