Skip to main content

Simple Omegle Bot Using Selenium With Python



Omegle is an online text-based and video-based chatting platform, which allows users from around the world to talk to complete strangers anonymously, for free! The text-based Omegle has a simple concept: Complete a Captcha verification, connect automatically to a stranger, and after chatting get on to the next stranger.

Apart from having a chit-chat with a stranger from the far side of the globe, Omegle poses as the perfect platform for other uses as well. At Omegle, you  can advertise your content, website, products and more for free. With access to about 40,000 strangers using Omegle at any given time, you can benefit if your ideas/advertisement is seen by potential customers.



But of course, we can't advertise to each guy we meet again and again. I mean, come on, that's a lot of hard work, even if your message consists of a few words. But worry not, since that's where our Omegle Bot comes in play. This bot works on a pre-determined set of messages that are to be conveyed to each stranger on the platform. The bot then moves on to the next stranger. As a result, this program saves money and is efficient.

Unfortunately, advertising in Omegle is not new. If you go to Omegle website now, you will find lots of bots (how to know which one is a bot? Believe me, you'll know). But that doesn't mean there are no humans online. And after all, we are creating a bot, which, if it needed be, can complete an entire conversation in a fraction of a second. Though, it is best to avoid that, so as to give a good effect to the stranger as well. So, Are you ready? Lets make a Bot!

Required Modules, Libraries and Software

First of all, you need a driver. For Selenium (the module to operate a website) to access a website, you need a driver of your preferred browser. I am using Chromedriver, since I am working with Chrome browser, which you can download from here. You also have to install the python libraries Selenium for aforementioned purpose and webdriver for making sure your Chromedriver is the latest version, both of which you can download using pip. You also need time library, so that your bot waits for brief moments, and random library, if you want the bot to choose a random message from a choice of different messages.

The Website Part

Selenium is very helpful and easy to use on websites. Suppose you are on a website and you want to click a button. Selenium cannot see the button as you can, but it can see the code underlying the web page. Selenium depends on you to pinpoint the code corresponding to the element (button, textbox, image etc.) .  For doing that, get to any website, say stay on this website. 

Press Ctrl + Shift + i to open inspect element tool, the window that pops on right side of the browser. 




Click on the mouse button:




Click on the element you want:




The code underlying that element will be highlighted in the inspection tools window. 




Now, there are many methods you can use to pinpoint an element for Selenium. Sometimes, you can see an id or a name tag in the highlighted code. You can note these down for the code. The way I am going to do, is something called XPath. You can copy it by right-clicking the highlighted code and then doing this:




You have to follow similar procedure for the Omegle website. You have to get the identifiers for the text area (where you input the message), the send button (which you click to send the message) and the new button (which you click to speak to a new stranger).

The Code

Firstly, importing all of our libraries:

To get the browser and get to Omegle website:

As you can see, we have initiated a variable driver, which first uses the webdriver library to install the newest version of Chromedriver (process is skipped if newest version is already installed). The command: driver.get(" ") accesses the website, whose URL is provided under the quotes.

Also you can see that there is a time.sleep(8), that is, the program waits for 8 seconds after accessing the website. This is because, as I myself have found, a proper XPath or any other identifier is not available for the button Text that allows you to chat with others by text. As a result, you will have to click it within the 8 seconds (you can increase the time, but remember that the bot will keep waiting even after the chat starts, if you give a large waiting time). Moving onto the message sending part:

For the sake of simplicity, I have taken a single message. We have the entire messaging function inside a while true loop, so that the bot runs infinitely, sending message and then going to the next stranger. Actually, it is under the try statement, which performs everything under it, but exits when there is an error. This error occurs when some stranger quits the chat before the bot does. In that case, we have the except statement, making the bot press the New button thrice to start a new chat.

 We have the variables text_area, send_btn, and new_btn for performing the driver function. The driver.find_element_by_xpath finds the required button or any other interactive item on the website using its XPath, which I have mentioned how to get in the previous section. There are other ways with which Selenium can find elements on the web page, like using id, name, class etc, more of which you can read here.

To send the message, we use the appropriate variable where we have stored the driver function to find that element as: <variable>.send_keys(" ... ")
In this case, our variable is text_area, and the message is inputted inside the parenthesis. If your message is stored in a variable, remove the quotes and write the variable inside the brackets.

Similarly for clicking the buttons, we perform: <variable>.click()
In this case, send_btn will be used after every <variable>.send_keys() command to send the message. The new_btn will be used to click on the exit button.

There are many such interesting Selenium function, which you can see here.

Lastly, I recommend adding the time.sleep(3) command at the end of a conversation, at the last of both the try and except statements. This is because, although Omegle is fast enough to connect you to the next stranger, but in case if there is a delay, the program might end up clicking the new button. Giving a brief waiting time of 2-3 seconds will prevent it.

And this is how you create a simple Omegle bot using Selenium. The best part? Selenium does not perform internally. As a result, you will be able to see it opening the webpage, typing the message and clicking the buttons!

Be sure to read the following section to make this bot a bit more interesting. Also make sure to download the program, from the link given at the end and also don't forget to read the Important Points section. Enjoy!

Tweaks

1] Giving a choice of different messages for one message can be an healthy practice. Store different messages inside different variables, and create a list from these. For sending the message, let the bot pick one message from this list, by picking out a random number between 0 and the length of the list. The number the bot gets will be the index position of the chosen message, like so:

