public async Task ProcessRequest(EventWrapperRequest request) { try { await FindSimilar(request); } catch (Exception e) { if (request?.Event?.Channel != null) { await _slackClient.SendMessageAsync(request.Event.Channel, Phrases.SlackError); } _logger.LogError(e, e.Message); } }
public async Task Handle(AskExpertsSlackActionParams actionParams) { if (actionParams == null) { throw new ArgumentNullException(nameof(actionParams)); } var askedUserId = actionParams.User.Id; var question = new Question { Text = actionParams.ButtonParams.QuestionText.Trim('\n', ' '), AskedUsersIds = new List <string> { askedUserId } }; _logger.LogInformation("User {User} with id {UserId} asked the experts. Question: {Question}", actionParams.User.Name, askedUserId, question.Id); await UpdateMessage(actionParams); question = await _questionService.UpsertAsync(question); var messageText = $"*<@{askedUserId}> asked the following question:*\n" + $"_{question.Text}_"; var attachments = CreateAttachments(question); await _slackClient.SendMessageAsync(_expertsChannelId, messageText, attachments); }
public async void SendMessageAsync_CorrectParametres_ShouldCallPostAsync() { //arange _httpClientMock .Setup(m => m.PostAsync(It.IsAny <string>(), It.IsAny <StringContent>())) .ReturnsAsync(new HttpResponseMessage() { Content = new StringContent("{\"ok\":\"true\"}") }); //act await _slackHttpClient.SendMessageAsync(ChannelId, Message); //assert _httpClientMock.Verify( m => m.PostAsync(It.Is <string>(x => x == "chat.postMessage"), It.IsAny <StringContent>()), Times.Once); }
public async Task Route(string payload) { if (string.IsNullOrEmpty(payload)) { throw new ArgumentException(); } var invocationPayload = JsonConvert.DeserializeObject <InvocationPayloadRequest>(payload, _defaultSlackSerializerSettings); try { await DefinePayloadTypeAndInvokeMethod(invocationPayload, payload); } catch (Exception e) { if (invocationPayload?.Channel?.Id != null) { await _slackHttpClient.SendMessageAsync(invocationPayload.Channel.Id, Phrases.SlackError); } _logger.LogError(e, e.Message); } }
public async Task Handle(ShowMoreAnswersSlackActionParams actionParams) { if (actionParams == null) { throw new ArgumentNullException(nameof(actionParams)); } if (actionParams.ButtonParams == null) { throw new ArgumentNullException(nameof(actionParams.ButtonParams)); } _logger.LogInformation( "User {User} with id {UserId} asked for more answers to the question {QuestionId}", actionParams.User.Name, actionParams.User.Id, actionParams.ButtonParams.QuestionId); await UpdateMessage(actionParams); var answers = await _questionService.GetAnswersOnQuestionExceptAsync( actionParams.ButtonParams.QuestionId, actionParams.ButtonParams.AnswerId); var attachmentTitle = actionParams.OriginalMessage.Attachments[actionParams.AttachmentId].Title; var answerText = answers.Count != 0 ? $"{attachmentTitle}\n{Phrases.FoundNumberOfAnswers}{answers.Count}" : $"{attachmentTitle}{Phrases.NoMoreAnswers}"; var attachments = new List <AttachmentDto>(); attachments.AddRange( answers.Select(answer => CreateAttachment(actionParams.ButtonParams.QuestionId, answer))); await _slackClient.SendMessageAsync( actionParams.Channel.Id, answerText, attachments); }
private async Task SendNotificationForUser(string userId, string message, IList <AttachmentDto> attachments) { var channel = await _slackClient.OpenDirectMessageChannelAsync(userId); await _slackClient.SendMessageAsync(channel.Id, message, attachments); }
public async Task Handle(AnswerSlackActionParams actionParams) { if (actionParams == null) { throw new ArgumentNullException(nameof(actionParams)); } if (actionParams.ButtonParams == null) { throw new ArgumentNullException(nameof(actionParams.ButtonParams)); } if (actionParams.User == null) { throw new ArgumentNullException(nameof(actionParams.User)); } var questionId = actionParams.ButtonParams.QuestionId; if (string.IsNullOrEmpty(questionId)) { throw new ArgumentException(nameof(questionId)); } var userId = actionParams.User.Id; var userName = actionParams.User.Name; _logger.LogInformation("User {User} with id {UserId} is going to add answer to the question {QuestionId}.", userName, userId, questionId); var question = await _questionService.GetQuestionAsync(questionId); if (question == null) { throw new ArgumentNullException(nameof(question)); } var attachments = new List <AttachmentDto>(); string messageText; if (question.Answers != null) { messageText = $"To the question:\n{question.Text}\n\nthe following answers were found:"; attachments.AddRange( question.Answers.OrderByDescending(a => a.Rank) .Select(answer => new AttachmentDto { Text = $"{answer.Text}\n{Phrases.RatingOfAnswer}{answer.Rank}", Color = Color.PictonBlue })); } else { messageText = $"To the question:\n{question.Text}\n\nNo answer found. You can be the first"; } var addAnswerParams = JsonConvert.SerializeObject(new AddAnswerActionButtonParams { QuestionId = questionId }); attachments.Add(new AttachmentDto { CallbackId = CallbackId.AddAnswerButtonId, Color = Color.Sand, Actions = new List <AttachmentActionDto> { new AddAnswerButtonAttachmentAction(addAnswerParams) } }); var channel = await _slackClient.OpenDirectMessageChannelAsync(userId); await _slackClient.SendMessageAsync(channel.Id, messageText, attachments); }