gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Untitled conversation
- User
User: ```oishy.php
- Assistant
User: ```oishy.php
If the previous approach didn't work, here’s an alternative method that uses cURL instead of file_get_contents to send a message to your Telegram bot. This can provide better error handling and support for additional options if you need them later.
/**
* Sends the new post title and URL to a Telegram bot using cURL
*
* @param array $post
* @return void
*/
function send_to_telegram_bot(array $post)
{
$bot_token = 'YOUR_BOT_TOKEN'; // Replace with your Telegram bot token
$chat_id = 'YOUR_CHAT_ID'; // Replace with your chat ID or group ID
$post_url = post_url($post); // Get the URL of the post
$message = "New Post Added:\nTitle: " . $post['post_title'] . "\nURL: " . $post_url;
// Telegram API URL for sending messages
$telegram_api_url = "https://api.telegram.org/bot" . $bot_token . "/sendMessage";
// Data to be sent to the Telegram API
$data = [
'chat_id' => $chat_id,
'text' => $message,
'parse_mode' => 'Markdown' // Optional: Allows formatting options
];
// Initialize cURL
$ch = curl_init($telegram_api_url);
// Set the cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Set to return the response as a string
curl_setopt($ch, CURLOPT_POST, true); // Use POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Set the data to be sent
// Execute the request
$response = curl_exec($ch);
// Check for errors
if(curl_errno($ch)) {
// Log the error or handle it as needed
error_log('Telegram Bot Error: ' . curl_error($ch));
} else {
// Optional: Decode the response to check for success
$result = json_decode($response, true);
if (!$result['ok']) {
// Log the error if the message wasn't sent successfully
error_log('Telegram Bot Message Error: ' . $result['description']);
}
}
// Close the cURL session
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_close($ch);
}
YOUR_BOT_TOKEN with the token from your Telegram bot.YOUR_CHAT_ID with your target chat ID or group ID.extension=curl in php.ini).send_to_telegram_bot function whenever you create a post.parse_mode option allows for Markdown formatting in the message, making it cleaner and more readable.This method should help you successfully integrate notifications to your Telegram bot whenever a new post is added. If you run into any issues, please check your bot token, chat ID, and ensure your server can make outgoing HTTP requests.
Not yet included in a published release.