To send a message through the Telegram API with a blurred part of the text, you can use the MarkdownV2 formatting supported by Telegram. The spoiler
tag creates “blurred” text that reveals when clicked. Here’s a step-by-step guide:
1. Get your bot token
- Create a Telegram bot via BotFather and get your API token.
2. Set up the request to the Telegram API
The message is sent via the sendMessage
method of the Telegram API. Example in PHP:
PHP Code:
<?php
// Your bot's token
$token = 'YOUR_BOT_TOKEN';
// Chat ID (can be found using @userinfobot or Telegram API methods)
$chat_id = 'YOUR_CHAT_ID';
// Message with blurred part
$message = "Hello! This is regular text.\nAnd this is the blurred text: ||secret message||";
// Telegram API URL
$url = "https://api.telegram.org/bot$token/sendMessage";
// Request parameters
$data = [
'chat_id' => $chat_id,
'text' => $message,
'parse_mode' => 'MarkdownV2' // Specify MarkdownV2 for spoiler support
];
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
curl_close($ch);
// Check response
if ($response) {
echo "Message sent: " . $response;
} else {
echo "Sending failed.";
}
3. Handling in WordPress
If needed for WordPress, you can add this function to your theme’s functions.php
file or create a plugin.
Example function:
function send_telegram_message($chat_id, $message) {
$token = 'YOUR_BOT_TOKEN';
$url = "https://api.telegram.org/bot$token/sendMessage";
$data = [
'chat_id' => $chat_id,
'text' => $message,
'parse_mode' => 'MarkdownV2'
];
$response = wp_remote_post($url, [
'body' => $data
]);
return $response;
}
// Example of calling the function
add_action('init', function() {
$chat_id = 'YOUR_CHAT_ID';
$message = "Hello! This is regular text.\nAnd this is the blurred text: ||secret message||";
$response = send_telegram_message($chat_id, $message);
if (is_wp_error($response)) {
error_log('Error sending to Telegram: ' . $response->get_error_message());
} else {
error_log('Message successfully sent.');
}
});
4. Important
- Use
MarkdownV2
for text formatting (make sure to escape special characters like*
,_
,[
,]
,(
,)
etc.). - Ensure that your bot is added to the chat and has the necessary permissions to send messages.
This code works for both pure PHP and WordPress implementations.