public Task ReceiveAsync(Message message, CancellationToken cancellationToken = default(CancellationToken))
        {
            MappedDiagnosticsLogicalContext.Set("originator", message.From.ToIdentity().ToString());
            return(RunRequestAsync(async() =>
            {
                try
                {
                    var contact = await _contactService.GetContactAsync(message.From, cancellationToken);
                    if (await IsActiveAsync(message, cancellationToken))
                    {
                        await SendComposingAsync(message.From, cancellationToken);
                        LogRequest(message);

                        var isInMPASession = await _mpaService.IsInMPASessionAsync(message.From, cancellationToken);
                        var isFirstInteration = await _context.IsFirstInteractionAsync(message.From, cancellationToken);

                        if (!isInMPASession && isFirstInteration)
                        {
                            message.Content = new PlainText {
                                Text = FIRST_INTERACTION_COMMAND
                            };
                            await _context.SetFirstInteractionAsync(message.From, cancellationToken);
                        }

                        await ReceiveMessageAsync(message, cancellationToken);
                    }
                    else
                    {
                        // Do nothing!
                        if (message.Content.ToString().ToLower().Contains(ACTIVATION_COMMAND.ToLower()))
                        {
                            try
                            {
                                await _context.SetSuspendedAsync(message.From, false, cancellationToken);
                                var result = await _mpaService.SendToMPA(FIRST_INTERACTION_COMMAND, message.From, cancellationToken);
                            }
                            catch (Exception e)
                            {
                                _logger.Error($"Error on activate user! User Node: {message.From}");
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(e, $"Some error ocurred. Trying to send Error Message to user. User: {message.From}");
                    await OnExceptionAsync(message, e, cancellationToken);
                }
            }));
        }
Example #2
0
        private async Task SendCommandToMPAAsync(string commmand, Message messageOriginator, CancellationToken cancellationToken)
        {
            var MessageToMPA = new Message(messageOriginator.Id)
            {
                To       = messageOriginator.From,
                From     = messageOriginator.From,
                Content  = PlainText.Parse(commmand),
                Metadata = messageOriginator.Metadata
            };

            var firstNavResult = await _mpaService.SendToMPA(MessageToMPA, cancellationToken);

            if (firstNavResult.NavigationState == NavigationState.Error)
            {
                _logger.Error($"Error on sending message to MPA. User from: {messageOriginator.From} message: {(MessageToMPA.Content as PlainText).Text}");

                var result = await _mpaService.SendToMPA(GenericErrorCommand, messageOriginator.From, cancellationToken);

                if (result.NavigationState == NavigationState.Error)
                {
                    await _genericErrorService.SendGenericErrorAsync(messageOriginator.From, cancellationToken);
                }
            }
        }