Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for sending direct messages with media #241

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open

Conversation

n-other
Copy link

@n-other n-other commented Jun 30, 2017

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
Copy link
Owner

J7mbo commented Jun 30, 2017

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
Copy link

Optisys67 commented Jan 18, 2018

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
Copy link
Author

n-other commented Jan 18, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants