0

I want to give telegram bot methods result like this:

{"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"}}

from this code:

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

And if sendPhoto was 'True' send a message to bot admin. But this not works correctly, What is the problem?

Mhdi
  • 11
  • 5
  • Is there any reason for writing your own bot from the scratch and not use one of the given packages listed on packagist.org? – Nico Haase Dec 10 '17 at 11:19
  • @NicoHaase i'm trying to learn more. – Mhdi Dec 10 '17 at 11:22
  • _"But this not works correctly, What is problem?"_ - What exactly does not do what you want it to do - hence what is the problem? – Jeff Dec 10 '17 at 11:23
  • @Jeff Telegram sends method result from json I want give this method (sendPhoto) result and check that was True bot sends a message to bot admin and if was false send error. In this code sendPhoto is true but bot sends Error: Desc: without any data. How i can give result true in this code? – Mhdi Dec 10 '17 at 11:26

2 Answers2

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
}
0

i use that and this works successful ;)

 $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
}}
Mhdi
  • 11
  • 5