Esempio n. 1
0
        public static void Main(string[] args)
        {
            Console.Title = "Hatman";
            Console.Write("Reading config...");

            if (!File.Exists("Config.txt"))
            {
                Console.WriteLine("Config.txt not found." +
                    " \nPlease ensure the file can be found within the working directory.");
                Console.Read();
                return;
            }

            var email = "";
            var pass = "";
            ReadConfig(out email, out pass);

            Console.Write("done.\nLogging into SE...");
            chatClient = new Client(email, pass);

            Console.Write("done.\nJoining room...");
            chatRoom = chatClient.JoinRoom(roomURL, true);
            Extensions.SelfID = chatRoom.Me.ID;

            ChatEventRouter router = new ChatEventRouter(chatRoom);

            Console.WriteLine("done.\n");
            chatRoom.PostMessageLight("Hiya");

            router.ShutdownMre.WaitOne();

            chatRoom.PostMessageLight("Cya");
            chatRoom.Leave();
        }
Esempio n. 2
0
        /// <summary>
        /// Joins the room with the settings passed in.
        /// </summary>
        public void JoinRoom(InstallationSettings settings)
        {
            // Copy over the settings into this class so this class can use it.
            this.settings = settings;

            // Create the ChatMessageProcessor.
            cmp = new ChatMessageProcessor(settings);
            cmp.StopBotCommandIssued += cmp_StopBotCommandIssued;

            // Logic to join the chat room.
            chatClient = new Client(settings.Email, settings.Password);
            cvChatRoom = chatClient.JoinRoom(settings.ChatRoomUrl);
            ChatBotStats.LoginDate = DateTime.Now;
            cvChatRoom.StripMention = false;

            // Say the startup message?
            if (!settings.StartUpMessage.IsNullOrWhiteSpace())
            {
                // This is the one of the few instances to not using the "OrThrow" method.
                var startMessage = cvChatRoom.PostMessage(settings.StartUpMessage);

                if (startMessage == null)
                {
                    throw new InvalidOperationException("Unable to post start up message to room.");
                }
            }

            cvChatRoom.EventManager.ConnectListener(EventType.MessagePosted, new Action<Message>(cvChatRoom_NewMessage));
            cvChatRoom.EventManager.ConnectListener(EventType.MessageEdited, new Action<Message>(cvChatRoom_NewMessage));
        }
Esempio n. 3
0
 public Chat()
 {
     chatClient = new Client(Configuration.LoginEmail, Configuration.LoginPassword, Configuration.ProxyUrl, Configuration.ProxyUsername, Configuration.ProxyPassword);
     try
     {
         chatRoom = chatClient.JoinRoom(Configuration.RoomUrl);
     }
     catch (AuthenticationException)
     {
         Console.WriteLine("Cannot Login");
         chatClient.Dispose();
     }
 }
Esempio n. 4
0
        static void Main()
        {
            Console.WriteLine("This is a ChatExchange.Net demonstration. Press the 'Q' key to exit...\n\n");

            // Create a client to authenticate the user (which will then allow us to interact with chat).
            var client = new Client("*****@*****.**", "MySuperStr0ngPa55word");

            // Join a room by specifying its URL (returns a Room object).
            var sandbox = client.JoinRoom("http://chat.stackexchange.com/rooms/1/sandbox");

            // Post a new message in the room (if successful, returns a Message object, otherwise returns null).
            // (If you have no use of the returned Message object, I'd recommend using .PostMessageFast() instead.)
            var myMessage = sandbox.PostMessage("Hello world!");

            // Listen to the InternalException event for any exceptions that may arise during execution.
            // See our wiki for examples of other events
            // (https://github.com/ArcticEcho/ChatExchange.Net/wiki/Hooking-up-chat-events).
            sandbox.EventManager.ConnectListener(EventType.InternalException, new Action<Exception>(ex => Console.WriteLine("[ERROR] " + ex)));

            // Listen to the MessagePosted event for new messages.
            sandbox.EventManager.ConnectListener(EventType.MessagePosted, new Action<Message>(message =>
            {
                // Print the new message (with the author's name).
                Console.WriteLine(message.Author.Name + ": " + message.Content);

                // If the message contains "3... 2... 1...", post "KA-BOOM!" (this is simply an [awful] example).
                if (message.Content.Contains("3... 2... 1..."))
                {
                    // Create a new MessageBuilder to format our message.
                    var msgBuilder = new MessageBuilder();

                    // Append the text "KA-BOOM!" (formatted in bold).
                    msgBuilder.AppendText("KA-BOOM!", TextFormattingOptions.Bold);

                    // Finally post the formatted message.
                    // (PostMessage() internally calls the message's ToString() method.)
                    var success = sandbox.PostMessage(msgBuilder) != null;

                    Console.WriteLine("'KA-BOOM' message successfully posted: " + success);
                }
            }));

            // Listen to the UserEntered event and post a welcome message when the event is fired.
            sandbox.EventManager.ConnectListener(EventType.UserEntered, new Action<User>(user =>
            {
                var success = sandbox.PostMessage("Hello " + user + "!") != null;

                Console.WriteLine("'Welcome' message successfully posted: " + success);
            }));

            // Wait for the user to press the "Q" key before we exit (not the best way to do this, but it'll suffice).
            while (char.ToLower(Console.ReadKey(true).KeyChar) != 'q')
            {
                Thread.Sleep(500);
            }

            // Not necessary for safe disposal, but still, it's nice to "physically"
            // leave the room rather than letting timeouts figure out that we've gone.
            sandbox.Leave();

            // Safely dispose of the client object (which'll also clean up all created room instances).
            client.Dispose();
        }