private Task MessageReceived(SlackMessage message)
 {
   var slackBotMessageContext = new SlackBotMessageContext(this, message) { Text = message.Text };
   _log.Info(string.Format("Chathub {0}-{2} Text:{1}", message.ChatHub.Name, message.Text , message.User.Name));
   _chatBot.Process(slackBotMessageContext).FireAndForgetWithLogging();
   return Task.Delay(1);
 }
Exemple #2
0
        private async Task MessageReceived(SlackMessage message)
        {
            Console.WriteLine("[[[Message started]]]");

            IMiddleware pipeline = _pipelineFactory.GetPipeline();
            var incomingMessage = new IncomingMessage
            {
                RawText = message.Text,
                FullText = message.Text,
                UserId = message.User.Id,
                Username = GetUsername(message),
                Channel = message.ChatHub.Id,
                UserChannel = await GetUserChannel(message),
                BotName = _client.UserName,
                BotId = _client.UserId,
                BotIsMentioned = message.MentionsBot
            };

            incomingMessage.TargetedText = incomingMessage.GetTargetedText();

            try
            {
                foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage))
                {
                    await SendMessage(responseMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: {0}", ex);
            }

            Console.WriteLine("[[[Message ended]]]");
        }
        public void Execute(IJobExecutionContext context)
        {
            Guid guid = Guid.Parse(context.JobDetail.JobDataMap["guid"].ToString());
            var schedule = _schedulePlugin.ListAllSchedules().First(x => x.Guid == guid);
            
            _statsPlugin.IncrementState("schedules:run");
            _log.Info($"Running schedule: {schedule}");

            SlackChatHubType channelType = schedule.ChannelType == ResponseType.Channel
                ? SlackChatHubType.Channel
                : SlackChatHubType.DM;

            var slackMessage = new SlackMessage
            {
                Text = schedule.Command,
                User = new SlackUser { Id = schedule.UserId, Name = schedule.UserName },
                ChatHub = new SlackChatHub { Id = schedule.Channel, Type = channelType },
            };

            _noobotCore.MessageReceived(slackMessage);
            schedule.LastRun = DateTime.Now;
        }
 private Task SlackConnectorOnOnMessageReceived(SlackMessage message)
 {
     return new Task(() => { });
 }
Exemple #5
0
 private async Task<string> GetUserChannel(SlackMessage message)
 {
     return (await GetUserChatHub(message.User.Id, joinChannel: false) ?? new SlackChatHub()).Id;
 }
Exemple #6
0
 private string GetUsername(SlackMessage message)
 {
     return _connection.UserCache.ContainsKey(message.User.Id) ? _connection.UserCache[message.User.Id].Name : string.Empty;
 }
Exemple #7
0
        public async Task MessageReceived(SlackMessage message)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();
            _log.Info($"[Message found from '{message.User.Name}']");

            IMiddleware pipeline = _container.GetMiddlewarePipeline();
            var incomingMessage = new IncomingMessage
            {
                RawText = message.Text,
                FullText = message.Text,
                UserId = message.User.Id,
                Username = GetUsername(message),
                Channel = message.ChatHub.Id,
                ChannelType = message.ChatHub.Type == SlackChatHubType.DM ? ResponseType.DirectMessage : ResponseType.Channel,
                UserChannel = await GetUserChannel(message),
                BotName = _connection.Self.Name,
                BotId = _connection.Self.Id,
                BotIsMentioned = message.MentionsBot
            };

            incomingMessage.TargetedText = incomingMessage.GetTargetedText();

            try
            {
                foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage))
                {
                    await SendMessage(responseMessage);
                }
            }
            catch (Exception ex)
            {
                _log.Error($"ERROR WHILE PROCESSING MESSAGE: {ex}");
            }

            stopwatch.Stop();

            _log.Info($"[Message ended - Took {stopwatch.ElapsedMilliseconds} milliseconds]");
            _averageResponse.Log(stopwatch.ElapsedMilliseconds);
        }
 public SlackBotMessageContext(SlackBotServer slackBotServer, SlackMessage message)
 {
     _slackBotServer = slackBotServer;
     _message = message;
 }
Exemple #9
0
 private string GetUsername(SlackMessage message)
 {
     return _client.UserNameCache.ContainsKey(message.User.Id) ? _client.UserNameCache[message.User.Id] : string.Empty;
 }
        private async Task RaiseMessageReceived(SlackMessage message)
        {
            var e = OnMessageReceived;
            if (e != null)
            {
                try
                {
                    await e(message);
                }
                catch (Exception)
                {

                }
            }
        }
        private Task HandleMessage(ChatMessage inboundMessage)
        {
            if (string.IsNullOrEmpty(inboundMessage.User))
                return Task.FromResult(false);

            if(!string.IsNullOrEmpty(Self.Id) && inboundMessage.User == Self.Id)
                return Task.FromResult(false);
            
            var message = new SlackMessage
            {
                User = GetMessageUser(inboundMessage.User),
                TimeStamp = inboundMessage.TimeStamp,
                Text = inboundMessage.Text,
                ChatHub = inboundMessage.Channel == null ? null : _connectedHubs[inboundMessage.Channel],
                RawData = inboundMessage.RawData,
                MentionsBot = _mentionDetector.WasBotMentioned(Self.Name, Self.Id, inboundMessage.Text)
            };

            return RaiseMessageReceived(message);
        }
Exemple #12
0
        private void ExecuteSchedule(ScheduleEntry schedule)
        {
            if (ShouldRunSchedule(schedule))
            {
                _log.Log($"Running schedule: {schedule}");

                SlackChatHubType channelType = schedule.ChannelType == ResponseType.Channel
                    ? SlackChatHubType.Channel
                    : SlackChatHubType.DM;

                var slackMessage = new SlackMessage
                {
                    Text = schedule.Command,
                    User = new SlackUser { Id = schedule.UserId, Name = schedule.UserName },
                    ChatHub = new SlackChatHub { Id = schedule.Channel, Type = channelType },
                };

                _noobotCore.MessageReceived(slackMessage);
                schedule.LastRun = DateTime.Now;
            }
        }
Exemple #13
0
 private async Task Connection_OnMessageReceived(SlackMessage message)
 {
    ProcessMessage(message);
 }
Exemple #14
0
      private void ProcessMessage(SlackMessage message)
      {
         // we should never respond to messages from the bot itself
         if (String.Equals(message.User.Name, "ConsensusBot", StringComparison.InvariantCultureIgnoreCase) &&
            !String.Equals(message.ChatHub.Name,"general",StringComparison.InvariantCulture))
         {
            return;
         }

         var mod = Moderator.GetModerator(message.User.Name);
         if (message.Text.StartsWith("$vote", StringComparison.InvariantCultureIgnoreCase))
         {
            ChatCommand cmd = null;
            if (ChatCommand.TryParse(ChatProviderType.Slack,message.Text, out cmd))
            {
               OnCommandReceived(new CommandReceivedEventArgs { Command = cmd, Message = message.Text, Moderator = mod });
            }
         }
      }
        private async Task ListenTo(InboundMessage inboundMessage)
        {
            if (inboundMessage?.MessageType != MessageType.Message)
                return;
            if (string.IsNullOrEmpty(inboundMessage.User))
                return;
            if (!string.IsNullOrEmpty(Self.Id) && inboundMessage.User == Self.Id)
                return;

            if (inboundMessage.Channel != null && !_connectedHubs.ContainsKey(inboundMessage.Channel))
            {
                _connectedHubs[inboundMessage.Channel] = _chatHubInterpreter.FromId(inboundMessage.Channel);
            }

            var message = new SlackMessage
            {
                User = new SlackUser
                {
                    Id = inboundMessage.User,
                    Name = GetMessageUsername(inboundMessage),
                },
                Text = inboundMessage.Text,
                ChatHub = inboundMessage.Channel == null ? null : _connectedHubs[inboundMessage.Channel],
                RawData = inboundMessage.RawData,
                MentionsBot = _mentionDetector.WasBotMentioned(Self.Name, Self.Id, inboundMessage.Text)
            };

            await RaiseMessageReceived(message);
        }
        private async Task RaiseMessageReceived(SlackMessage message)
        {
            if (OnMessageReceived != null)
            {
                try
                {
                    await OnMessageReceived(message);
                }
                catch (Exception)
                {

                }
            }
        }