2] Making the bot wait for some time between and after messages is important. You may observe how users chat, and add appropriate delays wherever necessary to give a human touch.

3] If you have used Omegle before you will know that when the stranger is typing something, the chatting area shows the message "Stranger is typing...". But since we are using a bot , the program instantly writes the entire message, which will obviously prevent Omegle from showing "Stranger is typing..." to the guy you are talking to. Instead you can introduce a for loop, to write the message letter-by-letter, with a very small delay of 0.1 seconds between letters, like so:

Important Points

Here's the catch: After you click on Text on the main page of Omegle website, you will be prompted to fill the Captcha Verification. And often this verification involves finding correct images as mentioned. But alas! you only have 8 seconds, before the bot tries to find the text area to send the message, fails to do so, and promptly stops the execution after giving a long list of Errors.

Therefore, this is what you are going to do: Dont worry about the 8-second countdown. Fill the Captcha patiently, and complete it. You will find that the program has already terminated. Therefore, close the browser and restart your program. Click on Text again, and you will be surprised to know that this time the Captcha does not even appear. Do this within the 8-seconds and your bot is set.

If the above trick doesn't happen, replace the 8-second countdown with something like a 2-3 minute delay. You will only miss a few starting conversations, but it ensures you have sufficient time for filling up Captcha.

And I hate to break this to you, but here it is: Omegle usually knows you are a bot. In fact, you can see it on the browser itself, where it says "Chrome is being controlled by an automated software". You may expect the bot to be able to chat with 200 or so people before Omegle bans you. Don't worry, this ban is not permanent, and you will be able to restart your bot again after the ban ends, which is generally a few hours at max.

This bot is just a tiny part of the marvel of Selenium. These processes can be used for any other website, for any other purpose. Essentially, you can automate many of your online activities using Selenium. I hope you enjoyed this small project!

Here's the promised Python program file: Download Omegle Bot Python File

Peace out.





Comments

  1. Ways How To Get Rid of omegle bot Captcha Every Time

    ReplyDelete
    Replies
    1. Omegle has image captcha, and there's plenty of programs available online, that "claim" to handle the Captcha, usually using Machine learning, so maybe you can try integrating that.

      Delete

Post a Comment

Popular posts from this blog

English Dictionary With Python And Tkinter!

English Dictionary on Python Have you ever tried to read from the small Oxford Dictionaries? Those small yet bulky books have thousands of words cramped on a page the length of your finger! And on top of that, not to forget the hassle of flipping across the pages, searching for your word, God knows where is hiding on which page! Nowadays though, hardly does anyone ever use the classic dictionaries. With the access of technology on our fingertips, one tap on Google, and you would be on with your way.  But how about making a program of your own which can do the same for you? Sounds interesting? Such a program can enable you to run it and keep it opened, while you are reading a book, so that you can search the definition of a new word you encountered. What if you are writing a book, perhaps a report? You know what you are supposed to write, but you choose to get a word for it, so that you sound professional. So you just search for a short definition and the program find the word with matc

Predicting Stock Prices using Machine Learning (XGBoost)

  Today, I'll show you how to create a machine learning program to predict stock prices. Machine learning is used in a variety of fields, and we can utilize its ability to learn and predict from data to predict useful variables, with minimal error. Therefore in theory, we can apply the same on stocks to predict the next closing price, so that we can make a killing gain. However, stock markets are highly unstable. Their price movement often depends on decisions taken by the company, favor and reaction of investors, social impact, human emotions, and price movements of some other related stocks. These types of data cannot be made available to a program. The prediction can be close only if the market remains relatively stable. You don't actually need to learn machine learning using python to understand how this program works, if not minimal. I'll describe each machine learning process. And don't forget to download the source code of this program, link provided at the end.