这段代码在电报中向我的机器人发送信件的最佳程度如何? 是否可以让代码更加优化?
!如何获取聊天ID: https://api.telegram.org/bot/getUpdates
发送到浏览器地址栏: https://api.telegram.org/bot470129572:HFApoAHFVN37852983HDFON385cNOhvs/getUpdates
@return {“确定”:true,“结果”:[{“update_id”:653246792,“消息”:{“message_id”:1,“来自”:{“id”:023752053 !是chatID: 023752053
<?php
$chatID = '023752053';
$token = '470129572:HFApoAHFVN37852983HDFON385cNOhvs';
/**
* Formatting options https://core.telegram.org/bots/api#formatting-options
*/
$message = '';
foreach(json_decode($fd) as $key => $value) {
$message .= '<b>' . $key . '</b>: ' . $value;
$message .= '
';
}
$data = [
'text' => $message,
'chat_id'=>$chatID,
'parse_mode' => 'HTML',
];
/**
* sendMessage https://core.telegram.org/bots/api#sendmessage
*/
$url = "https://api.telegram.org/bot" . $token . "/sendMessage";
$url .= '?'.http_build_query($data);
$ch = curl_init();
$optArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, JSON_PRETTY_PRINT);
if ($result['ok']) {
$result = json_encode(array('success'=>$result['ok']));
}
else {
$result = json_encode(array('error'=>$result));
}
return $result;
我不知道是否可以改进这段代码以通过php向电报机器人发送消息?
你的代码看起来不错!我找不到任何错误。
但是你可以尝试一些小的改变以获得更好的练习:
不要使用 URL 中的查询字符串数据,而是使用 CURLOPT_POSTFIELDS 将数据作为 POST 请求发送。更安全,我认为这是 Telegram API 的首选方法。
其他好主意是为 cURL 创建错误处理以捕获连接问题或 HTTP 错误。
另一件事是,我可以看到你的函数不仅仅只是发送消息,对吧?它正在格式化消息并处理响应。将这些关注点分成单独的功能可能会更易于维护。
我想就是这样。
噢!您必须小心管理机器人令牌的方式。
我可以举个例子:
<?php
function YourTelegramMessageFunction($chatID, $message, $token) {
$url = "https://api.telegram.org/bot" . $token . "/sendMessage";
$data = ['chat_id' => $chatID, 'text' => $message, 'parse_mode' => 'HTML'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// here you can Handle cURL error
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
curl_close($ch);
return json_encode(array('error' => $error_msg));
}
$http_code_message = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code_message >= 200 && $http_code_message < 300) {
$result = json_decode($response, true);
if ($result['ok']) {
$result = json_encode(array('success' => $result['ok']));
} else {
$result = json_encode(array('error' => $result));
}
} else {
// And here you can Handle HTTP error
$result = json_encode(array('error' => 'HTTP error ' . $http_code_message));
}
curl_close($ch);
return $result;
}
$chatID = 'the id'; // change for a correct ID
$token = 'the toke,'; // change for a correct Token
$message = 'the message'; // change with the current message formatting logic
echo YourTelegramMessageFunction($chatID, $message, $token);
$message
字符串可能包含格式错误的消息。逃逸是避免注射的一种方法。
$message .= '<b>' . htmlspecialchars($key) . '</b>: ' . htmlspecialchars($value);
$message .= "\n";
当您使用
cUrl
时,您希望确保成功,如果不能通过检查 HTTP 状态代码或 cUrl 错误进行错误处理。