Example #1
0
        internal void UnassignBot(long interactionId, UnassignmentReason reason)
        {
            using (Trace.BotManager.scope())
            {
                try
                {
                    Console.WriteLine("UnassignBot ({0})", interactionId);

                    // Unassign bot
                    IChatBot assignedBot = null;
                    if (_interactionAssignments.ContainsKey(interactionId))
                    {
                        assignedBot = _interactionAssignments[interactionId];
                        _interactionAssignments.Remove(interactionId);
                    }

                    // Let the bot know the bad news
                    if (assignedBot != null)
                    {
                        assignedBot.InteractionUnassigned(interactionId, reason);
                    }
                }
                catch (Exception ex)
                {
                    Trace.BotManager.exception(ex);
                }
            }
        }
Example #2
0
        private void timer_Tick(object sender, EventArgs e)
        {
            foreach (UserAccount account in Accounts)
            {
                if (account.Owner != null && account.Owner is IChatBot)
                {
                    IChatBot bot = (IChatBot)account.Owner;
                    lock (bot.OutgoingMessage)
                    {
                        if (bot.OutgoingMessage.Count > 0)
                        {
                            ChatMessage message = bot.OutgoingMessage.Dequeue();

                            if (!message.Hidden)
                            {
                                lock (chatLog)
                                {
                                    int row = chatLog.Rows.Add();
                                    if (message.Sender.Owner != null)
                                    {
                                        chatLog.Rows[row].DefaultCellStyle.ForeColor = message.Sender.Owner.FontColor;
                                    }
                                    chatLog.Rows[row].Cells["chatNameColumn"].Value = message.Sender.Username;

                                    if (message.Recipient.Owner is IChatBot && message.Recipient.Status != ChatStatus.Available)
                                    {
                                        chatLog.Rows[row].Cells["chatMessageColumn"].Value = message.Content.Message.Insert(message.Content.Message.Length - 1, ", " + message.Recipient.Owner.retrieve(this)[0].Username);
                                    }
                                    else
                                    {
                                        chatLog.Rows[row].Cells["chatMessageColumn"].Value = message.Content.Message;
                                    }

                                    //Autoscroll down
                                    int displayed = chatLog.DisplayedRowCount(true);
                                    if (chatLog.FirstDisplayedScrollingRowIndex >= 0 && displayed < chatLog.RowCount)
                                    {
                                        chatLog.FirstDisplayedScrollingRowIndex = chatLog.RowCount - displayed;
                                    }
                                }
                            }

                            UserAccount recipient = message.Recipient;
                            if (recipient.Owner != null && recipient.Owner is IChatBot)
                            {
                                ((IChatBot)recipient.Owner).receive(message);
                            }

                            if (IncomingMessage != null)
                            {
                                if (message.Sender.Owner != null)
                                {
                                    IncomingMessage(new ChatSourceMessage(message.Source, message.Sender.Owner.Profile.Name, message.Content.Message));
                                }
                            }
                        }
                    }
                }
            }
        }
Example #3
0
 /// <summary>
 /// Throws an exception if the bot is not assigned to the given interaction ID
 /// </summary>
 /// <param name="interactionId">The interaction ID</param>
 /// <param name="bot">The bot</param>
 internal void ValidateBotAssignmentAndThrow(long interactionId, IChatBot bot)
 {
     if (!ValidateBotAssignment(interactionId, bot))
     {
         throw new Exception("Interaction " + interactionId + " is not assigned to " + bot.BotName);
     }
 }
Example #4
0
        private string GetAttribute(long interactionId, string attributeName, IChatBot bot)
        {
            using (Trace.Main.scope())
            {
                try
                {
                    // Protect against cross-bot calls
                    _botManager.ValidateBotAssignmentAndThrow(interactionId, bot);

                    // Is chat?
                    var chat = GetChat(interactionId);
                    if (chat == null)
                    {
                        throw new Exception("Interaction " + interactionId + " was not able to be retrieved as a chat!");
                    }

                    // Return attribute (try to use watched call if possible)
                    return(chat.IsWatching(attributeName)
                        ? chat.GetWatchedStringAttribute(attributeName)
                        : chat.GetStringAttribute(attributeName));
                }
                catch (Exception ex)
                {
                    Trace.Main.exception(ex);
                    return("");
                }
            }
        }
Example #5
0
 public CouchPotatoService(IAuthorizationManager authorizationManager,IChatBot chatBot)
 {
     _couchpotatoApiKey = ConfigurationManager.AppSettings["couchpotato_apikey"];
     _couchpotatoUrl = ConfigurationManager.AppSettings["couchpotato_url"];
     _userAllowedMovieIds = new Dictionary<string, List<string>>();
     _authorizationManager = authorizationManager;
     _chatBot = chatBot;
 }
        public  SlackBotServer(string apiToken)
        {

            _apiToken = apiToken;
            _connector = new SlackConnector.SlackConnector();
            _chatBot = IocContainer.Instance.Resolve<IChatBot>();
            _isConnected = false;
        }
Example #7
0
 public ChatServer(IChatBot chatBot, IChatStreamOperator streamOperator, ServerSettings settings)
 {
     if (settings == null)
     {
         throw new ArgumentNullException(nameof(settings));
     }
     _chatBot        = chatBot ?? throw new ArgumentNullException(nameof(chatBot));
     _streamOperator = streamOperator ?? throw new ArgumentNullException(nameof(streamOperator));
     _listener       = new TcpListener(settings.EndPoint);
 }
Example #8
0
        /*
         * // Keep track of all users in queue to chat with a chat-worker
         * private static ConcurrentQueue<int> _queue = new ConcurrentQueue<int>();
         * private static ConcurrentDictionary<string, int> _inQueue = new ConcurrentDictionary<string, int>();
         */

        //private static ChatQueue _fullChatQueue = new ChatQueue();

        //IEnumerable<ChatGroup> _allChatGroups;
        //IEnumerable<ChatQueue> _allChatQueues;

        public ChatHub(IChatRepository chatRepository,
                       IChatbotRepository chatbotRepository,
                       IChatBot chatBot,
                       UserManager <ApplicationUser> userManager,
                       IOptions <RoleOptions> roleOptions,
                       IOptions <ChatbotKeywordOptions> keywordOptions)
        {
            _chatRepository    = chatRepository;
            _chatBotRepository = chatbotRepository;
            _chatBot           = chatBot;
            _userManager       = userManager;
            _roleOptions       = roleOptions.Value;
            _keywordOptions    = keywordOptions.Value;
        }
Example #9
0
 public MainWindowViewModel(MenuColorViewModel menuColorViewModel,
                            StreamOverlayViewModel streamOverlayViewModel,
                            NameBiddingViewModel nameBiddingViewModel,
                            SettingsViewModel settingsViewModel,
                            IChatBot chatBot)
 {
     MenuColorViewModel     = menuColorViewModel;
     NameBiddingViewModel   = nameBiddingViewModel;
     SettingsViewModel      = settingsViewModel;
     StreamOverlayViewModel = streamOverlayViewModel;
     ChatBot              = chatBot;
     ConnectBotCommand    = new SimpleCommand(x => ChatBot.Connect());
     DisconnectBotCommand = new SimpleCommand(x => ChatBot.Disconnect());
 }
Example #10
0
 private void DoAssignBot(ChatInteraction chat, IChatBot bot, IDictionary <string, string> attributes)
 {
     Trace.BotManager.status("Assigning interaction {} to bot {}", chat.InteractionId.Id, bot.BotName);
     Console.WriteLine("Assigning interaction {0} to bot {1}", chat.InteractionId.Id, bot.BotName);
     _interactionAssignments[chat.InteractionId.Id] = bot;
     try
     {
         bot.InteractionAssigned(chat.InteractionId.Id, attributes);
     }
     catch (Exception ex)
     {
         Trace.BotManager.exception(ex);
     }
 }
Example #11
0
 private void ReassignChat(long interactionId, string suggestedBotName, IChatBot bot)
 {
     using (Trace.Main.scope())
     {
         try
         {
             _botManager.ReassignChat(GetChat(interactionId), suggestedBotName, bot);
         }
         catch (Exception ex)
         {
             Trace.Main.exception(ex);
         }
     }
 }
Example #12
0
 public MainWindowViewModel(MenuColorViewModel menuColorViewModel,
                            StreamOverlayViewModel streamOverlayViewModel,
                            NameBiddingViewModel nameBiddingViewModel,
                            SettingsViewModel settingsViewModel,
                            IShowTwitchAuthCommand showTwitchAuthCommand,
                            IChatBot chatBot,
                            IDataStore <CharacterNameBid> dataStore)
 {
     MenuColorViewModel     = menuColorViewModel;
     NameBiddingViewModel   = nameBiddingViewModel;
     SettingsViewModel      = settingsViewModel;
     StreamOverlayViewModel = streamOverlayViewModel;
     ChatBot              = chatBot;
     ConnectBotCommand    = new SimpleCommand(x => ChatBot.Connect());
     DisconnectBotCommand = new SimpleCommand(x => ChatBot.Disconnect());
     OpenTwitchAuthWindow = showTwitchAuthCommand;
 }
Example #13
0
        private void SendChatMessage(long interactionId, IChatMessage message, IChatBot bot)
        {
            using (Trace.Main.scope())
            {
                try
                {
                    // Protect against cross-bot calls
                    _botManager.ValidateBotAssignmentAndThrow(interactionId, bot);

                    // Send message
                    SendToChat(interactionId, message);
                }
                catch (Exception ex)
                {
                    Trace.Main.exception(ex);
                }
            }
        }
Example #14
0
        internal void ReassignChat(ChatInteraction chat, string suggestedBotName, IChatBot bot)
        {
            using (Trace.BotManager.scope())
            {
                try
                {
                    if (chat == null)
                    {
                        throw new Exception("Unable to reassign null chat");
                    }

                    ValidateBotAssignmentAndThrow(chat.InteractionId.Id, bot);

                    Console.WriteLine("ReassignChat ({0})");

                    var targetBot =
                        Bots.FirstOrDefault(
                            b => b.BotName.Equals(suggestedBotName.Trim(), StringComparison.InvariantCultureIgnoreCase));
                    if (targetBot != null)
                    {
                        var attributes = chat.GetStringAttributes(targetBot.RequestedAttributes.ToArray());

                        // Found selected bot, see if it will claim the chat
                        if (targetBot.ClaimInteraction(chat.InteractionId.Id, attributes, true))
                        {
                            UnassignBot(chat.InteractionId.Id, UnassignmentReason.Reassigned);
                            DoAssignBot(chat, targetBot, attributes);
                        }
                    }

                    // Try to assign the bot if it's not assigned to the suggested bot
                    if (targetBot == null || !ValidateBotAssignment(chat.InteractionId.Id, targetBot))
                    {
                        AssignBot(chat);
                    }
                }
                catch (Exception ex)
                {
                    Trace.BotManager.exception(ex);
                }
            }
        }
Example #15
0
        internal bool ValidateBotAssignment(long interactionId, IChatBot bot)
        {
            using (Trace.Main.scope())
            {
                try
                {
                    // Bypass for internal commands; this will never be null when a bot's command gets here
                    if (bot == null)
                    {
                        return(true);
                    }

                    return(_interactionAssignments.ContainsKey(interactionId) &&
                           _interactionAssignments[interactionId].Equals(bot));
                }
                catch (Exception ex)
                {
                    Trace.Main.exception(ex);
                }
                return(false);
            }
        }
Example #16
0
 internal ChatBotServiceProvider(IChatBot bot, ServiceProviderDelegates delegates)
 {
     Bot = bot;
     _attributeService = new AttributeService
     {
         Bot = bot,
         GetAttributeMethod  = delegates.GetAttributeMethod,
         GetAttributesMethod = delegates.GetAttributesMethod,
         SetAttributeMethod  = delegates.SetAttributeMethod,
         SetAttributesMethod = delegates.SetAttributesMethod
     };
     _chatMessageService = new ChatMessageService
     {
         Bot = bot,
         SendChatMessageMethod = delegates.SendChatMessageMethod
     };
     _chatReassignmentService = new ChatReassignmentService
     {
         Bot = bot,
         ReassignChatMethod = delegates.ReassignChatMethod
     };
 }
Example #17
0
 public ChatController(IChatService _chatService, INotificationsService _notificationsService, IChatBot _chatBot)
 {
     chatService          = _chatService;
     notificationsService = _notificationsService;
     chatBot = _chatBot;
 }
Example #18
0
 private void InitializeChatBotModule()
 {
     IKernel kernel = new StandardKernel(new ChatBotModule());
     _chatBot = kernel.Get<IChatBot>();
     _chatBot.JoinChat(@"#jonar90/$nattregnet;f00327a27dd370f5");
     _timer = new Timer(2000);
     _timer.Elapsed += _timer_Elapsed;
     _timer.Start();
 }
Example #19
0
        public void send(string message, UserAccount sender)
        {
            lock (chatLog)
            {
                int row = chatLog.Rows.Add();
                chatLog.Rows[row].Cells["chatNameColumn"].Value    = sender.Username;
                chatLog.Rows[row].Cells["chatMessageColumn"].Value = message;

                //Autoscroll down
                int displayed = chatLog.DisplayedRowCount(true);
                if (chatLog.FirstDisplayedScrollingRowIndex >= 0 && displayed < chatLog.RowCount)
                {
                    chatLog.FirstDisplayedScrollingRowIndex = chatLog.RowCount - displayed;
                }
            }

            //Determine who the message should be directed to
            UserAccount target       = null;
            string      selectedName = userStatusGridView.SelectedRows[0].Cells[1].Value.ToString();

            //Split message into words
            string[] messagePieces = message.Split(new char [] { ',', ' ' });

            //Check if the message is directed to a chatbot
            foreach (UserAccount account in Accounts)
            {
                if (account.Owner != null && account.Owner is IChatBot)
                {
                    IChatBot bot = (IChatBot)account.Owner;

                    ///Check if the message contains a follower name
                    if (messagePieces.Length > 1)
                    {
                        //Check if the name is in the beginning of the message
                        if (messagePieces[0].ToUpper().IndexOf(bot.Profile.Name.ToUpper()) > -1)
                        {
                            target  = account;
                            message = message.Substring(messagePieces[0].Length);
                            break;
                        }
                        //Check if the username is in the beginning of the message
                        else if (messagePieces[0].ToUpper().IndexOf(account.Username.ToUpper()) > -1)
                        {
                            target  = account;
                            message = message.Substring(messagePieces[0].Length);
                            break;
                        }
                        //Check if the name is in the end of the message
                        else if (messagePieces[messagePieces.Length - 1].ToUpper().IndexOf(bot.Profile.Name.ToUpper()) > -1)
                        {
                            target  = account;
                            message = message.Substring(0, message.Length - messagePieces[messagePieces.Length - 1].Length);
                            break;
                        }
                        //Check if the username is in the end of the message
                        else if (messagePieces[messagePieces.Length - 1].ToUpper().IndexOf(account.Username.ToUpper()) > -1)
                        {
                            target  = account;
                            message = message.Substring(0, message.Length - messagePieces[messagePieces.Length - 1].Length);
                            break;
                        }
                    }

                    //Direct the message to follower selected given that the message might not contain a follower name
                    if (target == null && account.Username.Equals(selectedName) && account.Status == ChatStatus.Available)
                    {
                        target = account;
                    }
                }
            }

            if (target != null && target.Owner != null)
            {
                //Target must be a chat bot
                ((IChatBot)target.Owner).receive(new ChatMessage(this, sender, target, new TextMessage(message)));
            }

            UserAccount lastListener = null;

            //Direct all followers who is available to listen to the message
            foreach (UserAccount account in Accounts)
            {
                if (account.Owner != null && account.Owner is IFollower && target != account && account.Status == ChatStatus.Available)
                {
                    lastListener = account;
                    IFollower follower = (IFollower)account.Owner;
                    follower.listen(new ChatMessage(this, sender, account, new TextMessage(message)));
                }
            }

            //Select the (last) follower in current conversation
            if (target != null && target.Owner != null)
            {
                userStatusGridView.Rows[statusTableBS.Find("Name", target.Username)].Selected = true;
            }
            else if (lastListener != null)
            {
                userStatusGridView.Rows[statusTableBS.Find("Name", lastListener.Username)].Selected = true;
            }
        }
 public AuthorizationService(IChatBot chatBot, IAuthorizationManager authorizationManager)
 {
     _chatBot = chatBot;
     _authorizationManager = authorizationManager;
 }
Example #21
0
 public ChatBotLauncher(IChatBot chatBot, ITimeStampGenerator timeStampGenerator, IOptions <AppSettings> appSettings)
 {
     this.chatBot            = chatBot;
     this.timeStampGenerator = timeStampGenerator;
     this.appSettings        = appSettings;
 }
Example #22
0
        private void SetAttributes(long interactionId, IDictionary <string, string> attributes, IChatBot bot)
        {
            using (Trace.Main.scope())
            {
                try
                {
                    // Protect against cross-bot calls
                    _botManager.ValidateBotAssignmentAndThrow(interactionId, bot);

                    // Is chat?
                    var chat = GetChat(interactionId);
                    if (chat == null)
                    {
                        throw new Exception("Interaction " + interactionId + " was not able to be retrieved as a chat!");
                    }

                    // Set attributes
                    chat.SetStringAttributes(attributes);
                }
                catch (Exception ex)
                {
                    Trace.Main.exception(ex);
                }
            }
        }