public async Task <IActionResult> ProcessFollowerEvent(string channel) { _logger.LogFormattedMessage($"Processing Twitch webhook for channel {channel}"); using var reader = new StreamReader(Request.Body); var data = await reader.ReadToEndAsync(); var json = JObject.Parse(data); var updates = json["data"].ToObject <List <TwitchWebhookFollowersResponse> >(); try { foreach (var entity in updates.Select(update => new ChannelActivityEntity { Activity = StreamActivity.UserFollowed.ToString(), PartitionKey = update.ToName, RowKey = update.FollowedAt.ToRowKeyString(), Viewer = update.FromName })) { var result = await _storageService.AddDataToStorage(entity, _tableStorageOptions.StreamingTable); _logger.LogFormattedMessage($"Processed Twitch webhook for channel {channel}"); } } catch (Exception ex) { _logger.LogError("An exception has occurred", ex); } return(NoContent()); }
public async Task ProcessFollowersQueueMessage( [QueueTrigger("followers-data")] string message, ILogger logger) { var json = JObject.Parse(message); var updates = json["data"].ToObject <List <TwitchWebhookFollowersResponse> >(); foreach (var update in updates) { var entity = new ChannelActivityEntity { Activity = StreamActivity.UserFollowed.ToString(), PartitionKey = update.ToName, RowKey = update.FollowedAt.ToString(Constants.DATETIME_FORMAT).Replace(":", string.Empty).Replace("-", string.Empty), Viewer = update.FromName }; await _storageService.AddDataToStorage(entity); } }
public async Task <IActionResult> ProcessStreamEvent(string channel) { _logger.LogFormattedMessage($"Processing Twitch webhook for event on channel: {channel}"); using var reader = new StreamReader(Request.Body); var messageText = await reader.ReadToEndAsync(); if (string.IsNullOrEmpty(messageText)) { _logger.LogError("The message body is empty"); return(new NoContentResult()); } // Check if the json["data"] has values. If so, join the Channel. If not, leave the channel var json = JObject.Parse(messageText); ChannelActivityEntity entity; if (json["data"].HasValues) { var startedAt = json["data"][0]["started_at"].ToString(); var result = DateTime.TryParse(startedAt, out DateTime date); entity = new ChannelActivityEntity { Activity = StreamActivity.StreamStarted.ToString(), PartitionKey = channel, RowKey = date.ToRowKeyString() }; } else { entity = new ChannelActivityEntity { Activity = StreamActivity.StreamStopped.ToString(), PartitionKey = channel, RowKey = DateTime.UtcNow.ToRowKeyString() }; } await _hubContext.Clients.All.SendAsync(SIGNALR_UPDATECHANNEL, entity); await _storageService.AddDataToStorage(entity, TABLE_STREAMING); _logger.LogFormattedMessage($"Completed processing Twitch webhook for event on channel: {channel}"); return(await Task.FromResult(NoContent())); }
public async Task <IActionResult> PostFollowerUpdate([FromBody] object data, string channel) { var json = JObject.Parse(data.ToString()); var updates = json["data"].ToObject <List <TwitchWebhookFollowersResponse> >(); foreach (var update in updates) { var entity = new ChannelActivityEntity { Activity = StreamActivity.UserFollowed.ToString(), PartitionKey = channel, RowKey = update.FollowedAt.ToString("s").Replace(":", string.Empty).Replace("-", string.Empty), Viewer = update.FromName }; await _storageService.AddDataToStorage(entity); } return(NoContent()); }