Esempio n. 1
0
        public async Task ProcessIncomingBotMessage(ChatMessage message)
        {
            if (message.Author == null)
            {
                throw new InvalidOperationException("Message.Author cannot be null");
            }

            var action = await botService.Process(message);

            if (action == null)
            {
                return;
            }

            switch (action.Action)
            {
            case "Say":
                var botMessage = new ChatMessage
                {
                    Date    = DateTime.UtcNow,
                    Message = await messageTemplateService.Interpolate(action.Parameters, message.Author),
                    Source  = ApiSource.RamblerBot
                };

                var ramblerBot = await authorService.GetAuthors()
                                 .FirstOrDefaultAsync(x => x.Source == ApiSource.RamblerBot &&
                                                      x.Name == "RamblerBot" &&
                                                      !string.IsNullOrWhiteSpace(x.SourceAuthorId));

                if (ramblerBot == null)
                {
                    ramblerBot = new Author
                    {
                        Name           = "RamblerBot",
                        Source         = message.Author.Source,
                        SourceAuthorId = Guid.NewGuid().ToString()
                    };
                }
                else
                {
                    botMessage.AuthorId = ramblerBot.Id;
                }

                botMessage.Author = ramblerBot;

                await ProcessOutgoingMessage(botMessage);

                break;

            case "Play media":
                await chatHubContext.Clients.All.SendAsync("PlayMedia", action.Parameters);

                break;

            default:
                break;
            }
        }