Send Messages to your Telegram Broadcast through your WP Dashboard

Hello,

we we’re looking for a simple solution and creates this small, handy, tool. To use, unzip the folder, you can download here, and open “telegram-message-sender.php”.

Enter your Bot Token and your Channel-ID with appending @ into the PHP Code.

Either use the code as a functions.php file (f.ex. through Code Snippets) by Copy and Pasting the following Code, or download the .zip file, change the required steps, and upload it zipped through WordPress > Dashboard > Plugins > Add New.

In the Dashboard Menu, the Link “Telegram Message Sender” will appear. Add text, link, thumbnail URL and button text on that page and upon hitting Send, the message will be sent to your public Broadcast or Channel.

scrnli 24 9 2024 18 13 38
Send Messages to your Telegram Broadcast through your WP Dashboard

Please note: This will not automatically submit your Posts to Telegram. You might want to look for the plugin WP-Telegram. Instead, it sends a message to your Channel as your bot.

// Add Admin Menu Page
function telegram_message_sender_menu() {
    add_menu_page(
        'Telegram Message Sender',
        'Telegram Message Sender',
        'manage_options',
        'telegram-message-sender',
        'telegram_message_sender_page',
        'dashicons-email-alt',
        100
    );
}
add_action('admin_menu', 'telegram_message_sender_menu');

// Create the Admin Page
function telegram_message_sender_page() {
    if (isset($_POST['telegram_message'])) {
        $message = sanitize_text_field($_POST['telegram_message']);
        $image_url = sanitize_text_field($_POST['telegram_image_url']);
        $button_text = sanitize_text_field($_POST['telegram_button_text']);
        $button_url = sanitize_text_field($_POST['telegram_button_url']);
        $response = send_telegram_message($message, $image_url, $button_text, $button_url);
        if ($response) {
            echo '<div class="notice notice-success is-dismissible"><p>Message sent successfully!</p></div>';
        } else {
            echo '<div class="notice notice-error is-dismissible"><p>Failed to send message.</p></div>';
        }
    }
    ?>
    <div class="wrap">
        <h1>Send Message to Telegram Channel</h1>
        <form method="post" action="">
            <table class="form-table">
                <tr valign="top">
                    <th scope="row">Message</th>
                    <td><textarea name="telegram_message" rows="5" cols="50" class="large-text"></textarea></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Image URL</th>
                    <td><input type="text" name="telegram_image_url" class="regular-text" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Button Text</th>
                    <td><input type="text" name="telegram_button_text" class="regular-text" /></td>
                </tr>
                <tr valign="top">
                    <th scope="row">Button URL</th>
                    <td><input type="text" name="telegram_button_url" class="regular-text" /></td>
                </tr>
            </table>
            <?php submit_button('Send Message'); ?>
        </form>
    </div>
    <?php
}

// Send Message to Telegram
function send_telegram_message($message, $image_url = '', $button_text = '', $button_url = '') {
    $bot_token = 'YOUR_TOKEN';
    $chat_id = '@YOUR_CHANNEL_ID';
    $url = "https://api.telegram.org/bot$bot_token/sendMessage";

    // Escape special characters for MarkdownV2
    $message = str_replace(
        ['_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'],
        ['\_', '\*', '\[', '\]', '\(', '\)', '\~', '\`', '\>', '\#', '\+', '\-', '\=', '\|', '\{', '\}', '\.', '\!'],
        $message
    );

    $data = [
        'chat_id' => $chat_id,
        'text' => $message,
        'parse_mode' => 'MarkdownV2'
    ];

    if (!empty($image_url)) {
        $data['photo'] = $image_url;
        $data['caption'] = $message;
        $url = "https://api.telegram.org/bot$bot_token/sendPhoto";
    }

    if (!empty($button_text) && !empty($button_url)) {
        $data['reply_markup'] = json_encode([
            'inline_keyboard' => [
                [
                    ['text' => $button_text, 'url' => $button_url]
                ]
            ]
        ]);
    }

    $options = [
        'http' => [
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ],
    ];

    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);

    return $result !== FALSE;
}

Posted

in

by

Tags:

Comments

Leave a Reply

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