Skip to content

Instantly share code, notes, and snippets.

@NavidVafaeiMajd
Last active February 5, 2026 12:50
Show Gist options
  • Select an option

  • Save NavidVafaeiMajd/f38c2408d6471dc7e48a883ae34a177f to your computer and use it in GitHub Desktop.

Select an option

Save NavidVafaeiMajd/f38c2408d6471dc7e48a883ae34a177f to your computer and use it in GitHub Desktop.
telegram plugin automation
add_action('wp_login', 'send_telegram_on_login', 10, 2);
function send_telegram_on_login($user_login, $user) {
$bot_token = 'your-token';
$chat_id = '1111';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
} else {
$ip = $_SERVER['REMOTE_ADDR'] ?? 'Unknown';
}
$message = "🔐 لاگین جدید به وردپرس\n";
$message .= "👤 نام کاربری: {$user_login}\n";
$message .= "📧 ایمیل: {$user->user_email}\n";
$message .= "🌐 IP: {$ip}\n";
$message .= "🕒 زمان: " . date('Y-m-d H:i:s');
$url = "https://api.telegram.org/bot{$bot_token}/sendMessage";
wp_remote_post($url, [
'body' => [
'chat_id' => $chat_id,
'text' => $message,
]
]);
}
add_action('rest_api_init', function () {
register_rest_route('telegram/v1', '/webhook', [
'methods' => 'POST',
'callback' => 'telegram_blog_bot_handler',
]);
});
function telegram_blog_bot_handler($request) {
$data = $request->get_json_params();
if (!isset($data['message']['text'])) {
return 'ok';
}
$chat_id = $data['message']['chat']['id'];
$text = trim($data['message']['text']);
search_blog_and_send($chat_id, $text);
return 'ok';
}
function telegram_send_message($chat_id, $text) {
$bot_token = 'your-token';
wp_remote_post("https://api.telegram.org/bot{$token}/sendMessage", [
'body' => [
'chat_id' => $chat_id,
'text' => $text,
]
]);
}
function search_blog_and_send($chat_id, $keyword) {
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => 5,
'post_status' => 'publish',
's' => $keyword
]);
if (!$query->have_posts()) {
telegram_send_message(
$chat_id,
"❌ مقاله‌ای با «{$keyword}» پیدا نشد"
);
return;
}
$message = "🔍 نتایج برای «{$keyword}»:\n\n";
while ($query->have_posts()) {
$query->the_post();
$message .= "• " . get_the_title() . "\n";
$message .= get_permalink() . "\n\n";
}
wp_reset_postdata();
telegram_send_message($chat_id, $message);
}
add_action('transition_post_status', 'notify_telegram_on_publish', 10, 3);
function notify_telegram_on_publish($new_status, $old_status, $post) {
if ($new_status !== 'publish' || $old_status === 'publish') {
return;
}
if ($post->post_type !== 'post') {
return;
}
$title = get_the_title($post->ID);
$link = get_permalink($post->ID);
$message = "🆕 مقاله جدید منتشر شد!\n\n";
$message .= "📌 {$title}\n";
$message .= "{$link}";
telegram_send_message_post('@shop_vafaei', $message, get_permalink($post->ID));
}
add_action('post_updated', 'notify_telegram_on_update', 10, 3);
function notify_telegram_on_update($post_ID, $post_after, $post_before) {
if ($post_after->post_type !== 'post') {
return;
}
if ($post_after->post_status !== 'publish') {
return;
}
// جلوگیری از پیام در اولین انتشار
if ($post_before->post_status !== 'publish') {
return;
}
$message = "✏️ مقاله بروزرسانی شد\n\n";
$message .= "📌 " . get_the_title($post_ID) . "\n";
$message .= get_permalink($post_ID);
telegram_send_message_post('@shop_vafaei', $message, get_permalink($post_ID));
}
function telegram_send_message_post($chat_id, $text,$url) {
$bot_token = 'your-token';
$keyboard = [
'inline_keyboard' => [
[
[
'text' => '🔗 مشاهده مقاله',
'url' => $url
]
]
]
];
wp_remote_post(
"https://api.telegram.org/bot{$token}/sendMessage",
[
'timeout' => 15,
'body' => [
'chat_id' => $chat_id,
'text' => $text . "\n\n" . $url,
'reply_markup' => json_encode([
'inline_keyboard' => [
[
['text' => '🔗 مشاهده مقاله', 'url' => 'https://unacclivitously-vulcanisable-zayden.ngrok-free.dev']
]
]
]), ]
]
);
}
add_action('save_post_product', 'notify_telegram_on_product_update', 10, 3);
function notify_telegram_on_product_update($post_id, $post, $update) {
if (!$update) return;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ($post->post_status !== 'publish') return;
$product_title = get_the_title($post_id);
$product_url = get_permalink($post_id);
$text = "🛒 محصول آپدیت شد:\n\n{$product_title}";
telegram_send_message_post(
'@shop_vafaei',
$text,
$product_url
);
}
add_action('wp_footer', function () {
if (is_admin()) return;
$bot_token = 'your-token';
$chat_id = '1111';
$ip = $_SERVER['REMOTE_ADDR'];
$time = current_time('H:i:s');
$message = "👀 سایت باز شد\n⏰ $time\n🌐 IP: $ip";
$url = "https://api.telegram.org/bot$token/sendMessage?chat_id=$chat_id&text=" . urlencode($message);
@file_get_contents($url);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment