public async Task <bool> SubscribeToTwitchStreamWebHook(TwitchSubscriptionData subscription) { var token = await GetAppToken(); using (var client = _clientFactory.CreateClient("twitch")) { var callback = $"{_baseCallbackUrl}/api/twitch/streamupdate/{subscription.UserId}/{subscription.TwitchAccountId}"; var mode = $"subscribe"; var topic = $"{_twitchWebhookUrl}/helix/streams?user_id={subscription.TwitchAccountId}"; var leaseSeconds = 864000; var parameters = new TwitchWebHookParameters() { Callback = callback, Mode = mode, Topic = topic, Lease = leaseSeconds }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var contentString = JsonConvert.SerializeObject(parameters); var content = new StringContent(contentString, Encoding.UTF8, "application/json"); var url = "helix/webhooks/hub"; var response = await client.PostAsync(url, content); return(response.IsSuccessStatusCode); } }
public async Task ProcessAsync(IMessage message) { if (message is TwitchWebhookSubscriptionMessage twitchWebhookSubscriptionMessage) { var twitchAccounts = await _repository.GetIncluding( x => x.UserId.Equals(twitchWebhookSubscriptionMessage.UserId) && x.Platform == Site.Domain.Enums.Platform.Twitch); foreach (var twitchAccount in twitchAccounts) { var parameter = new TwitchSubscriptionData() { UserId = twitchAccount.UserId, TwitchAccountId = twitchAccount.PlatformId }; await _twitchService.SubscribeToTwitchStreamWebHook(parameter); } } }