我怎样才能给 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
}}
我想给电报机器人方法这样的结果:
{"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
}}