O
O
object_Object2021-03-25 22:05:40
PHP
object_Object, 2021-03-25 22:05:40

How to download photos from a telegram channel using Madeline?

I am using MadelineProto
I need to get a photo from the latest telegram post
The code I am using is:

<?php
if (!file_exists('madeline.php')) {
    copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php');
}
include 'madeline.php';

$MadelineProto = new \danog\MadelineProto\API('session.madeline');
$MadelineProto->start();

$channel = '@example';
$offset_id = 0;
$limit = 1;

$messages_Messages = $MadelineProto->messages->getHistory(['peer' => $channel, 'offset_id' => $offset_id, 'offset_date' => 0, 'add_offset' => 0, 'limit' => $limit, 'max_id' => 0, 'min_id' => 0, 'hash' => 0 ]);

$message = $messages_Messages['messages'][0];
echo json_encode($message);

$MadelineProto->downloadToFile($message, "/tmp.png");

$message
{"_":"messageMediaPhoto","photo":{"_":"photo","has_stickers":false,"id":5**54901******2695,"access_hash":-82*****8334,"file_reference":{"_":"bytes","bytes":"AkV4QiEAABFvYFze*****QoZFrTBH7fvQ="},"date":1616692497,"sizes":[{"_":"photoStrippedSize","type":"i","bytes":{"_":"bytes","bytes":"ASgdpA8dV\/EU0n*****FFJjTA=="}},{"_":"photoSize","type":"m","location":{"_":"fileLocationToBeDeprecated","volume_id":4****0743,"local_id":44***85},"w":231,"h":320,"size":12760},{"_":"photoSize","type":"x","location":{"_":"fileLocationToBeDeprecated","volume_id":4000****43,"local_id":44***6},"w":502,"h":696,"size":36269}],"dc_id":4}}


Data on the photo comes, but the file is not created, what's the problem?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
object_Object, 2021-03-27
@object_Object

As it turned out, Madeline went a little crazy
. All you had to do was add the __DIR__ constant.
The last line of code:

$MadelineProto->downloadToFile($message,  __DIR__ . "/tmp.png");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question