Send Telegram Text with App Events

Description/Instructions

Telegram is an amazing platform to get notifications quickly. In order to do so, follow these instructions:

  • Navigate to the backend of your site and go to Voxel > App Events.
  • Click on the notification you want to have sent via Twilio.
  • Open the accordion and click on “Advanced”.
  • Copy all of that information and paste it into the functions.php file of your Voxel Child Theme. If you do not have a child theme setup, visit this article to learn how.
  • Where it says “// your custom code…” paste the following code and get rid of the “// your custom code…”

How to get a bot token:

  • Go to the @BotFather bot
  • Send the command /token
  • Select the bot you need a token for
  • Connect the bot with IM
  • Copy the token
  • Create a channel on the Telegram IM Module

If this snippet helped, feel free to buy me a taco :)

// Telegram Bot API token and chat ID
$telegram_bot_token = 'YOUR_TELEGRAM_BOT_TOKEN';
$telegram_chat_id = 'YOUR_TELEGRAM_CHAT_ID';

// Post title from the event
$post_title = $event->post->get_title();

// Message content
$message_content = '"' . $post_title . '" website has been submitted.';

// Telegram API URL
$url = "https://api.telegram.org/bot$telegram_bot_token/sendMessage";

// Setup request to send JSON via POST
$data = array(
'chat_id' => $telegram_chat_id,
'text' => $message_content,
);
$post = json_encode($data);

// Set up cURL
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// Send the request and get the response
$response = curl_exec($ch);
$error = curl_error($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Close cURL session handle
curl_close($ch);

// Check for errors and process the response
if ($error) {
error_log('Curl error: ' . $error);
} else {
// Process the response as needed
error_log('Telegram API response: ' . $response);
}

  • PHP
Copy Code

Let's Chat About this Snippet