Create a WhatsApp Bot that schedules, Automates Messages on the cloud.
Tags: webdev, cloud, bot, javascript
Published: 2023-02-05 10:13:16


<figure class="graf graf--figure"></figure>
<p class="graf graf--p">In this article, we will create a bot that automates your message and sends them automatically and on time as long as you need, even if your network is down or unavailable.</p>
<p class="graf graf--p">To jump on this tutorial, you need the following.</p>

<h3 class="graf graf--h3">Perquisite</h3>
<ul class="postList">
 	<li class="graf graf--li"><a class="markup--anchor markup--li-anchor" href="https://www.twilio.com/" target="_blank" rel="noreferrer nofollow noopener noopener noopener" data-href="https://www.twilio.com/">Twilio </a>account</li>
 	<li class="graf graf--li">Git Account</li>
 	<li class="graf graf--li">Code Editor of your choice — VsCode</li>
 	<li class="graf graf--li"><a class="markup--anchor markup--li-anchor" href="https://www.heroku.com/" target="_blank" rel="noreferrer nofollow noopener noopener noopener" data-href="https://www.heroku.com/">Heroku </a>— Free cloud hosting Service account</li>
 	<li class="graf graf--li">PIP — Python Installed</li>
</ul>
<h3 class="graf graf--h3">Twilio</h3>
<p class="graf graf--p">Click on the link to get started and register on Twilio. If you do not have an account, after installing, it will authenticate your account and ask you to fill this.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*y5IIix7raTS6GNwK.PNG" data-image-id="0*y5IIix7raTS6GNwK.PNG" data-width="712" data-height="598" /></figure>
<p class="graf graf--p">After filling out our form and logging in, open your file folder and create a new folder. If you want to be fast, open your terminal and cd into your preferred location like this.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*-n02SJfczSGx5Mi-.PNG" data-image-id="0*-n02SJfczSGx5Mi-.PNG" data-width="662" data-height="527" /></figure>
<p class="graf graf--p">First, cd desktop to go to the desktop directory, then create a new folder by running mkdir whatsappBot, create another folder called Bot1 inside the whatsappBot.</p>
<p class="graf graf--p">Next, we are going to test if we have python installed by running python — version</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*TwqgY0fdt5GnW8tV.PNG" data-image-id="0*TwqgY0fdt5GnW8tV.PNG" data-width="523" data-height="142" /></figure>
<p class="graf graf--p">From this point, we will be using the terminal on Vscode, you can find the terminal of Vscode here.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*nKHM0STgurZKjaxj.PNG" data-image-id="0*nKHM0STgurZKjaxj.PNG" data-width="838" data-height="398" /></figure>
<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*BLoLgw_FzoO096sG.PNG" data-image-id="0*BLoLgw_FzoO096sG.PNG" data-width="488" data-height="163" /></figure>
<p class="graf graf--p">Make sure you navigate to the Bot1 folder when you open your Vscode and click on a new terminal.</p>
<p class="graf graf--p">Next, we are going to create a python file where we will write our codes. Notice that I used a command called touch and the name of the file. If your system throws an error that it does not recognize touch, run npm <strong class="markup--strong markup--p-strong">install</strong> <strong class="markup--strong markup--p-strong">touch</strong>-CLI -g this will install the dependency, and you can use touch now.</p>
<p class="graf graf--p">You should be able to see the new file in your folder with an extension of .py</p>
<p class="graf graf--p">Now we need to go back to the Twilio dashboard when you log in, click on develop on the left, scroll down to messaging and click on send a WhatsApp message.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*NaW5JHJcpCEpTety.PNG" data-image-id="0*NaW5JHJcpCEpTety.PNG" data-width="869" data-height="444" /></figure>
<p class="graf graf--p">You will be asked to run this join enter-forget to a number on WhatsApp to activate your sandbox, if you do that correctly you should see this.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*5aCZJ6vG3-y0ajXE.PNG" data-image-id="0*5aCZJ6vG3-y0ajXE.PNG" data-width="361" data-height="190" /></figure>
<p class="graf graf--p">Next, we send a one-way message to test out the bot if we can send messages from Twilio to our number.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*J7LNtngmr56RwIm5.PNG" data-image-id="0*J7LNtngmr56RwIm5.PNG" data-width="1066" data-height="131" /></figure>
<h3 class="graf graf--h3">Using Our Local Machine</h3>
<p class="graf graf--p">We are going to run our bot from our command line and not from Twilio.</p>
<p class="graf graf--p">Copy the codes, paste them on your code editor on the left, and click on show Auth Token and python.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*_UBm_IGbu_5P-vcb.PNG" data-image-id="0*_UBm_IGbu_5P-vcb.PNG" data-width="1064" data-height="480" /></figure>
<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*aYVr9FmPVme7r_Ye.PNG" data-image-id="0*aYVr9FmPVme7r_Ye.PNG" data-width="1119" data-height="621" /></figure>
<p class="graf graf--p">Next, we are going to install the Twilio library in vscode so we can do all the above with codes, pip install twilio but don’t get confused, if you have installed python, pip comes with it so you don’t have to install it again.</p>
<p class="graf graf--p">Let’s test our bot from our local machine and see if it will work.</p>

<ul class="postList">
 	<li class="graf graf--li">First, we must change put in the correct recipient number in the To</li>
 	<li class="graf graf--li">Next, we must change the auth_token id to the live token, you can get your live token from your dashboard by clicking on account and API keys, and Tokens make sure you copy the live token and not the test token.</li>
</ul>
<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*VoNBOIriLzGaiH-h.PNG" data-image-id="0*VoNBOIriLzGaiH-h.PNG" data-width="539" data-height="363" /></figure>
<p class="graf graf--p">Next, we need to create a scheduler that handles the timing of our message hitting our targets, click <a class="markup--anchor markup--p-anchor" href="https://apscheduler.readthedocs.io/en/3.x/" target="_blank" rel="noreferrer nofollow noopener noopener noopener" data-href="https://apscheduler.readthedocs.io/en/3.x/">here</a> to follow the guild.</p>
<p class="graf graf--p"><code class="markup--code markup--p-code">pip install apscheduler</code></p>
<p class="graf graf--p">After installing, create a new schedule.py file and populate it with this. Make sure to check for indent errors.</p>
<p class="graf graf--p"><code class="markup--code markup--p-code">from apscheduler.schedulers.blocking import BlockingScheduler</code></p>
<p class="graf graf--p"><code class="markup--code markup--p-code">from botfiles import love</code></p>
<p class="graf graf--p"><code class="markup--code markup--p-code">sched = BlockingScheduler()</code></p>
<p class="graf graf--p"><code class="markup--code markup--p-code">sched.add_job(love, 'interval', seconds=2)</code></p>
<p class="graf graf--p"><code class="markup--code markup--p-code">sched.start()</code></p>
<p class="graf graf--p">From apscheduler.schedulers.blocking import BlockingScheduler we imported the Blockingscheduler.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*-sPFsmAmwTPi3LVp.PNG" data-image-id="0*-sPFsmAmwTPi3LVp.PNG" data-width="712" data-height="140" /></figure>
<p class="graf graf--p">Next, we defined a new function in our botfiles.py and imported the function to our new schedule.py</p>
<p class="graf graf--p">We created a new variable of BlockingScheduler()</p>
<p class="graf graf--p">we added the job and called our function sched.add_job(love, ‘interval’, seconds=2) and specify how long we want our bot to wait before it sends a message again.</p>
<p class="graf graf--p"><code class="markup--code markup--p-code">sched.start()</code></p>
<p class="graf graf--p">Our bot begins.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*_NuoihCHiwD2yBo8.PNG" data-image-id="0*_NuoihCHiwD2yBo8.PNG" data-width="495" data-height="606" /></figure>
<p class="graf graf--p">At this point, if we test our bot we notice a ton of messages every 2 seconds, and this is spamming, you can increase the time length but I just wanted to show you how reliable our bot can be.</p>

<h3 class="graf graf--h3">Deploy your bot on the Cloud</h3>
<p class="graf graf--p">We have succeeded in automating the process of our bot sending messages, but what if we are offline and we can not start the process or our local machine shuts down, we are going to deploy the application on the cloud.</p>
<p class="graf graf--p">Click <a class="markup--anchor markup--p-anchor" href="https://dashboard.heroku.com/apps" target="_blank" rel="noreferrer nofollow noopener noopener noopener" data-href="https://dashboard.heroku.com/apps">here</a> to signup or login if you already have an account.</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*VS6VdP4Tq2rYbgUs.PNG" data-image-id="0*VS6VdP4Tq2rYbgUs.PNG" data-width="1019" data-height="507" /></figure>
<p class="graf graf--p">Click on Create a new app</p>

<figure class="graf graf--figure"><img class="graf-image" src="https://cdn-images-1.medium.com/max/1600/0*8KRA2ZQk9g2zPcjP.PNG" data-image-id="0*8KRA2ZQk9g2zPcjP.PNG" data-width="798" data-height="367" /></figure>
<p class="graf graf--p">Specify the application's name and choose the region; suggest you use the United States. Click on create an app, and the steps to follow to use Heroku will be shown to you.</p>
<p class="graf graf--p">Click on download and install <a class="markup--anchor markup--p-anchor" href="https://devcenter.heroku.com/articles/heroku-cli" target="_blank" rel="noreferrer nofollow noopener noopener noopener" data-href="https://devcenter.heroku.com/articles/heroku-cli">Heroku CLI</a>, and follow the requirement for your OS.</p>
<p class="graf graf--p">After installing, login to your Heroku account from your command line heroku login we need to do two things, first, we need to create a requirement file where we will tell Heroku what dependencies to install and run for our bot to work.</p>
<p class="graf graf--p">Let us go back to Vscode and create these files.</p>
<p class="graf graf--p">First, we create the requirement.txt file and add the following dependencies</p>
<p class="graf graf--p"><code class="markup--code markup--p-code">twilio == 6.35.0</code></p>
<p class="graf graf--p"><code class="markup--code markup--p-code">APScheduler == 3.6.1</code></p>
<p class="graf graf--p">Next, we will create a file called Procfile, please make sure the first P is a capital letter, and we will add the name of the scheduler as the file we want Heroku to run.</p>
<p class="graf graf--p"><code class="markup--code markup--p-code">appscheduler : python appscheduler.py</code></p>
<p class="graf graf--p">Now you should have four files inside your folder; we will go back to our terminal and add all of the files to the Heroku server.</p>

<ul class="postList">
 	<li class="graf graf--li">First, we initialize the folder with the code git init</li>
 	<li class="graf graf--li">Next, we set our remote host to our app heroku git:remote -a bot9ja check the heroku page after creating the app and copy directly.</li>
 	<li class="graf graf--li">Next, add all the files to source control by using git add . the dot makes sure we add all the files.</li>
 	<li class="graf graf--li">Next, we commit our files with git commit -am “make it better” and this sets them ready to be deployed.</li>
 	<li class="graf graf--li">Finally, we push our codes with git push heroku master</li>
</ul>
<h3 class="graf graf--h3">Conclusion</h3>
<p class="graf graf--p">In this article, we saw how to build a Whatsapp bot using Twilio, appscheduler, and host it on Heroku.</p>
<p class="graf graf--p">If you have any issues working on this, please feel free to send me a message or check out these resources for reference.</p>

<ul class="postList">
 	<li class="graf graf--li"><a class="markup--anchor markup--li-anchor" href="https://www.twilio.com/docs/quickstart" target="_blank" rel="noreferrer nofollow noopener noopener noopener" data-href="https://www.twilio.com/docs/quickstart">Getting started with Twilio</a></li>
 	<li class="graf graf--li"><a class="markup--anchor markup--li-anchor" href="https://www.tutorialspoint.com/event-scheduler-in-python" target="_blank" rel="noreferrer nofollow noopener noopener noopener" data-href="https://www.tutorialspoint.com/event-scheduler-in-python">Event Scheduler in python</a></li>
 	<li class="graf graf--li"><a class="markup--anchor markup--li-anchor" href="https://devcenter.heroku.com/start" target="_blank" rel="noreferrer nofollow noopener noopener noopener" data-href="https://devcenter.heroku.com/start">Getting started with Heroku</a></li>
 	<li><a href="http://blog.learnhub.africa">Learnhub</a></li>
</ul>