So, let’s get started! One solution is to use Python’s Selenium package 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 thinking of them everyday (kind of), we’ll do this in a three-step process: All of this may be discouraging, but the free tier solution does the job fine. Also, for now, this is the only available way. Now you have to connect the receiver’s phone to the WhatsApp Sandbox to start receiving messages. To do this, head to WhatsApp beta in the learn section of the console.
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 message 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 account SSID and authentication 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 extract it. Inside this, you’ll find your code file and deployment package:
whatsapp_messaging.py aws_lambda_deploy.zip
twilio_sid auth_token contact_directory from_ body (optional)
After you have changed these, save it. Then extract the aws_lambda_deploy.zip and replace the whatsapp_messaging.py inside it with your newly created one. Zip the package again. We just wanted to change the code with your credentials and contact details. Your deployment package is now ready.
AWS Lambdais a serverless computing service where you have a piece of code you want to run based on different AWS events and triggers, according to the users’ needs. Therefore, it’s a waste of computing resources and money to run a server (EC2 instance in AWS) 24/7 to do our small task. Here, our Lambda function will only run for a very short time every day on our mentioned time trigger. The Lambda service is very cheap and gives you a million requests per month for free.
Once you’ve completed those steps, it’s time to give your function a name. We selected Python 3.6 as our environment of choice as we don’t need to connect to other services in AWS, the option of basic permission is fine. Now, click oncreate function and you’ll be taken to the main dashboard.
In the function code block, specify to the Lambda Function Handler that we want to run ourwhatsapp_messagingPython file and themsg_mom_and_dadfunction inside it each time the Lambda function is called. To do this, change the handler value as above. At the code entry point, selectupload a .zip file, upload the deployment package you created in step 2, and saveit. Our code is ready to run. You can click ontestand check that the function successfully sends a message to the specified WhatsApp number. Our last step is to trigger it every day at a given time. To do this, click add trigger > CloudWatch Events in the designer box.
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 specified 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 setand?to specify every day, month, and year. You can refer to the example below to create your owncron parameters. Otherwise, you can refer to thecron guide onaws_cron_docsto learn it in depth.
After you’re done! But make sure the Enable trigger checkbox is checked. Finally, click the add button.
On the Lambda function dashboard, you can now see your CloudWatch Event attached to your Lambda function. Upon scrolling down, you can see the CloudWatch event enabled to trigger your function. That’s it! You can change the Twilio Sandbox name to your name and can even respond to replies from the Twilio dashboard. You truly are the favorite child.
This article was originally published on Better Programming by Kartik Nighania, a Software Engineer at HSBC. You can read the original article here.