Save Time and Automate Your Life

Build Your Own Bill Paying Bot

Sats Sehgal
The Startup
5 min readJan 14, 2021

--

Photo by Alex Knight on Unsplash

It’s no surprise that we all lead extremely busy lives, and some automation can go a long way in helping us simplify some of the mundane tasks that we tend to put aside for “later”. I’m guilty, I may get a bill in the mail or online and intend to pay it right away but with working from home and having three small children at home during the pandemic, it’s not always at the top of mind. Usually stopping them from fighting or cleaning a runny nose or making a sandwich take priority over logging into to pay my bills right away.

So in the spirit of helping people learn how to code I created a bot that will pay my cell phone bill with a few small principles in mind. Those include:

  • Log into my account to check if I have a balance
  • If the balance is $0 then log me out.
  • If there is a balance then use my credentials to fill in my card information and pay the bill. I don’t store my card on their servers. I also use a prepaid card for security reasons. It send me a message when the bill is paid
  • While not in this code, the additional piece I will add is if the balance is higher then a specific threshold then don’t pay it, rather send me a text message. This is fairly straight forward to do Twillio.

So the next question would be, why don’t I just have automated billing whereby the telco would automatically bill me for my bill. I learned a very big lesson about 5 years ago where I was billed nearly 2x my monthly bill as a result of a billing error. There were certain credits that were to be applied to my account that fell off somehow. I dislike having to call in and deal with that and ask for a refund.

Another caution I would throw out there is to never share your credentials as that can lead to fraudulent activity. You also have to have a certain risk appetite and trust in the technology you build to go down this route. Finally, the bot is specific to Bell Canada though if you are familiar with web scraping porting this over to other providers isn’t a difficult task. It’s really about getting the framework setup. Ok so if you’re feeling up to it then let’s get started.

The code for this project is also available on githib here

The first item we would do is to import all of our dependencies. I used selenium for this project.

Now most of these dependencies are standard selenium packages. There is one that takes in data from a secrets file that will hold your credit card information. You can choose to store this in an environment variable as well.

Now let’s create a class that will initiate our project. I will call the class Bell since that is the provider but the elements within this class would be very similar. Everytime I initiate this class it opens the browser and logs me in with the following code

So now that we have our main class defined lets go ahead and build some functions. First let’s build a function that will get our account balance

In the function above the browser will use the XPATH to find the field that holds the account balance and print. This is saved as a float amount so that we can use is later down stream.

Ok now that we have our bank balance we need to determine whether a payment needs to be made.

In the code above if there is a balance to be paid then we will call the make payment function otherwise we can print no payment required and again use XPATH to find the logout element and log out of the system.

So let’s follow the unhappy path in that a payment is required. We would then need to define the make payment function. Its big…brace yourself

Ok let’s recap what the code above does, first it will click the necessary buttons to get to the payment screen. It will then take your credit card details in the secrets file and populate the required information, pay and log you out. Again I would update this code in the future to include a threshold and that is if the payment is more than 10% of what I normally pay, to stop and notify me and log me out.

The final piece would be to execute this code using the following few lines of code which will take in your username, password and credit card details.

Again this bot was a fun little project to test out some python skills and code but at the same time build something that can help automate things for me. What you can do is actually automate and setup the bot to run once a day through a cron job or python automation to check for the balance and execute. Again, you get notified when the bill is paid, but again the control of paying the bill still resides with you rather than automated billing.

Whenever I develop code or direct my team to do so, reusability is a key factor in developing any code. How do I use this code for other applications? Well the obvious point is make some changes to the code and use it for other bills. You can use similar logic to pull all your bills from the multiple collectors and build a budgeting app. What if I told you that you can use similar logic for stock trading (with the appropriate API to place the trade). Last you can build a bot to check inventory of products in stores. I did a different video on building a selenium bot for merchandise stock updates which you can find here. The truth is leveraging something like selenium presents endless options for you to dabble into for building automation and even matching learning applications.

Hopefully you enjoyed this walkthrough. To learn more about AI training, bot development, cloud capabilities and automation be sure to check out levers.ai

--

--

Sats Sehgal
The Startup

Sats is a data and analytics business executive. He enjoys working with organizations to create Data, AI and Digital Strategies. He also enjoys teaching coding