📜 ⬆️ ⬇️

Motivational autoposting using Twitter API

image
After reading the article Makaveli Simple notification service via Twitter , I decided to do something for twitter.com too.

I will not describe how I came to the conclusion about writing an autoposter (bot) for Twitter, which would be engaged in motivational “posting”. The essence of the service is that any user added to the readers receives an motivational tweet every hour. (Who is interested in Twitter is @MotivatorForYou ).

This is just an example of how you can use twitter api to automate tweet writing.
')
So what was needed:


To work with twitter api, you must first register your application. For this
  1. Go to https://dev.twitter.com/ , click "Create an app"
  2. Fill out the form and create the application.
  3. In order for our script to write tweets, it needs to be switched to the “Read and write” mode. To do this, go to "Settings" and select "Read and write". (Do not forget to save the changes by the button below)
  4. Go back to “Details” and get 4 keys to work with api (they will need to be entered into our script): Consumer key, Consumer secret, Access token, Access token secret (to get the last two, you must click the button “Create my access token” )

Everything. Now we can work with twitter api.

We write a script for posting:

require_once "twitteroauth/twitteroauth.php"; //       api define("CONSUMER_KEY", "< Consumer key>"); define("CONSUMER_SECRET", "< Consumer secret>"); define("OAUTH_TOKEN", "< Access token>"); define("OAUTH_SECRET", "< Access token secret>"); $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET); $content = $connection->get('account/verify_credentials'); $file = file_get_contents(dirname(__FILE__).'/data.txt'); //      $array = explode("\n",$file); $text = $array[mt_rand(0,sizeof($array) - 1)]; $connection->post('statuses/update', array('status' => $text)); //   

That's all. We fill in on the server, we speak krone that sent every hour.

Source: https://habr.com/ru/post/139900/


All Articles