You might be considering:
OK, however doesn’t Alexa already present a method to buy merchandise? Absolutely, this why Amazon created Alexa?
Truly, it does! Pretty just lately, the Alexa app began permitting you to Shop your list. You merely add gadgets to your Alexa checklist after which use the Alexa app to pick out from an inventory of Contemporary or Entire Meals merchandise:
This interface is fairly slick, however there’s one drawback. My spouse says:
I’ve already thought of boycotting Amazon as they aren’t essentially the most socially accountable firm. We already purchase an excessive amount of on Amazon and we have to help different grocery shops.
As a lot as a I really like the comfort, velocity, and costs on Amazon, she’s proper! So, for now, we’ll stick with purchasing at Safeway. Certain, Safeway (a subsidiary of Albertsons) is without doubt one of the 5 largest grocery chains within the US, however a minimum of it isn’t Amazon.
I used to be nonetheless left questioning if Safeway or Amazon is cheaper. So, I did a fast price comparability between Safeway and Amazon Contemporary by constructing a purchasing cart of 32 related gadgets on every of their web sites. In doing this, I discovered that their pricing was roughly the identical for my basket of merchandise, e.g. round $150:
Understanding that we wished to proceed utilizing Safeway for grocery supply and Alexa for our purchasing checklist, I started to marvel how I might automate my purchasing? Enter: Robotic Course of Automation (RPA)
For those who haven’t heard of it, RPA is just a method of automating repetitive duties, which are usually finished by a human, with a software program robotic. These are sometimes duties finished in Graphical Person Interfaces (GUIs) like net browsers, MS Excel, e mail shoppers, and so forth…
I had been casually studying about RPA for the previous few years and even pitched it to my non-technical management friends at work, once they have been searching for a strategy to automate operational processes with out distracting our already-spread-thin engineers. It appeared like the proper alternative for me to leap in and construct an RPA bot.
After doing a little fast analysis, I shortly discovered that there are a selection of pricy proprietary RPA distributors like UIPath and Automation Anyplace. I don’t doubt that these methods are price what you pay for them as they’ll in the end scale back your labor prices. However, there are additionally a number of open-source options like Robot Framework and TagUI, and as a frequent contributor to open supply, I’m an enormous fan of open supply.
At the moment, Robotic Framework has essentially the most GitHub stars and is supported by an in depth neighborhood. So, I fired up a fast Getting Started with Robot Framework Tutorial and located it fairly simple to construct a bot that might navigate an internet site. I discovered it very cool that Robotic Framework is a layer on high of Python and subsequently, it’s simple to insert customized Python code into the bot. And, what I discovered notably cool is that there are community provided libraries for all sorts of issues.
Earlier than we get into how I constructed my bot, let’s focus on my objectives. As a software program engineer, I knew I might craft some code that fired off API requests to the Todoist and Safeway backends. Nevertheless, this may sidestep my need to play with the Robotic Framework. As a substitute, I opted to make use of the SeleniumLibrary with Robotic Framework, which beneath the hood would permit me to construct a bot that makes use of Selenium to manage a browser in a method that’s similar to how I’d in any other case manually use a browser to do my grocery purchasing.
Later, I’ll point out among the pitfalls I discovered with RPA and why sooner or later, I’d select to make an API-based bot as an alternative of a GUI-based bot. In truth, Todoist seems to have a supported developer API. Safeway/Albertsons doesn’t seem to have a developer API, however its web site does make backend calls and subsequently you possibly can reverse engineer the backend API.
My different objective was to simplify the deployment structure by not introducing one other datastore. In different phrases, I wished my bot to simply use the info in Todoist and Safeway and never should retailer something in a separate datastore. Finally, I’m capable of do my grocery purchasing with out writing down something additional so why can’t the bot operate with out a further database?
Within the subsequent few sections, I’m going to be strolling via excerpts of my bot code. Please be happy to obtain the total supply code at github.com/redgeoff/grocery-shopping-bot
In case you are new to the Robotic Framework, please learn via Robot Framework Quick Start: UI Automation Tutorial.
Reusable key phrases
The Robotic Framework refers to your typical programming language operate as a key phrase. We’re going to outline some key phrases in order that we are able to summary among the tough edges that you just’ll encounter when working with an internet site in a browser. Particularly, web sites are beasts that load many components asynchronously, which signifies that components might not but be seen or enabled. Bots are inherently a lot quicker than people so in the event you aren’t cautious, it’s simple on your bot to error out from the UI not but being prepared or as a result of different race situations happen. In fact, you possibly can insert Sleep
statements in your code, which might simulate the timing {that a} human would encounter, however that is an anti-pattern as a result of:
- Including
Sleep
statements will make your bot a lot slower - Timing can range based mostly on the community, the backend servers, and even your compute energy.
As a substitute, we’ll create some helper key phrases that may anticipate components to be seen and enabled, and even introduce a retry loop for when the web page takes an exorbitant period of time to load or an sudden error happens.
Our first key phrase will likely be Wait Till Ingredient Prepared
:
Subsequent, we’ll outline Click on Ingredient When Prepared
, which makes use of Wait Till Ingredient Prepared
after which clicks the factor.
To make it in order that we are able to resume the identical browser session, which is able to assist us to keep away from Todoist sending us an e mail every time we log in, we’ll borrow a trick from this post.
Lastly, utilizing a trick from this post, we’ll outline a strategy to get baby components. This can assist us to do issues like get an inventory of groceries after which pluck out the nested attributes.
The precise bot, has plenty of other reusable keywords, however they’re omitted from this put up for brevity.
We’ve now abstracted among the less-fun stuff, let’s soar into making the bot do it’s thang!
Get the checklist from Todoist
The very first thing we would like the bot to do is log in to Todoist, choose our desired checklist, after which save the checklist to reminiscence. The Todoist web page that lists our gadgets, appears like this:
To keep away from making additional log-ins and triggering emails from Todoist, we’ll make it in order that the bot solely logs in once we aren’t already logged in. That is notably useful throughout testing when you could run the script MANY instances to search out all of the race situations that consequence from navigating the web site within the browser. Naturally, you possibly can log in (and sign off) every time the script runs in the event you’d want.
For those who take a more in-depth take a look at the Get Todoist Listing Objects
key phrase, you’ll see that we’re utilizing an XPath question to get all of the li
components with a task_list_item
class. After which we pluck the list_item
baby factor in order that we are able to get the merchandise title
and id
. (Sure, that is how Todoist identifies the gadgets of their HTML and in the event you suppose this appears brittle… nicely, yup)
Yow will discover the entire code at todoist.robot.
Purchase it once more
After we get the gadgets from Todoist, the following step is so as to add corresponding gadgets to our Safeway cart. Now, we might simply go to the Safeway search web page and seek for the merchandise, however the search web page orders by Finest Match and doesn’t will let you order by essentially the most just lately bought gadgets.
The issue is that typically we purchase gadgets simply because others are out of inventory, despite the fact that we’ve a choice for the gadgets which are out of inventory. As a substitute, I wish to first give choice to the gadgets that we incessantly buy. This manner, we’ll at all times try to first seize our most popular merchandise, even when it’s not in inventory till the following time we store. Certain as a human, I can merely decide what I would like from the checklist, however the bot isn’t that sensible. For the bot, we are able to do that by going to the Purchase It Once more web page and sorting by Continuously Bought:
Our bot will then iterate via the checklist of things that we obtained from Todoist and search for the primary match in Safeway’s Purchase It Once more checklist. It can then add that merchandise to the Safeway cart.
Did you discover the enjoyable stuff taking place in Add To Cart
? Effectively, the Safeway web site requires that you just mouse over an merchandise after which click on it earlier than it may be added to the cart. As for the Wait Till Web page Does Not Include Ingredient
, the web site flashes a loading icon when an merchandise is added to the cart and we wish to wait till this icon is now not displayed as this confirms that the merchandise is in our cart. (Professional Tip: to examine a disappearing factor in Chrome, open the Sources tab, and hit F8 to cease the browser execution and hold the icon displayed)
You’ll be able to view the entire Safeway robotic code at safeway.robot.
Seek for gadgets not bought earlier than
In case you are like me, and most of your grocery purchasing consists of the identical 300 or so gadgets, the bot may have accomplished virtually your complete checklist simply by purchasing from the Purchase It Once more (just lately bought) web page. The remaining handful of things in your checklist will possible be these that you’ve got by no means bought earlier than, e.g. you are attempting a brand new recipe or your typical model is out of inventory.
There are additionally different the reason why you’ll have remaining gadgets in your checklist:
- Safeway truncates lengthy product names, e.g. Signature Care Wipes Delicate…
- The formatting of the gadgets in your checklist might not match, e.g. nutrigrain vs nutri-grain
- The gadgets in your checklist have names that don’t match the title of the Safeway merchandise. As you may anticipate, Safeway associates hidden key phrases with every product. For instance, you could have pepper jack cheese in your checklist, however the Safeway product could also be known as Cheese Slices Pepper Jack
To deal with these circumstances, our bot, visits the Safeway search web page and easily picks the primary merchandise that’s in inventory so as to add to the cart:
Mark Todolist gadgets accomplished
By now, our bot created a cart with each merchandise it might discover. The subsequent step is to mark these things as accomplished on our Todoist checklist.
Maintain on, what’s up with these Sleep
statements? Shouldn’t we keep away from them? Sure, however Todoist seems to have some form of charge limiting in place. In different phrases, if our bot it too quick, the web site will error out as people don’t full gadgets that shortly. In my testing, I discovered that once I took 2 seconds in between gadgets and waited 10 seconds after every batch of 10 gadgets I averted this charge limiting. (You might be saying as soon as once more, however this appears brittle… yup! 😵💫)
Ship an e mail with the checklist of things added to cart
The very last thing that I wished my bot to do was to ship an e mail that lists all of the gadgets that have been added to the cart. The e-mail ought to checklist the title of the merchandise because it appeared on my Todoist checklist and because it seems in my Safeway purchasing cart. I’m then capable of examine this e mail shortly to see if something appears misplaced. I’ve usually discovered that a minimum of a pair gadgets look funky.
One uncommon cause is that Safeway’s search could be a little buggy. For instance, in the event you seek for bananas, the primary result’s a cabbage:
One other factor that may happen is {that a} easy Todoist merchandise like onion can truly match each Inexperienced Onion and Yellow Onion within the Purchase It Once more checklist. A method that I’ve thought of bettering this sooner or later is to have a Replacements checklist that I can use to coach the bot in order that every time I say onion, I imply Yellow Onion. For the MVP of my bot nonetheless, a easy e mail like the next is sufficient to empower me to make a number of handbook tweaks to my cart:
From what I can inform, sending an e mail with Robotic Framework isn’t as out-of-the-box as you’ll hope. Robocorp, a fairly cool internet hosting firm for Robotic Framework bots, maintains an RPA Library for Email, however I discovered that it didn’t play nicely with the ppodgorsek/robot-framework
Docker picture that I used to be utilizing to run my robotic in a container. As well as, I used to be excited to make use of a customized Python script in my bot and knew that sending an e mail with Python is trivial. So, I merely created a customized E-mail.py file:
and uncovered it by way of a Ship E-mail
key phrase:
The E-mail.py
script connects to an SMTP server and sends an HTML e mail. I merely arrange a brand new Gmail account and enabled SMTP entry by enabling the allow less secure apps option.
Notice: it is vitally possible that emails despatched this fashion will find yourself in your Spam folder so it’s possible you’ll have to create a filter to maintain them out of your Spam folder. Sending non-spamy emails from a correct mail host is a slightly concerned course of and is out of the scope of this put up.
Right here’s a fast video that demos the way to run the bot:
To run the bot regionally, observe the steps here.
After you run the bot, you’ll see that an output
listing was created and it incorporates some new artifacts:
You’ll be able to open the log.html
to view an in depth log from the bot, e.g.
This log, and the embedded screenshots, could be fairly useful to troubleshoot any points.
In case you are a Visible Studio (VS) Code consumer, I discovered that the Robot Framework Intellisense extension could be very useful.
In case you are not already acquainted with the Examine characteristic within the browser console, I recommend that you just give it a attempt because it permits you to uncover the construction of the weather on a webpage:
As you could have seen within the code above, as a result of Todoist’s and Safeway’s web sites aren’t actually essentially the most accessible, the bot makes use of XPath to navigate the web sites. For instance, we choose all of the Todoist gadgets with:
Get WebElements xpath=//li[contains(@class,'task_list_item')]
A lesser recognized characteristic of Chrome and Firefox is that you may truly take a look at out your XPath question within the console with the $x()
operate:
What if you wish to run the bot with no UI (headless) and within the background? Straightforward, simply observe the instructions at Run Locally with Docker.
What if you wish to deploy the bot to the cloud? I’ll cowl this intimately in a later put up, however for now, check out the steps at Deploy to AWS for a way I run the bot for simply pennies a month.
This bot is only a Minimal Viable Product (MVP). Listed here are some issues that I feel could possibly be added to make it much more helpful:
- As talked about above, an inventory of Replacements could possibly be used to substitute gadgets like onion with Yellow Onion in order that it doesn’t simply match Inexperienced Onion.
- Assuming {that a} human will carry out minor tweaks to the checklist after the bot has run, it needs to be potential for the bot to study from these tweaks. In different phrases, if the bot determines {that a} Yellow Onion was bought, when the unique merchandise on the checklist was onion, it might add
onion => yellow onion
to the Replacements checklist in your behalf. - Key phrase enlargement could possibly be used so {that a} single phrase can characterize a basket of things. For instance, your loved ones might eat a whole lot of fruit and need the fruit key phrase to incorporate strawberries, raspberries, and blueberries. This characteristic may be good for ordering all of the groceries for a dish. Maybe, that is carried out as a part of the Replacements checklist, i.e.
lasagne => ricotta, beef, eggs, ...
- Help for merchandise counts. For instance, 7 bananas would add 7 bananas to the purchasing cart. For now, the bot provides only one banana to the cart and a human has to extend the rely manually after the bot has completed executing.
- On Safeway’s product pages there’s typically an choice to clip a coupon for a particular product, i.e. it can save you some cash on the acquisition. The bot ought to do that for you.
- As proven above, Alexa’s store your checklist characteristic gives a UI that permits you to choose from an inventory of the highest search outcomes that match the merchandise in your purchasing checklist. There could possibly be worth in displaying such an interface after the bot has run to be able to make any wanted tweaks shortly.
- The bot might analyze your buying habits and make suggestions to purchase sure gadgets in bulk once you eat gadgets quick sufficient and when buying in bulk would lead to saving cash.
- Use machine studying to recommend merchandise which have by no means been shopped for (new merchandise) based mostly in your current preferences for model, value, taste, and dimension.
- Help for different grocery shops. The prevailing bot already helps any sensible speaker that integrates with Todoist, but it surely solely helps the grocery retailer, Safeway.
Most likely not. As a lot enjoyable because it was taking part in with the Robotic Framework, the usage of the framework, via the browser, led to a reasonably brittle resolution. If Todoist or Safeway determine to vary their web sites, the bot will more than likely break.
I’d argue that one of the simplest ways to implement such a bot is to make use of an formally supported developer API, resembling what’s offered by Todoist (or Alexa). The principle cause is that this API is a contract with which Todoist will try to stick. Or, in different phrases, it will make it lots much less possible that Todoist would introduce a change that might break the bot.
Safeway alternatively, doesn’t seem to have a supported developer API. As a substitute, you possibly can use the browser’s Community tab to reverse engineer their backend API. Utilizing this API would considerably enhance the bot in that it will:
- Make it much less depending on the presentation layer (HTML) of the web site. In different phrases, the look of the web site might change, however so long as the backend API doesn’t change, the bot wouldn’t break. Though Safeway doesn’t expose the developer API formally, they possible have groups of engineers engaged on the backend and frontend and they might more than likely keep away from breaking the API because it might trigger instability of their web site and scale back their staff’s velocity.
- Make it a lot quicker as navigating the GUI is SLOW.
- Cut back the quantity of assets that the bot consumes as making a RESTful API name takes lots much less assets than working a browser
- Cut back the chance for race situations which are encountered within the browser when a bot navigates the GUI quicker than a human
- Keep away from the complexity of working with popups, which can be added at any time to the GUI and should have little to do with the bot’s main goal
I’ve been utilizing the bot now for a number of weeks. I’ve discovered that it precisely builds a cart for about 90% of the gadgets on my checklist. I then go in and make some minor tweaks to my purchasing cart by way of the Safeway cellular app, however general, I’ve minimize down the weekly time I spend ordering groceries from about half-hour to five minutes. So, yeah, it really works fairly nicely for me!
I host the bot on AWS Batch for pennies a month and have even created a customized Alexa talent that kicks off the bot. Which means after my Alexa checklist is finished, I can simply say open grocery purchasing bot and the bot does its magic. In a future put up, I’ll dive into how this deployment works, however for now, you possibly can play with it here.
Loved this text? If this bot have been was an easy-to-use service and supported your most popular grocery retailer and sensible speaker, would you pay for it? Please contemplate taking 3 minutes to answer these 3 quick questions
Thanks for studying this put up.
Here’s a checklist of useful assets talked about on this article: