2016-01-26 85 views
0

我正在使用PHP爲BOT配置webhook。電報從外部服務器發送圖像

我想從其他服務器發送圖片。

我已經試過這樣

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); 
} 

但我只接收消息。 問題是什麼? 我試圖改變在http,但問題仍然存在

回答

1

嗯,我已經做了一個解決方法,因爲我的cUrl版本似乎有一個上傳文件的錯誤。

現在我用的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(); 
0

你必須發送一個文件,而不是一個URL。

所以:

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);    # <--- 
} 

這是爲原料例如,不檢查的file_get_contents()成功。

在我的機器人我使用這種模式,它工作正常。