如何在PHP中提供Telegram bot方法结果?

时间:2017-12-10 11:15:56

标签: php json telegram telegram-bot php-telegram-bot

我想给这样的电报机器人方法结果:

{"ok":true,"result":{"message_id":13,"from":{"id":415006699,"is_bot":true,"first_name":"Sama","username":"SamaXXXBot"},"chat":{"id":123456789,"first_name":"M\u044f.M\u043d\u0257\u03b9","type":"private"},"date":1512903870,"text":"test"}}

来自此代码:

    <?php

    define('BOT_TOKEN', '415006699:AAEs-xxxx');
    define('ME', 123456); // Admin UID

    function makeHTTPRequest($method, $types = []){
        $url = 'https://api.telegram.org/bot'.BOT_TOKEN.'/'.$method;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $types);
        $res = curl_exec($ch);
        if (curl_error($ch)){
            var_dump(curl_error($ch));
        } else {
            return json_decode($res);
        }
    }

    $sendPhoto = makeHTTPRequest('sendPhoto', [
        'chat_id' => 123456,
        'photo' =>"https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png",
        'caption' => 'New Photo ...!'
    ])->result;

    if ($sendPhoto->ok == true){
        // Photo sent successfully
        var_dump(makeHTTPRequest('sendMessage', [
            'chat_id' => ME,
            'text' => 'Photo send successfully'
        ]));
    } else {
        var_dump(makeHTTPRequest('sendMessage', [
            'chat_id' => ME,
            'text' => 'Error: '.$sendPhoto->error_code."\nDesc: ".$sendPhoto->description
        ]));
    }
?>

如果sendPhoto是&#39; True&#39;向bot管理员发送消息。 但这不正常,问题是什么?

2 个答案:

答案 0 :(得分:0)

$sendPhoto =makeHTTPRequest('sendPhoto', [ 'chat_id' => 123456, 'photo' =>"https://www.google.co.uk/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png", 'caption' => 'New Photo ...!' ]); 
If(isset($sendPhoto->ok){
///Your code is true 
}Else{
//Your code is false
}

答案 1 :(得分:0)

我使用它,这成功;)

 $sendPhoto = makeHTTPRequest('sendPhoto', [
        'chat_id'=>123456,
        'photo'=>"https://my-domain.com/path/to/photo.jpg",
        'caption'=>'photo caption'
    ]);
$sendTrue = $sendPhoto->ok;
if($sendTrue == True){
//bot send true message
} else {
//bot send false message
}}