I want to auto-post all posts under a certain custom post type ‘escort’ one-by-one (orderly or randomly) every x-time to my Telegram channel.
For orderly auto-posting, loop.
There’re many WordPress plugins out there that offer auto-posting to Telegram, but NOT even the premium ones can do this.
Since no plugin could meet my needs, I decided to do it myself. But I ran into a serious trouble…
I added a menu item to WP Admin menu to be sure that the code captures the post links from the intended custom post type ‘escort’ correctly, which it does.
But the real trouble now is how to post each of these links to my bot on Telegram to post on my channel (post 1 link every 5mins). I’ve edited the code to try different posting methods but the bot isn’t getting it.
I know that my bot works because I have tested it with plugins like WP Telegram and Blog2social. I’ve also tested it with URL commands like: https://api.telegram.org/bot/sendMessage?chat_id=@&text=some_message and it works.
So, I’m almost certain that my bot is not getting the link from my code, which means my code isn’t posting the link to the bot, I guess.
Please someone should help correct my code.
Here are two variations of my custom plugin code:
<?php
/**
* Plugin Name: SFS Auto Post to Telegram
* Plugin URI: https://example.com/auto-post-to-telegram
* Description: Automatically posts links from the "escort" custom post type to a Telegram channel at regular intervals.
* Version: 1.0.0
* Author: Godman Andrew
* Author URI: https://example.com
*/
// Add a custom menu page to display the post links
function sfs_auto_post_to_telegram_menu_page() {
add_menu_page(
'Post Links',
'Post Links',
'manage_options',
'sfs_auto_post_to_telegram_links',
'sfs_auto_post_to_telegram_links_callback'
);
}
add_action('admin_menu', 'sfs_auto_post_to_telegram_menu_page');
// Callback function to output the post links on the custom menu page
function sfs_auto_post_to_telegram_links_callback() {
// Query all posts of the custom post type "escort"
$args = array(
'post_type' => 'escort',
'posts_per_page' => -1,
);
$loop = new WP_Query($args);
// Create an array to store the post links
$postLinks = array();
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$postLink = get_permalink();
$postLinks[] = $postLink;
}
}
// Close the loop
wp_reset_postdata();
// Output the post links on the page
echo '<div class="wrap">';
echo '<h1>Post Links</h1>';
if (!empty($postLinks)) {
echo '<ul>';
foreach ($postLinks as $link) {
echo '<li>' . $link . '</li>';
}
echo '</ul>';
} else {
echo '<p>No post links found.</p>';
}
echo '</div>';
}
// Set up the Telegram bot API credentials
$telegramBotToken = '6099757413:AAERb-CCDsrhoyXKZTgSYs7fjiWCuY9Hps0';
$telegramChannel = '@ExampleXYZT';
// Register a custom cron schedule interval
function sfs_auto_post_to_telegram_register_cron_interval($schedules) {
$schedules['sfs_5_minutes'] = array(
'interval' => 300,
'display' => __('Every 5 minutes'),
);
return $schedules;
}
add_filter('cron_schedules', 'sfs_auto_post_to_telegram_register_cron_interval');
// Schedule the Telegram posting event
function sfs_auto_post_to_telegram_schedule_event() {
if (!wp_next_scheduled('sfs_auto_post_to_telegram_event')) {
wp_schedule_event(time(), 'sfs_5_minutes', 'sfs_auto_post_to_telegram_event');
}
}
register_activation_hook(__FILE__, 'sfs_auto_post_to_telegram_schedule_event');
// Unschedule the Telegram posting event
function sfs_auto_post_to_telegram_unschedule_event() {
wp_clear_scheduled_hook('sfs_auto_post_to_telegram_event');
}
register_deactivation_hook(__FILE__, 'sfs_auto_post_to_telegram_unschedule_event');
// Telegram posting event callback
function sfs_auto_post_to_telegram_event_callback() {
// Include the necessary WordPress files
// Check if WordPress is loaded, and if not, load the WordPress environment
if ( ! defined( 'ABSPATH' ) ) {
/** Set up WordPress environment */
require_once( dirname( __FILE__ ) . '/../../../wp-load.php' );
}
// Query all posts of the custom post type "escort"
$args = array(
'post_type' => 'escort',
'posts_per_page' => -1,
);
$loop = new WP_Query($args);
// Create an array to store the post links
$postLinks = array();
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$postLink = get_permalink();
$postLinks[] = $postLink;
}
}
// Close the loop
wp_reset_postdata();
// Get the current link index from options
$currentLinkIndex = get_option('sfs_auto_post_to_telegram_current_link_index', 0);
// Get the current post link
$currentLink = $postLinks[$currentLinkIndex];
// Send the current post link to Telegram using the bot API
$telegramEndpoint = 'https://api.telegram.org/bot' . $telegramBotToken . '/sendMessage';
$telegramParams = array(
'chat_id' => $telegramChannel,
'text' => $currentLink,
);
$telegramResponse = wp_remote_post(
$telegramEndpoint,
array(
'body' => $telegramParams,
)
);
// Move to the next post link
$currentLinkIndex++;
if ($currentLinkIndex >= count($postLinks)) {
$currentLinkIndex = 0;
}
// Update the current link index in options
update_option('sfs_auto_post_to_telegram_current_link_index', $currentLinkIndex);
}
add_action('sfs_auto_post_to_telegram_event', 'sfs_auto_post_to_telegram_event_callback');
// Delete the current link index option on plugin uninstallation
function sfs_auto_post_to_telegram_uninstall() {
delete_option('sfs_auto_post_to_telegram_current_link_index');
}
register_uninstall_hook(__FILE__, 'sfs_auto_post_to_telegram_uninstall');
THE SECOND VARIATION
<?php
/**
* Plugin Name: SFS Auto Post to Telegram
* Plugin URI: https://example.com/auto-post-to-telegram
* Description: Automatically posts links from the "escort" custom post type to a Telegram channel at regular intervals.
* Version: 1.0.0
* Author: Godman Andrew
* Author URI: https://example.com
*/
// Add a custom menu page to display the post links
function sfs_auto_post_to_telegram_menu_page() {
add_menu_page(
'Post Links',
'Post Links',
'manage_options',
'sfs_auto_post_to_telegram_links',
'sfs_auto_post_to_telegram_links_callback'
);
}
add_action('admin_menu', 'sfs_auto_post_to_telegram_menu_page');
// Callback function to output the post links on the custom menu page
function sfs_auto_post_to_telegram_links_callback() {
// Query all posts of the custom post type "escort"
$args = array(
'post_type' => 'escort',
'posts_per_page' => -1,
);
$loop = new WP_Query($args);
// Create an array to store the post links
$postLinks = array();
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$postLink = get_permalink();
$postLinks[] = $postLink;
}
}
// Close the loop
wp_reset_postdata();
// Output the post links on the page
echo '<div class="wrap">';
echo '<h1>Post Links</h1>';
if (!empty($postLinks)) {
echo '<ul>';
foreach ($postLinks as $link) {
echo '<li>' . $link . '</li>';
}
echo '</ul>';
} else {
echo '<p>No post links found.</p>';
}
echo '</div>';
}
// Set up the Telegram bot API credentials
$apiToken = '6025671013:AAERb-CCDQ06VjfsthkSYs7BtmWCuY9Hps0';
$chat_id = '-1036853733382';
// Register a custom cron schedule interval
function sfs_auto_post_to_telegram_register_cron_interval($schedules) {
$schedules['sfs_5_minutes'] = array(
'interval' => 300,
'display' => __('Every 5 minutes'),
);
return $schedules;
}
add_filter('cron_schedules', 'sfs_auto_post_to_telegram_register_cron_interval');
// Schedule the Telegram posting event
function sfs_auto_post_to_telegram_schedule_event() {
if (!wp_next_scheduled('sfs_auto_post_to_telegram_event')) {
wp_schedule_event(time(), 'sfs_5_minutes', 'sfs_auto_post_to_telegram_event');
}
}
register_activation_hook(__FILE__, 'sfs_auto_post_to_telegram_schedule_event');
// Unschedule the Telegram posting event
function sfs_auto_post_to_telegram_unschedule_event() {
wp_clear_scheduled_hook('sfs_auto_post_to_telegram_event');
}
register_deactivation_hook(__FILE__, 'sfs_auto_post_to_telegram_unschedule_event');
// Telegram posting event callback
function sfs_auto_post_to_telegram_event_callback() {
// Include the necessary WordPress files
// Check if WordPress is loaded, and if not, load the WordPress environment
if (!defined('ABSPATH')) {
/** Set up WordPress environment */
require_once(dirname(__FILE__) . '/../../../wp-load.php');
}
global $apiToken, $chat_id; // Make $apiToken and $chat_id accessible
// Query all posts of the custom post type "escort"
$args = array(
'post_type' => 'escort',
'posts_per_page' => -1,
);
$loop = new WP_Query($args);
// Create an array to store the post links
$postLinks = array();
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$postLink = get_permalink();
$postLinks[] = $postLink;
}
}
// Close the loop
wp_reset_postdata();
// Get the current link index from options
$currentLinkIndex = get_option('sfs_auto_post_to_telegram_current_link_index', 0);
// Get the current post link
$currentLink = $postLinks[$currentLinkIndex];
// Prepare the message to be sent to Telegram
$message = sprintf('<a href="%s">%s</a>', $currentLink, $currentLink);
// Send the message to Telegram using the bot API
$telegramEndpoint = 'https://api.telegram.org/bot' . $apiToken . '/sendMessage';
$telegramParams = array(
'chat_id' => $chat_id,
'text' => $message,
'parse_mode' => 'HTML',
);
$args = array(
'method' => 'POST',
'timeout' => 45,
'headers' => array(
'Content-Type: application/json',
),
'body' => json_encode($telegramParams),
);
$telegramResponse = wp_remote_post($telegramEndpoint, $args);
// Move to the next post link
$currentLinkIndex++;
if ($currentLinkIndex >= count($postLinks)) {
$currentLinkIndex = 0;
}
// Update the current link index in options
update_option('sfs_auto_post_to_telegram_current_link_index', $currentLinkIndex);
}
add_action('sfs_auto_post_to_telegram_event', 'sfs_auto_post_to_telegram_event_callback');
// Delete the current link index option on plugin uninstallation
function sfs_auto_post_to_telegram_uninstall() {
delete_option('sfs_auto_post_to_telegram_current_link_index');
}
register_uninstall_hook(__FILE__, 'sfs_auto_post_to_telegram_uninstall');