public static TwitchConnection GetTwitchClient()
        {
            if (UnitTestBase.connection == null)
            {
                UnitTestBase.connection = TwitchConnection.ConnectViaLocalhostOAuthBrowser(clientID, clientSecret, scopes).Result;
            }

            Assert.IsNotNull(UnitTestBase.connection);
            return(UnitTestBase.connection);
        }
        public static async Task <Result <TwitchPlatformService> > Connect(IEnumerable <OAuthClientScopeEnum> scopes)
        {
            try
            {
                TwitchConnection connection = await TwitchConnection.ConnectViaLocalhostOAuthBrowser(TwitchPlatformService.ClientID, ChannelSession.Services.Secrets.GetSecret("TwitchSecret"),
                                                                                                     scopes, forceApprovalPrompt : true, successResponse : OAuthExternalServiceBase.LoginRedirectPageHTML);

                if (connection != null)
                {
                    return(new Result <TwitchPlatformService>(new TwitchPlatformService(connection)));
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                return(new Result <TwitchPlatformService>(ex));
            }
            return(new Result <TwitchPlatformService>("Failed to connect to establish connection to Twitch"));
        }
Exemple #3
0
        public static void Main(string[] args)
        {
            Task.Run(async() =>
            {
                try
                {
                    Logger.SetLogLevel(LogLevel.Debug);
                    Logger.LogOccurred += Logger_LogOccurred;

                    using (StreamWriter writer = new StreamWriter(File.Open("Packets.txt", FileMode.Create)))
                    {
                        await writer.FlushAsync();
                    }

                    System.Console.WriteLine("Connecting to Twitch...");

                    connection = await TwitchConnection.ConnectViaLocalhostOAuthBrowser(clientID, clientSecret, scopes);
                    if (connection != null)
                    {
                        System.Console.WriteLine("Twitch connection successful!");

                        user = await connection.NewAPI.Users.GetCurrentUser();
                        if (user != null)
                        {
                            System.Console.WriteLine("Logged in as: " + user.display_name);

                            System.Console.WriteLine("Connecting to Chat...");

                            chat = new ChatClient(connection);

                            chat.OnDisconnectOccurred += Chat_OnDisconnectOccurred;
                            chat.OnSentOccurred       += Chat_OnSentOccurred;
                            chat.OnPacketReceived     += Chat_OnPacketReceived;

                            chat.OnPingReceived            += Chat_OnPingReceived;
                            chat.OnGlobalUserStateReceived += Chat_OnGlobalUserStateReceived;
                            chat.OnUserListReceived        += Chat_OnUserListReceived;
                            chat.OnUserJoinReceived        += Chat_OnUserJoinReceived;
                            chat.OnUserLeaveReceived       += Chat_OnUserLeaveReceived;
                            chat.OnMessageReceived         += Chat_OnMessageReceived;
                            chat.OnUserStateReceived       += Chat_OnUserStateReceived;
                            chat.OnUserNoticeReceived      += Chat_OnUserNoticeReceived;

                            await chat.Connect();

                            await Task.Delay(1000);

                            await chat.AddCommandsCapability();
                            await chat.AddTagsCapability();
                            await chat.AddMembershipCapability();

                            await Task.Delay(1000);

                            await chat.Join(user);

                            await Task.Delay(2000);

                            System.Console.WriteLine(string.Format("There are {0} users currently in chat", initialUserList.Count()));

                            await chat.SendMessage(user, "Hello World!");

                            while (true)
                            {
                                string line = System.Console.ReadLine();
                                await chat.SendMessage(user, line);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.ToString());
                }
            }).Wait();

            System.Console.ReadLine();
        }
Exemple #4
0
        public static void Main(string[] args)
        {
            Task.Run(async() =>
            {
                try
                {
                    Logger.SetLogLevel(LogLevel.Debug);
                    Logger.LogOccurred += Logger_LogOccurred;

                    System.Console.WriteLine("Connecting to Twitch...");

                    connection = TwitchConnection.ConnectViaLocalhostOAuthBrowser(clientID, clientSecret, scopes).Result;
                    if (connection != null)
                    {
                        System.Console.WriteLine("Twitch connection successful!");

                        user = await connection.NewAPI.Users.GetCurrentUser();
                        if (user != null)
                        {
                            System.Console.WriteLine("Logged in as: " + user.display_name);

                            System.Console.WriteLine("Connecting to PubSub...");

                            pubSub = new PubSubClient(connection);

                            pubSub.OnDisconnectOccurred += PubSub_OnDisconnectOccurred;
                            pubSub.OnSentOccurred       += PubSub_OnSentOccurred;
                            pubSub.OnReconnectReceived  += PubSub_OnReconnectReceived;
                            pubSub.OnResponseReceived   += PubSub_OnResponseReceived;
                            pubSub.OnMessageReceived    += PubSub_OnMessageReceived;
                            pubSub.OnWhisperReceived    += PubSub_OnWhisperReceived;
                            pubSub.OnPongReceived       += PubSub_OnPongReceived;

                            await pubSub.Connect();

                            await Task.Delay(1000);

                            List <PubSubListenTopicModel> topics = new List <PubSubListenTopicModel>();
                            foreach (PubSubTopicsEnum topic in EnumHelper.GetEnumList <PubSubTopicsEnum>())
                            {
                                topics.Add(new PubSubListenTopicModel(topic, user.id));
                            }

                            await pubSub.Listen(topics);

                            await Task.Delay(1000);

                            await pubSub.Ping();

                            while (true)
                            {
                                System.Console.ReadLine();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.ToString());
                }
            }).Wait();

            System.Console.ReadLine();
        }