2 min read

Inspired by automation and using Laravel Zero

Inspired by automation and using Laravel Zero

I recently read a blog post about coders programming themselves out of a job. It listed another article on someone using Node, and mentions automating the boring stuff with Python. Both of these languages aren’t something I use day to day, so they don’t interest me.

However, what did interest me was the fact I could automate a bunch of things I do daily. For instance, when I create a new blog post, it doesn’t get posted to all my socials. It will go to Facebook, LinkedIn, Twitter and Tumblr, but I also have Mastodon and Vero. Fortunately, Mastodon has an API, but Vero does not. One less task is better than zero!

I’m a huge fan of Laravel and use it daily. There’s also a fun project called “Laravel Zero” which is Laravel specifically for use as a command tool. This was a good start for me, and it finally gave me a reason to play around with Laravel Zero.

First off, I had to create an application with Mastodon which was simple enough. I used their API documentation to find out how to interact with the API and using curl requests. My first issue was the fact I couldn’t post unless I was authenticated. How can you do that if your app is CLI based?

They provide a link you need to visit in order to get a one-time authentication code. You can then copy and paste this in to authenticate. This was a good start, but I still had a few issues.

  • The code is one-time. As soon as it’s used, the code can’t be used again.
  • How can I launch the link via my command?
  • How can I grab the value and place it back in so it’s fully automated?

Thankfully, Laravel can use headless Chrome and interact with it through code. I installed console-dusk and tried to install the chromium web driver. Unfortunately, the package was out of date, and would only work with 73. This is a problem because Chrome’s current version is 106, and 73 is no longer marked as stable.

I used the package locally to update the composer.json file which allows me to use the latest Laravel Dusk version. This version can use the current Chromium version which was great. I also pushed it to git and created a pull-request. Thankfully, this got approved and merged (thank you).

I can now interact with the headless browser, post my username and password, and get a code. Oh wait… I have two factor authentication enabled. Unfortunately, I had to disable 2FA for this to work. I’m not too worried since I doubt my account will be hacked, but it would be nice if there was a better workaround.

My email and password is sent through the headless browser, and I can get back a single-use code. This then gets passed in when posting a new status. Currently, it says “Test from Sam”. Sam is what I have decided to call my CLI (named after my brother).

Now for the easiest part. Grab my latest blog post via RSS, and compare it to the latest entity in my database. If they don’t match, then the new post hasn’t been posted yet. This then gets sent to Mastodon as a new status. If the data matches, then nothing is posted, and I get notified it’s the same as before. This is just future thinking in case I get myself an admin or two (unlikely, but who knows?!?).