簡體   English   中英

如何給Telegram機器人方法生成PHP?

[英]How can I give Telegram bot methods result in PHP?

我想給電報機器人方法結果如下:

{"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為“ True”,則向機器人管理員發送消息。 但這不能正常工作,這是什么問題?

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

我使用它,這成功了;)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM