C# OmegleBot Class

Tue. June 22, 2010
Categories: C#
Tags:
// C# OmegleBot class
// by Andrew Brown
// Free to be modified, redistributed, or otherwise have your way with in any way you see fit
// http://www.drusepth.net/
 
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Collections;
using System.Windows.Forms;
 
class OmegleBot
{
    private string id;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
 
    private ListBox chatBox;
    public ListBox ChatBox
    {
        get { return chatBox; }
        set { chatBox = value; }
    }
 
    private GroupBox container;
    public GroupBox Container
    {
        get { return container; }
        set { container = value; }
    }
 
    private string lastMessage = String.Empty;
    public string LastMessage
    {
        get { return lastMessage; }
        set { lastMessage = value; }
    }
 
    private bool paired = false;
    public bool Paired
    {
        get { return paired; }
        set { paired = value; }
    }
 
    private Thread t;
    public Thread Thread
    {
        get { return t; }
        set { t = value; }
    }
 
    public Omegler(GroupBox container)
    {
        // GUI Shizzle
        Control.ControlCollection ccol = container.Controls;
        ListBox chatbox = new ListBox();
        chatbox.Dock = DockStyle.Fill;
        chatbox.HorizontalScrollbar = true;
        ccol.Add(chatbox);
        ChatBox = chatbox;
        Container = container;
 
        // Logic
        Paired = true;
        ID = Connect();
    }
 
    public string Connect()
    {
        try
        {
            ID = Request("http://www.omegle.com/start", "")
                    .Substring(1, 6);
 
            t = new Thread(EventWatcherThread);
            t.Start();
 
            return ID;
        }
        catch
        {
            MessageBox.Show("Couldn't get a new chatter ID", "Problem!");
            return String.Empty;
        }
    }
 
    public void Disconnect()
    {
        Request("http://www.omegle.com/disconnect", "id=" + ID);
 
        t.Abort();
    }
 
    public void Say(string what)
    {
        AddToChat("You: " + what);
        Request("http://www.omegle.com/send", "id=" + ID + "&msg=" + what);
    }
 
    private void EventWatcherThread()
    {
        while (true)
        {
            if (Paired)
                GetEvents();
            else
                Disconnect();
 
            Thread.Sleep(1000);
        }
    }
 
    private void GetEvents()
    {
        string event_json = Request("http://www.omegle.com/events", "id=" + ID);
 
        if (event_json != "null")
        {
            Hashtable events = ParseJSON(event_json);
            HandleEvents(events);
        }
    }
 
    delegate void SetTextCallback(string text);
    private void AddToChat(string what)
    {
        if (ChatBox.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(InvokeChat);
            ChatBox.Invoke(d, new object[] { what });
        }
        else
        {
            InvokeChat(what);
        }
    }
 
    private void InvokeChat(string text)
    {
        ChatBox.Items.Add(text);
        ChatBox.SelectedIndex = ChatBox.Items.Count - 1;
    }
 
    public delegate void EventHandlerDelegate(string e, string more);
    private void HandleEvents(Hashtable events)
    {
        IDictionaryEnumerator en = events.GetEnumerator();
        while (en.MoveNext())
        {
            switch (en.Key.ToString())
            {
                case "typing":
                    break;
 
                case "waiting":
                    break;
 
                case "connected":
                    Paired = true;
                    AddToChat("Found a new stranger!");
                    break;
 
                case "gotMessage":
                    AddToChat("Stranger: " + en.Value.ToString());
                    LastMessage = en.Value.ToString();
                    break;
 
                case "strangerDisconnected":
                    Paired = false;
                    AddToChat("The stranger has disconnected.");
                    break;
            }
 
        }
 
    }
 
    private Hashtable ParseJSON(string json)
    {
        Hashtable result = new System.Collections.Hashtable();
 
        // [["connected"], ["gotMessage", "lol"]]
        json = json.Remove(0, 1);
        json = json.Remove(json.Length - 1, 1);
        string[] json_messages = json.Split(']');
 
        foreach (string message in json_messages)
        {
            string m = message;
 
            if (m == "")
                break;
 
            if (message.Substring(0, 2) == ", ")
                m = message.Remove(0, 2);
 
            m.Remove(0, 1); // Remove ["
 
            string[] split = m.Split(',');
            string key = "", value = "";
 
            if (split.Length >= 1)
            {
                key = split[0];
 
                // Strip off " surrounding key
                key = key.Remove(0, 2);
                key = key.Remove(key.Length - 1, 1);
            }
 
            if (split.Length == 2)
            {
                value = split[1].Remove(0, 1);
 
                // Strip off " surrounding value
                value = value.Remove(0, 1);
                value = value.Remove(value.Length - 1, 1);
            }
 
            try
            {
                result.Add(key, value);
            }
            catch { }
        }
 
        return result;
    }
 
    private string Request(string url, string parameters)
    {
        try
        {
            WebRequest request;
 
            request = WebRequest.Create(url);
            request.Method = "POST";
 
            byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
 
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
 
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
 
            WebResponse response = request.GetResponse();
 
            dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
 
            reader.Close();
            dataStream.Close();
            response.Close();
 
            return responseFromServer;
        }
        catch
        {
            return String.Empty;
        }
    }
}
  • Pingback: » Hacking At Omegle: Various APIs For Conversing Drusepth Chown

  • C-Sharp user

    Hey, I’ve copied your code into VS2010 to try it out, it looks great, but there seems to be some problem. It connects and gets a session ID, but then I immediately get a ‘disconnected’ message.

    I’ve got the error output here:

    The remote server returned an error: (404) Not Found.
    at System.Net.HttpWebRequest.GetResponse()

    Could you shed any light on this, does the version work your end? I’d appreciate any help at all here.

    Many thanks!