In our busy work lives, we often forget to WhatsApp our loved ones. Looking at how approved my parents are messaging me with adorning quotes and health tips first thing in the morning, it was time to reciprocate.

In this tutorial, we will write a simple Python script to send a WhatsApp bulletin every morning. Twilio is the Python amalgamation we will use. To run it every day at a assertive time, we’ll put our code on the AWS (Amazon Web Services) cloud.

So, let’s get started!

One band-aid is to use Python’s Selenium amalgamation and go to WhatsApp web rather than using Twilio, which is subscription-based after the free tier is over. But as WhatsApp requires QR-code scanning through mobile from time to time, automation isn’t possible.

To let your parents know you’re parents cerebration of them accustomed (kind of), we’ll do this in a three-step process:

  • Initially setup your Twilio profile.
  • Understand the code and modify it.
  • Put our amalgamation on AWS lambda with triggers.

Step 1: Initially ambience up your Twilio profile

Firstly, create a free Twilio annual and affirm your email abode and mobile number.  Also, a free tier Twilio annual requires using Twilio Head for WhatsApp, which means you can’t use your number and have to go through ancient permission to accept WhatsApp messages. Both can be solved when you get your own number, which is done after WhatsApp approves Twilio to use your number. There is a form to fill out and a wait time too.

webrok

All of this may be discouraging, but the free tier band-aid does the job fine. Also, for now, this is the only accessible way. Now you have to affix the receiver’s phone to the WhatsApp Head to start accepting messages. To do this, head to WhatsApp beta in the learn section of the console.

webrok

webrok

Now, save the WhatsApp number assigned to you in your contacts — you can give it any name you want. For simplicity, I saved it as Twilio Sandbox, then sent it a bulletin from my dad’s phone, as seen above. This has to be done once and only once.

Now go to the Twilio Console and retrieve your annual SSID and affidavit token. This will help Twilio know it’s you when the code is executed.

Step 2: Understanding and modifying the code

Download the GitHub repository and abstract it. Inside this, you’ll find your code file and deployment package:

  • whatsapp_messaging.py
  • aws_lambda_deploy.zip
webrok
  • line 1 imports the Twilio amalgamation and uses its REST client to hit the Twilio API.
  • line 3: We create a function, msg_mom_and_dad, which will be given to AWS to run every day at a assertive time.
  • line 6–7: Replace the sid and auth_token of your annual as discussed in step 1.
  • line 9: Twilio client object is created with our credentials.
  • line 13: A Python concordance is created with name as key and mobile number as value. You can keep adding to this Python concordance to send letters to more people as well.
  • line 15: A for loop that runs through all the key and value pairs (currently we just have one). In body, mention your message. I have created a simple one, which says good morning, followed by the key value. In the above code, it is “good morning daddy!” We then acknowledgment the from number, which is the Twilio WhatsApp number you got earlier, and the to number, from which you ahead sent the WhatsApp head confirmation.
  • line 23: A line to check bulletin status by press the SID. We won’t use this anyway.

Now, we have five things to change:

  • twilio_sid
  • auth_token
  • contact_directory
  • from_
  • body (optional)

After you have afflicted these, save it. Then abstract the aws_lambda_deploy.zip and alter the whatsapp_messaging.py inside it with your newly created one. Zip the amalgamation again. We just wanted to change the code with your accreditation and acquaintance details. Your deployment amalgamation is now ready.

Step 3: Put your amalgamation on AWS Lambda with triggers

Our code is ready to run and send WhatsApp messages! If you’re apprehensive what the other files in our deployment amalgamation are, they’re the Twilio amalgamation and all its other dependencies. This is because we’ll use an AWS Lambda action in a Python ambiance that doesn’t have the Twilio package. But why not just runpip install twilioto install it? This is because we don’t have a server here.

webrok

AWS Lambdais a serverless accretion account where you have a piece of code you want to run based on altered AWS events and triggers, according to the users’ needs. Therefore, it’s a waste of accretion assets and money to run a server (EC2 instance in AWS) 24/7 to do our small task. Here, our Lambda action will only run for a very short time every day on our mentioned time trigger.

The Lambda account is very cheap and gives you a actor requests per month for free.

webrok

So, our next step: Log in to AWS Amazon. Then, select  -> -> -> create a function.

webrok

Once you’ve completed those steps, it’s time to give your action a name. We called Python 3.6 as our ambiance of choice as we don’t need to affix to other casework in AWS, the option of basic permission is fine. Now, click oncreate function and you’ll be taken to the main dashboard.

webrok

In the action code block, specify to the Lambda Action Abettor that we want to run ourwhatsapp_messagingPython file and themsg_mom_and_dadfunction inside it each time the Lambda action is called. To do this, change the abettor value as above.

At the code entry point, selectupload a .zip file, upload the deployment amalgamation you created in step 2, and saveit. Our code is ready to run. You can click onand check that the action auspiciously sends a bulletin to the defined WhatsApp number.

Our last step is to activate it every day at a given time. To do this, click in the artist box.

webrok

We’ll now have to create a new rule. You can give it any name and description you want. Then, set the rule type asschedule expression. We defined the time usingcron().30 1refers to 1:30 am, UTC (equivalent to my 7 AM, IST time). The next two,* *, are for the day of the month and the month. The next two,? *,are for the day of the week and year. We set*and?to specify every day, month, and year. You can refer to the archetype below to create your owncron parameters. Otherwise, you can refer to thecron guide onaws_cron_docsto learn it in depth.

webrok

After you’re done! But make sure the  checkbox is checked. Finally, click the add button.

webrok

On the Lambda action dashboard, you can now see your CloudWatch Event absorbed to your Lambda function. Upon scrolling down, you can see the CloudWatch event enabled to activate your function.

That’s it! You can change the Twilio Head name to your name and can even acknowledge to replies from the Twilio dashboard. You truly are the admired child.

webrok