Andrea Mason
Andrea Mason

Reputation: 229

Telegram Send image from external server

I'm using PHP to config a webhook for a BOT.

I'd like to send picture from another server.

I've tried this way

function bot1($chatID,$sentText) {
    $botUrl = 'https://api.telegram.org/bot'.self::_BOT_TOKEN_;
    $img = "https://www.server2.com/1.jpeg";
    $this->sendPhoto($botUrl,$chatID,$img);
}

function sendPhoto($botUrl,$chatID, $img){
    $this->sendMessage($botUrl,$chatID,'This is the pic'.$chatID);

    $this->sendPost($botUrl,"sendPhoto",$chatID,"photo",$img);
}
function sendMessage($botUrl,$chatID, $text){
    $inserimento = file_get_contents($botUrl."/sendMessage?chat_id=".$chatID."&text=".$text."&reply_markup=".json_encode(array("hide_keyboard"=>true)));
}

function sendPost($botUrl,$function,$chatID,$type,$doc){

    $response = $botUrl. "/".$function;
    $post_fields = array('chat_id'   => $chatID,
            $type     => new CURLFile(realpath($doc))
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-Type:multipart/form-data"
    ));
    curl_setopt($ch, CURLOPT_URL, $response);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    $output = curl_exec($ch);
}

But I receive only the message. What is the problem? I've tried to change in http but the problem persists

Upvotes: 0

Views: 1997

Answers (2)

Andrea Mason
Andrea Mason

Reputation: 229

Well, I've done a workaround because my cUrl version seems to have a bug in uploading file.

Now I use Zend FW

$botUrl = 'https://api.telegram.org/bot'.self::_BOT_TOKEN_;
$realpath = realpath($doc);
$url = $botUrl . "/sendPhoto?chat_id=" . $chatID;
$client = new Zend_Http_Client();
$client->setUri($url);
$client->setFileUpload($realpath, "photo");
$client->setMethod('POST');
$response = $client->request();

Upvotes: 1

fusion3k
fusion3k

Reputation: 11689

You have to send a file, not a URL.

So:

function bot1( $chatID,$sentText )
{
    $botUrl = 'https://api.telegram.org/bot'.self::_BOT_TOKEN_;
    $img = "https://www.server2.com/1.jpeg";
    $data = file_get_contents( $img );                            # <---
    $filePath = "/Your/Local/FilePath/Here";                      # <---
    file_put_contents( $data, $filePath );                        # <---
    $this->sendPhoto( $botUrl, $chatID, $filePath );              # <---
}

This is as raw example, without checking success of file_get_contents().

In my bot I use this schema, and it works fine.

Upvotes: 0

Related Questions