/// <summary> /// Handles the events that are posted. /// </summary> /// <param name="event">The posted @event</param> /// <returns>Action results.</returns> public async Task <HttpResponseMessage> Post(ServiceHookEventBase @event) { try { if (!this.Request.Headers.Contains("subscriptionToken")) { throw new Exception(Exceptions.MissingSubscriptionToken); } if (!Guid.TryParse(this.Request.Headers.GetValues("subscriptionToken").First(), out var subscriptionId)) { throw new Exception(Exceptions.InvalidSubscriptionToken); } var querySpec = new SqlQuerySpec { QueryText = "SELECT * FROM subscriptions s WHERE s.id = @id", Parameters = new SqlParameterCollection { new SqlParameter("@id", subscriptionId), } }; var subscriptions = this.documentClient .CreateDocumentQuery <Subscription>( UriFactory.CreateDocumentCollectionUri("botdb", "subscriptioncollection"), querySpec) .ToList(); var tasks = new List <Task>(); foreach (var subscription in subscriptions) { foreach (var strategy in this.strategies.Where(s => s.ShouldHandle(@event))) { var t = strategy.Handle(@event, subscription); tasks.Add(t); } } await Task.WhenAll(tasks); return(this.Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception ex) { return(this.Request.CreateResponse(HttpStatusCode.BadRequest, ex)); } }
/// <inheritdoc /> public async Task Handle(ServiceHookEventBase serviceHookEvent, Subscription subscription) { var ev = serviceHookEvent as ServiceHookEvent <ApprovalResource>; ev.ThrowIfNull(nameof(serviceHookEvent)); subscription.ThrowIfNull(nameof(subscription)); var address = new Address(string.Empty, subscription.ChannelId, subscription.UserId, string.Empty, string.Empty); var botData = this.botDataFactory.Create(address); await botData.LoadAsync(CancellationToken.None); var data = botData.UserData.GetValue <UserData>("userData"); MicrosoftAppCredentials.TrustServiceUrl(subscription.ServiceUri.AbsoluteUri); var client = new ConnectorClient(subscription.ServiceUri, this.credentials.MicrosoftAppId, this.credentials.MicrosoftAppPassword); var conversation = subscription.ChannelId.Equals(ChannelIds.Msteams) ? client.Conversations.CreateOrGetDirectConversation( new ChannelAccount(subscription.BotId, subscription.BotName), new ChannelAccount(subscription.RecipientId, subscription.RecipientName), subscription.TenantId) : client.Conversations.CreateDirectConversation( new ChannelAccount(subscription.BotId, subscription.BotName), new ChannelAccount(subscription.RecipientId, subscription.RecipientName)); var activity = new Activity { Conversation = new ConversationAccount { Id = conversation.Id }, From = new ChannelAccount(subscription.BotId, subscription.BotName), Recipient = new ChannelAccount(subscription.RecipientId, subscription.RecipientName), Text = Labels.PendingApproval, Type = ActivityTypes.Message }; var card = new ApprovalCard(data.Account, ev.Resource.Approval, data.TeamProject); activity.Attachments.Add(card); client.Conversations.SendToConversation(activity, conversation.Id); }
/// <inheritdoc /> public bool ShouldHandle(ServiceHookEventBase serviceHookEvent) { serviceHookEvent.ThrowIfNull(nameof(serviceHookEvent)); return(serviceHookEvent.GetType() == typeof(ServiceHookEvent <ApprovalResource>)); }