J7mbo / twitter-api-php

The simplest PHP Wrapper for Twitter API v1.1 calls
MIT License
1.82k stars 802 forks source link

Support for sending direct messages with media #241

Open n-other opened 7 years ago

n-other commented 7 years ago

Unfortunately this API was not usable when coming to the sending of direct messages with media attached.

This process requires 4 twitter api calls: 1) media init 2) media append (upload) 3) media finalize 4) direct message with media_id

It took me some time to develop this code snippet along with a patch expanding the code to allow this type of messages. Here it the code snippet: $settings = array( 'oauth_access_token' => env('TWITTER_ACCESS_TOKEN'), 'oauth_access_token_secret' => env('TWITTER_ACCESS_TOKEN_SECRET'), 'consumer_key' => env('TWITTER_CONSUMER_KEY'), 'consumer_secret' => env('TWITTER_CONSUMER_SECRET') );

            $url = 'https://upload.twitter.com/1.1/media/upload.json';
            $method = 'POST';

            $twitter = new TwitterAPIExchange($settings);

            # init
            $params = array('command' => 'INIT', 'total_bytes' => $filesize, 'media_type' => $mediatype, 'media_category' => 'dm_image');
            $json = $twitter->setPostfields($params)->buildOauth($url, $method)->performRequest();
            $res = json_decode($json);
            $id = $res->media_id_string;

            # upload
            $params = array('command' => 'APPEND', 'media_id' => $id, 'segment_index' => 0, 'media_data' => base64_encode(file_get_contents($file)));
            $json = $twitter->setPostfields($params)->buildOauth($url, $method)->performRequest();

            # finalize
            $params = array('command' => 'FINALIZE', 'media_id' => $id);
            $json = $twitter->setPostfields($params)->buildOauth($url, $method)->performRequest();

            // reset postfields, otherwise it will fill POSTFIELDS from previous call
            $twitter->setPostfields(array());

            # post dm with media
            $url = "https://api.twitter.com/1.1/direct_messages/events/new.json";
            $params = json_encode(array('event' => array('type' => 'message_create', 'message_create' => array('target' => array("recipient_id" => $userid), 'message_data' => array('text' => '', 'attachment' => array('type' => 'media', 'media' => array('id' => $id)))))));  
            $json = $twitter->buildOauth($url, $method)->performRequest(true, array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'), CURLOPT_POSTFIELDS => $params));

zipped patch attached patch.zip

J7mbo commented 7 years ago

Thanks a lot for your PR and many people will appreciate the time you spent - let me give it a good look over when I have some time and then I'll get back to you :)

Optisys67 commented 6 years ago

Hi, i tried to use this patch for sending direct message with quick replies. Unfortunately it only send a simple text message without quick replies. (Remark: it works fine for message with media, thanks!;o)) Here my code for the params:

$params = json_encode(array('event' => array('type' => 'message_create', 'message_create' => array('target' => array('recipient_id' => $userid), 'message_data' => array('text' => 'Test with quick replies'), 'quick_reply' => array('type' => 'options', 'options' => array(array('label' => 'A','description' => 'ADesc','metadata' => 'external_id_1'),array('label' => 'B','description' => 'BDesc','metadata' => 'external_id_2'),array('label' => 'C','description' => 'CDesc','metadata' => 'external_id_3')))))));

Thank you so much for help !

n-other commented 6 years ago

Sorry guys, I don’t have right now available time to check it, I guess quick reply messages require usage of different Twitter API.

On 18January, 2018 at 19:23:10 PM, Optisys67 (notifications@github.com) wrote:

Hi, i tried tu use this patch for sending direct message with quick replies. Unfortunately it only send a simple text message without quick replies. (Remark: it works fine for message with media, thanks!;o)) Here my code for the params:

$params = json_encode(array('event' => array('type' => 'message_create', 'message_create' => array('target' => array("recipient_id" => $userid), 'message_data' => array('text' => '', 'attachment' => array('type' => 'media', 'media' => array('id' => $id)))))));

Thank you so much for help !

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/J7mbo/twitter-api-php/pull/241#issuecomment-358644790, or mute the thread https://github.com/notifications/unsubscribe-auth/AK5dOx89-BW8s8SEkmBZ0C866DoskHWjks5tL0W8gaJpZM4OKc-2 .