Notification for Telegram Shortcode

Below, you’ll find a detailed guide on how to add the Notification for Telegram Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the Notification for Telegram Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the Notification for Telegram Plugin and the shortcodes it provides:

Plugin Icon
Notification for Telegram

"Notification for Telegram is a WordPress plugin that enables seamless integration with Telegram, allowing automatic notifications to be sent directly to your Telegram account."

★★★★☆ (60) Active Installs: 2000+ Tested with: 6.2.3 PHP Version: 5.4
Included Shortcodes:
  • [telegram_mess]

Notification for Telegram [telegram_mess] Shortcode

The Notification-for-Telegram plugin shortcode, ‘telegram_mess’, enables users to send customized messages to specified Telegram chat IDs. The shortcode retrieves the token and chat IDs from the plugin configuration. It allows for optional inclusion of the site name, user IP, and city in the message. Messages are sent via Telegram’s API.

Shortcode: [telegram_mess]

Parameters

Here is a list of all possible telegram_mess shortcode parameters and attributes:

  • token – The unique Telegram Bot token for sending messages.
  • chatids – The IDs of the Telegram chats where messages will be sent.
  • message – The content of the message to be sent to the Telegram chats.
  • showip – If set to 1, the sender’s IP address is included in the message.
  • showcity – If set to 1, the sender’s city is included in the message.
  • showsitename – If set to 1, the name of the website is included in the message.

Examples and Usage

Basic example – In this example, the shortcode is used to send a simple message via Telegram. The ‘token’ and ‘chatids’ parameters are automatically retrieved from the plugin configuration.

[telegram_mess message="Hello, World!" /]

Advanced examples

In this example, the shortcode is used to send a message with the site name appended. The ‘showsitename’ parameter is set to ‘1’, which will append the site name to the message. The ‘token’ and ‘chatids’ parameters are automatically retrieved from the plugin configuration.

[telegram_mess message="Hello, World!" showsitename="1" /]

In this next advanced example, the shortcode is used to send a message with the user’s IP and city appended. The ‘showip’ and ‘showcity’ parameters are set to ‘1’, which will append the user’s IP and city to the message. The ‘token’ and ‘chatids’ parameters are automatically retrieved from the plugin configuration.

[telegram_mess message="Hello, World!" showip="1" showcity="1" /]

Finally, in this advanced example, the shortcode is used to send a message to multiple chat ids. The ‘chatids’ parameter is set to a comma-separated list of chat ids. The ‘token’ parameter is automatically retrieved from the plugin configuration.

[telegram_mess message="Hello, World!" chatids="123456,789012" /]

PHP Function Code

In case you have difficulties debugging what causing issues with [telegram_mess] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'telegram_mess', 'nftb_telegram_mess' );

Shortcode PHP function:

function nftb_telegram_mess($atts) {
 
 	$TelegramNotify = new nftb_TelegramNotify();
	$token =  $TelegramNotify->getValuefromconfig('token_0');
	$chatids_ = $TelegramNotify->getValuefromconfig('chatids_');
	$apiToken = $token ;
	// $blog_title = get_the_title( $post_id );	
	$bloginfo = get_bloginfo( 'name' );
 
 
 //options default
 $a = shortcode_atts( array(
 'token' => $token ,
 'chatids' => $chatids_,
 'message' => 'no message',
 'showip' => '0',
 'showcity' => '0',
 'showsitename'=> '0'
  ), $atts );
 
 $newtoken = $a['token'];
 $newmessage = $a['message'];
 
 if ($a['showsitename'] == "1") { 
 $newmessage = $newmessage." - Message from  ".$bloginfo;
 }
  if ($a['showip'] == "1") { 
 $newmessage = $newmessage. " ,IP: ".nftb_get_the_user_ip();
 }
 
  if ($a['showcity'] == "1") { 
  $userip = nftb_get_the_user_ip();
//  $details = json_decode(wp_remote_get("http://ipinfo.io/{$userip}/json"));
  
  
  $newmessage =  $newmessage .nftb_ip_info($userip);
  
 }
 
$users=explode(",",$a['chatids']);
	foreach ($users as $user)
		{
    	if (empty($user)) continue;
    	$data = [
        'chat_id' => $user,
        'text' => $newmessage];
        
    
        	//$response = @file_get_contents("https://api.telegram.org/bot$newtoken/sendMessage?" . http_build_query($data) );
	 		$response = wp_remote_get( "https://api.telegram.org/bot$apiToken/sendMessage?" . http_build_query($data), array( 'timeout' => 120, 'httpversion' => '1.1' ) );
	 					
		}
  }

Code file location:

notification-for-telegram/notification-for-telegram/include/tnfunction.php

Conclusion

Now that you’ve learned how to embed the Notification for Telegram Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *