public ActionResult <string> Post([FromBody] ContactMessage message)
        {
            if (message is null)
            {
                return(JsonConvert.SerializeObject(new { status = "error", message = "invalid input" }));
            }

            try
            {
                string result = _SlackClient.PostMessage(message.Message);

                if (result.Equals("ok"))
                {
                    return(JsonConvert.SerializeObject(new { status = "ok", message = "message successfully submitted" }));
                }
                else
                {
                    return(JsonConvert.SerializeObject(new { status = "error", message = "failed to send message" }));
                }
            }
            catch
            {
                return(JsonConvert.SerializeObject(new { status = "error", message = "System Error" }));
            }
        }
Example #2
0
        public async Task <string> NotifyNewPullRequest(PullRequest pr)
        {
            var profilePicture = await GetAuthorProfilePictureUrl(pr).ConfigureAwait(false);

            var payload = profilePicture != null
                ? SlackMessageFormatter.FormatNewPullRequestWithImage(pr, _configuration.Channel, profilePicture)
                : SlackMessageFormatter.FormatNewPullRequest(pr, _configuration.Channel);

            var success = await _client.PostMessage(payload);

            if (success.Ok)
            {
                return(success.Timestamp);
            }

            return(null);
        }
Example #3
0
        public async Task Process(string company, string token, IncomingMessage incomingMessage)
        {
            if (incomingMessage == null)
            {
                throw new ArgumentNullException("incomingMessage");
            }

            var values  = incomingMessage.Text.Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            var command = values[incomingMessage.Command == null ? 1 : 0].ToLower();

            incomingMessage.Text = string.Join(" ", values.Skip(incomingMessage.Command == null ? 2 : 1));

            var messages = new List <Message>();

            try
            {
                using (var scope = _rootScope.BeginLifetimeScope())
                {
                    var handlers = scope.Resolve <IEnumerable <IHandleMessage> >();

                    foreach (var handler in handlers.Where(handler => handler.CanHandle(command)))
                    {
                        messages.Add(await handler.Handle(incomingMessage));
                    }

                    if (messages.Any() == false)
                    {
                        messages.Add(new Message
                        {
                            Text    = string.Format("@{0} Umm, what do you mean by \"{1} {2}\"", incomingMessage.UserName, command, incomingMessage.Text),
                            Channel = string.Format("#{0}", incomingMessage.ChannelName)
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                messages.Add(new Message
                {
                    Text    = string.Format("@{0} Umm, something went wrong  \"{1} {2}\" {3}", incomingMessage.UserName, command, incomingMessage.Text, ex.Message),
                    Channel = string.Format("#{0}", incomingMessage.ChannelName)
                });
            }

            if (incomingMessage.IsSlashCommand() && command != "echo")
            {
                foreach (var message in messages)
                {
                    message.Channel = string.Format("@{0}", incomingMessage.UserName);
                }
            }

            foreach (var message in messages)
            {
                await _client.PostMessage(company, token, message);
            }
        }
 public void SendNotification(string message)
 {
     Task.Run(() =>
     {
         slackClient.PostMessage(new Payload()
         {
             Text     = message,
             Channel  = "datatec",
             Username = "******"
         });
     });
 }
        public async Task ReportToSlack([ActivityTrigger] SlackReportingContext slackReportingContext)
        {
            if (slackReportingContext is null)
            {
                throw new ArgumentNullException(nameof(slackReportingContext));
            }

            var context = new MessageUtil.MessageContext
            {
                DeletedResourceGroups = slackReportingContext.DeletedResourceGroups,
                WasSimulated          = _cleanupConfiguration.Simulate
            };
            var message = MessageUtil.CreateDeleteInformationMessage(_cleanupConfiguration.SlackChannel, context);
            await _slackClient.PostMessage(message).ConfigureAwait(false);
        }
Example #6
0
        public Task <SwaggerResponse> SendSlackAsync(MailDto slackMessage)
        {
            var headers = new Dictionary <string, IEnumerable <string> >();

            try
            {
                var userId  = _httpContextAccessor.HttpContext.User.FindFirst(cl => cl.Type.Equals("id")).Value;
                var profile = _unitOfWork.AppProfiles.FindById(userId);
                _slackClient.SetConfig(_slackConfigOptions);
                _slackClient.PostMessage($"From {profile.Identity.Email}: {slackMessage.Subject} {slackMessage.Body}");
                return(Task.Run(() => new SwaggerResponse(StatusCodes.Status200OK, headers)));
            }
            catch (Exception exception)
            {
                return(HandleException(exception, headers));
            }
        }
Example #7
0
        public async Task <string> PostToSlack(string message)
        {
            var response = _slackClient.PostMessage(message);

            return(response);
        }
Example #8
0
        private void Run()
        {
            logger.Log("Starting SlackBot message loop");
            try
            {
                running = true;

                ISlackClient client = slackClientFactory.CreateSlackClient(slackToken);
                client.OnMessageReceived += (message) =>
                {
                    try
                    {
                        if (message.channel == null)
                        {
                            return;
                        }

                        if (IsMessageFromBot(client, message))
                        {
                            return;
                        }

                        if (client.IsDirectMessageChannel(message.channel))
                        {
                            logger.Log("DM from " + client.GetUsername(message.user) + " at " + message.ts.ToShortTimeString() + ": " + message.text);
                            string response = responseHandler.GenerateResponse(message.text, true);
                            if (response != null)
                            {
                                client.PostMessage(message.channel, response, () =>
                                {
                                    logger.Log("Sent DM back to " + client.GetUsername(message.user) + ": " + response);
                                });
                            }
                        }
                        else
                        {
                            logger.Log("Message in channel " + client.GetChannelName(message.channel) + " at " + message.ts.ToShortTimeString() + " from " + client.GetUsername(message.user) + ": " + message.text);
                            string response = responseHandler.GenerateResponse(message.text, false);
                            if (response != null)
                            {
                                client.PostMessage(message.channel, response, () =>
                                {
                                    logger.Log("Sent message to channel " + client.GetChannelName(message.channel) + ": " + response);
                                });
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        logger.Log("Got exception when processing message:" + e);
                    }
                };

                client.Connect(() => { logger.Log("successfully connected"); });
                client.RefreshUserList(() => { logger.Log("refreshed users"); });
                client.RefreshChannelList(() => { logger.Log("refreshed channels"); });

                while (running)
                {
                    Thread.Sleep(1000);
                }
            }
            catch (Exception e)
            {
                logger.Log("Got exception when running SlackBot thread: " + e);
                running = false;
            }
            logger.Log("Finished SlackBot message loop");
        }