<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Python on GeekyHub</title>
    <link>https://www.geekyhub.in/tags/python/</link>
    <description>Recent content in Python on GeekyHub</description>
    <generator>Hugo -- 0.148.1</generator>
    <language>en</language>
    <lastBuildDate>Sat, 23 Jan 2021 22:31:54 +0530</lastBuildDate>
    <atom:link href="https://www.geekyhub.in/tags/python/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Build a Simple WhatsApp bot in Python using Selenium</title>
      <link>https://www.geekyhub.in/post/making-a-whatsapp-bot-using-python-selenium/</link>
      <pubDate>Fri, 20 Oct 2017 17:51:16 +0530</pubDate>
      <guid>https://www.geekyhub.in/post/making-a-whatsapp-bot-using-python-selenium/</guid>
      <description>DIY Article for building a simple WhatsApp bot using Python and Selenium</description>
      <content:encoded><![CDATA[<figure>
    <img loading="lazy" src="/images/robots-764951_640.png"/> 
</figure>

<p>Selenium is a web automation package available for all popular languages. To know more about selenium you can refer to <a href="http://docs.seleniumhq.org/">official Selenium docs</a>.
Here, we will be making a simple WhatsApp bot using Python and Selenium which will reply the current time for every message.</p>
<h2 id="installing-selenium">Installing Selenium</h2>
<p>So, first of all, we need to install Selenium for Python by running following command in terminal.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>pip install selenium 
</span></span></code></pre></div><p>Selenium also requires a driver to interface with the chosen browser. For Firefox, we need to install geckodriver. Without proper driver, you will get <em>WebDriverException</em>.</p>
<p>Download latest geckodriver from <a href="https://github.com/mozilla/geckodriver/releases">Mozilla&rsquo;s GitHub Repo</a> and add to the path.</p>
<h2 id="start-coding">Start Coding</h2>
<p>Now we are ready to proceed.
Make a new Python file i.e. <em>bot.py</em> and make some necessary imports.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> datetime <span style="color:#f92672">import</span> datetime
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> selenium <span style="color:#f92672">import</span> webdriver
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> selenium.webdriver.common.keys <span style="color:#f92672">import</span> Keys
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> selenium.common.exceptions <span style="color:#f92672">import</span> NoSuchElementException
</span></span></code></pre></div><p>Now we will create and initialize Firefox WebDriver and make a get request to open Whatsapp Web URL.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>driver <span style="color:#f92672">=</span> webdriver<span style="color:#f92672">.</span>Firefox()
</span></span><span style="display:flex;"><span>driver<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;http://web.whatsapp.com&#39;</span>)
</span></span><span style="display:flex;"><span>print(<span style="color:#e6db74">&#39;Please Scan the QR Code and press enter&#39;</span>)
</span></span><span style="display:flex;"><span>input()
</span></span></code></pre></div><p>The <em>print</em> and <em>input</em> functions are there just to give us time to scan the QR code to connect our phone&rsquo;s WhatsApp. Once we are connected we can hit enter to continue further execution of code.</p>
<p>Once the WhatsApp Web interface is open, we need to look for unread messages. Thankfully, all unread messages in left pane are individual HTML element with CSS class &lsquo;chat unread&rsquo;.</p>
<p>WebDriver have a function to find elements by CSS selector which will return the first element with the given argument. We will make use of this function.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span>content <span style="color:#f92672">=</span> driver<span style="color:#f92672">.</span>find_element_by_css_selector(<span style="color:#e6db74">&#39;.chat.unread&#39;</span>)
</span></span><span style="display:flex;"><span>content<span style="color:#f92672">.</span>click()
</span></span><span style="display:flex;"><span>input_form <span style="color:#f92672">=</span> driver<span style="color:#f92672">.</span>find_element_by_css_selector(<span style="color:#e6db74">&#39;.pluggable-input-placeholder&#39;</span>)
</span></span><span style="display:flex;"><span>input_form<span style="color:#f92672">.</span>send_keys(str(datetime<span style="color:#f92672">.</span>now()),Keys<span style="color:#f92672">.</span>RETURN)
</span></span></code></pre></div><p>Above code snippet is very simple. We are selecting first element with class <em>chat</em> and <em>unread</em> and clicking it. Then we need to find message box which is an HTML element with class <em>pluggable-input-placeholder</em>. <em>send_keys</em> function will send the keyboard event to the element. So we send the current time and RETURN key(Enter Key) to send the message.</p>
<p>As I have already mentioned that function <em>find_element_by_css_selector</em> will return only first element for given argument, So we need to put the whole snippet in a loop. Also, when there is no such element for the argument it will throw <em>NoSuchElementException</em>, so enclosing with <em>try</em> <em>except</em> is also necessary.</p>
<p>The final code will be</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> datetime <span style="color:#f92672">import</span> datetime
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> selenium <span style="color:#f92672">import</span> webdriver
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> selenium.webdriver.common.keys <span style="color:#f92672">import</span> Keys
</span></span><span style="display:flex;"><span><span style="color:#f92672">from</span> selenium.common.exceptions <span style="color:#f92672">import</span> NoSuchElementException
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>driver <span style="color:#f92672">=</span> webdriver<span style="color:#f92672">.</span>Firefox()
</span></span><span style="display:flex;"><span>driver<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;http://web.whatsapp.com&#39;</span>)
</span></span><span style="display:flex;"><span>print(<span style="color:#e6db74">&#39;Please Scan the QR Code and press enter&#39;</span>)
</span></span><span style="display:flex;"><span>input()
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">while</span> <span style="color:#66d9ef">True</span>:
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">try</span>:
</span></span><span style="display:flex;"><span>        content <span style="color:#f92672">=</span> driver<span style="color:#f92672">.</span>find_element_by_css_selector(<span style="color:#e6db74">&#39;.chat.unread&#39;</span>)
</span></span><span style="display:flex;"><span>        content<span style="color:#f92672">.</span>click()
</span></span><span style="display:flex;"><span>        input_form <span style="color:#f92672">=</span> driver<span style="color:#f92672">.</span>find_element_by_css_selector(<span style="color:#e6db74">&#39;.pluggable-input-placeholder&#39;</span>)
</span></span><span style="display:flex;"><span>        input_form<span style="color:#f92672">.</span>send_keys(str(datetime<span style="color:#f92672">.</span>now()),Keys<span style="color:#f92672">.</span>RETURN)
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">except</span> NoSuchElementException:
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">pass</span>
</span></span></code></pre></div><p>This code is just a proof of concept, you can obviously improve it for better performance.</p>]]></content:encoded>
    </item>
  </channel>
</rss>
