示例#1
0
        public async Task Subscribe(WebSubHandler webSubHandler)
        {
            string externalAddress = await webAccessConfig.GetExternalWebSubAddress();

            externalURL = $"{externalAddress}/TASagentBotAPI/WebSub/Stream";
            subURL      = $"https://api.twitch.tv/helix/streams?user_id={botConfig.BroadcasterId}";

            bool success = await helixHelper.WebhookSubscribe(
                callback : externalURL,
                mode : "subscribe",
                topic : subURL,
                lease : 48 * 60 * 60,
                secret : webSubHandler.CreateSecretForRoute("/TASagentBotAPI/WebSub/Stream"));

            if (!success)
            {
                communication.SendErrorMessage("Failed to subscribe to Stream Changes. Aborting.");

                externalURL = null;
                subURL      = null;

                return;
            }

            TwitchStreams streamData = await helixHelper.GetStreams(userIDs : new List <string>()
            {
                botConfig.BroadcasterId
            });

            if (streamData.Data is null || streamData.Data.Count == 0)
            {
                currentStreamData = null;
            }
示例#2
0
        private LiveStreamData MakeStreamData(TwitchStreamData streamData, TwitchGamesResponse gamesResponse, TwitchUsersResponse usersResponse)
        {
            var gameData = gamesResponse.Data.Find(x => streamData.Game_id == x.Id);

            if (gameData == null)
            {
                _logger.LogWarning($"{nameof(gameData)} Is Null; gameId: {streamData.Game_id};UserId: {streamData.User_id}; StreamName {streamData.Title}");
            }

            if (gameData?.Name == null)
            {
                _logger.LogWarning($"Twitch Game with Id {streamData.Game_id} not found");
            }

            var gameName = gameData?.Name ?? gameData?.Id ?? "Unknown game";

            var userData = usersResponse.Data.Find(x => streamData.User_id == x.Id);

            if (userData == null)
            {
                throw new InvalidOperationException($"{nameof(userData)} Is Null; gameId: {streamData.Game_id};UserId: {streamData.User_id} StreamName {streamData.Title}");
            }

            if (userData.Login == null)
            {
                _logger.LogError($"Twitch User with Id {streamData.User_id} not found");
                return(null);
            }

            var lsData = new LiveStreamData()
            {
                StreamName = streamData.Title,
                GameName   = gameName,
                UserName   = userData.Login,
                ChannelUrl = GetChannelUrl(userData.Login),
                StartedAt  = streamData.Started_at,
                Type       = streamData.Type
            };

            return(lsData);
        }