Hi everybody.
I need to upload some images and attach them to some messages (Direct Message), I can upload them and a attach them when the user interacts with my webhook, but this time I need set them as a welcome message (I already know and I can create those welcome messages).
But I am stuck with something about the media api, a need upload the images defining them as shared, according with the docs (https://developer.twitter.com/en/docs/direct-messages/message-attachments/guides/attaching-media) you must set to true the property shared while you are initializing the transfer by uploading chunk media, but I get the following error:
{“request”:"/1.1/media/upload.json",“error”:“Invalid shared: Some(1).”}
As I said previously I already can attach the images but only when I upload them and they are not defined as shared and I just can attach them within the next 24 hours.
I am using the Abraham’s twitteroauth (https://packagist.org/packages/abraham/twitteroauth) version: 0.6.2 and I already tried with the version 0.7.4 but the result was the same.
When I upload without the shared property it expires after 86400 seconds (this is the output)
stdClass Object
(
[media_id] => XXXXXXX
[media_id_string] => XXXXXXX
[media_key] => 5_984824252061777920
[size] => 68805
[expires_after_secs] => 86400
[image] => stdClass Object
(
[image_type] => image/jpeg
[w] => 952
[h] => 488
)
)
In fact I think the library does not consider upload the medias as shared so I have modified to send this property:
In the class: /abraham/twitteroauth/src/TwitterOAuth.php
in the method: uploadMediaChunked($path, $parameters) Line 263
I added these lines:
private function uploadMediaChunked($path, $parameters)
{
// ===================================================
// lines added
$data = [
‘command’ => ‘INIT’,
‘media_type’ => $parameters[‘media_type’],
‘total_bytes’ => filesize($parameters[‘media’])
];
if(isset($parameters[‘shared’])){
$data[‘shared’] = $parameters[‘shared’];
}
if(isset($parameters[‘media_category’])){
$data[‘media_category’] = $parameters[‘media_category’];
}
// lines added
// ===================================================
// I have changed the array defined directly in the function call (third param) for the $data variable in which I add additional parametrs
// Init
$init = $this->http('POST', self::UPLOAD_HOST, $path, $data);
// ===================================================
// And these
if(!isset($init->media_id_string)){
return $init;
}
// ===================================================
// Append
$segment_index = 0;
$media = fopen($parameters['media'], 'rb');
while (!feof($media))
{
$this->http('POST', self::UPLOAD_HOST, 'media/upload', [
'command' => 'APPEND',
'media_id' => $init->media_id_string,
'segment_index' => $segment_index++,
'media_data' => base64_encode(fread($media, self::UPLOAD_CHUNK))
]);
}
fclose($media);
// Finalize
$finalize = $this->http('POST', self::UPLOAD_HOST, 'media/upload', [
'command' => 'FINALIZE',
'media_id' => $init->media_id_string
]);
return $finalize;
}
Is the changes to this code valid?
Am I right with this change according to the docs?
Do I have to change something else or keep that method a is and change another thing?
I hope this information is clear.
Thanks for you help and time.
regards.
EDIT: I have forgotten the error output:
stdClass Object
(
[request] => /1.1/media/upload.json
[error] => Invalid shared: Some(1).
)