![]() ![]() 0 */2 * * * php PATH_TO_LARAVEL_FOLDER/artisan twitter:tweet You can use this tool to design your own cron command based on your need. crontab -eĪnd Add the cron command to run it every 2 hours. ![]() Now that we have created the console command, we will put the command on the cron Job to run it on interval of every 2 hours.įor that we log in to our server and do the following. InRandomOrder(): will fetch a random row from the databaseĬompose a tweet with the song details $tweetContent = sprintf(“%s Chords, Lyrics and Video by %s on MusicCafeNepal %s”, $song->getTitle(), $song->getArtistNames(), $song->getUrl()) Īnd with Tweet Facade, tweet the above content Twitter::postTweet() 5. To Fetch a random song Songs::inRandomOrder()->first() Now when someone hits the console command, we will fetch a random song from the database and Tweet that song. Fetch a random song from the database in Laravel To send tweets to the Twitter API, I decided to create a console command on Laravel.Ĭreate a new console command in Laravel: php artisan make:command TwitterĪnd add the code inside the handle() function in app/console/Commands/Twitter.php fileĪlso, change the value of $signature property in the above file to look like this, protected $signature = ‘twitter:tweet’ 4. Create a Tweet command in Laravel console env file in Laravel TWITTER_CONSUMER_KEY=XXXX composer require atymic/twitterĪdd the above keys and credentials to the. Install the package on Laravel by using the following command. Add Package atymic/twitter on Laravelįor this task, we are using a composer package atymic/twitter. ![]() Sign up to and create a new App and get the credentials.įor this, we are using API Key, API Secret Key, Access Token and Access Token Secret. I decided to post a random song from the website every 2 hours on the Twitter account MusicCafeNepal Steps 1. Here’s a step-by-step process on how I accomplished this process. Last weekend I decided to make one of my Twitter accounts a little more active by posting a random article from the website at a random interval of time. ![]()
0 Comments
Leave a Reply. |