Friday, March 29, 2019

Retweet, replay and direct message with twitter api in laravel

Retweet, replay and direct message with twitter api in laravel:

First need to install thujohn/twitter package to your laravel app.
Then you need to logged in twitter to get access token.
With that access token you can do those below operations.

To retweet use the below function :

public function reTweet(Request $request)

{

        $tokens = Session::get('access_token');

        Twitter::reconfig($tokens);

        $input = $request->all();
        $postID = (string)$input['postId'];
        $status = $input['message'];

        $postStatus = ['format' => 'json', 'status' => $status];

        $result = Twitter::postRt($postID, $postStatus);
        $result = json_decode($result, true);

        return response()->json($result);
}

To reply use the below function :

public function reply(Request $request)
{
        $tokens = Session::get('access_token');
        Twitter::reconfig($tokens);

        $input = $request->all();
        $postID = (string)$input['id'];
        $status = '@'.$input['screen_name'].' '.$input['message'];

        $parameters = [
            'status' => $status,
            'in_reply_to_status_id' => $postID,
            'format' => 'json'
        ];

        $result = Twitter::postTweet($parameters);
        $result = json_decode($result, true);

        return response()->json(['success' => $result]);
}

To direct message to user use the below function :

public function sendMessage(Request $request)
{
        $tokens = Session::get('access_token');
        Twitter::reconfig($tokens);

        $test = Twitter::postDm([
            'user_id' => (string)$request->id,
            'screen_name' => $request->screen_name,
            'text' => $request->message
        ]);
        return response()->json($test);

}

Switch PHP version in Ubuntu OS or Zorin OS

First check where is installed your PHP by the below command: $ which php See PHP version by below command: $ php -v As I've installed P...