For my new quote site, AtomicQuote, I wanted to add the ability for the site to auto-tweet the most popular quotes throughout the day. I ran into a few issues and wanted to write this post to hopefully help others who want to create an auto tweeting Twitter bot.
The first task was to sign up for a Twitter API account and check their documentation. Make sure you use the account your want to tweet from when you generate your keys. You will need a Key, Secret Key, Token, and Token Secret.
Searching for a Laravel Twitter library was harder than expected. Most of the packages were outdated and would not work with the latest Twitter API version. After some time, I discovered a TwitterOauth that looked like it would work. Make sure you set the API Version to be 2: $connection->setApiVersion(‘2’);
$connection = new TwitterOAuth(config('services.twitter.key'), config('services.twitter.secret'), config('services.twitter.token'), config('services.twitter.tokenSecret')); $connection->setApiVersion('2'); $response = $connection->post('tweets', ['text' => 'Tweet Goes Here'], true);
The final step was to schedule the tweet job to run every hour to tweet the most popular quotes. Fortunately, Laravel makes this simple by allowing you to schedule commands.
Follow the @AtomicQuote Twitter account to see it in action!
Thanks for reading. Make sure you follow me on Twitter to stay up to date on the progress of my side projects T.LY, Weather Extension, and Link Shortener Extension. If you are interested in the tech I use daily, check out my uses page.