Sending Messages Using Telegram Bot with PHP

  ·   3 min read

In today’s world of automation and rapid communication, integrating Telegram bots into your applications can significantly elevate your capability to send messages or notifications instantly. This article will guide you through the process of creating a simple Telegram bot using PHP, allowing you to send messages programmatically.

Prerequisites

Before we dive into the code, ensure you have the following:

  1. A registered Telegram account.
  2. A basic understanding of PHP.
  3. Access to a web server that can run PHP scripts (e.g., Apache, Nginx).
  4. PHP installed on your server (7.0 or higher recommended).

Step 1: Creating a Telegram Bot

  1. Open Telegram and find the @BotFather.
  2. Start a conversation with BotFather and send the command /newbot.
  3. Follow the prompts to set a name and username for your bot.
  4. Once created, you will receive a unique token for your bot. This token is essential for sending messages.

Step 2: Setting Up the PHP Script

Create a new PHP file on your server, e.g., send_telegram_message.php. In this file, you’ll write the script to send a message.

<?php
// This is your Bot Token from BotFather
$botToken = "YOUR_BOT_TOKEN_HERE";

// The chat ID you want to send the message to
$chatId = "YOUR_CHAT_ID_HERE";

// The message you want to send
$message = "Hello, this is a message from your Telegram bot!";

// URL to send the message using Telegram's Bot API
$url = "https://api.telegram.org/bot$botToken/sendMessage?chat_id=$chatId&text=" . urlencode($message);

// Sending the request to Telegram
$response = file_get_contents($url);

// Display the response for debugging
var_dump($response);
?>

Step 3: Finding Your Chat ID

To send a message, you need to know the chat ID. There are several methods to find your chat ID:

  1. Send any message to your bot and then open this URL in your browser, replacing YOUR_BOT_TOKEN_HERE with your actual bot token:

    https://api.telegram.org/botYOUR_BOT_TOKEN_HERE/getUpdates
    

    The response will contain a JSON object with your chat ID.

  2. Alternatively, you can use the @username of your account if your privacy settings allow it. Just remember, using a chat ID is more reliable.

Step 4: Running the Script

Upload your send_telegram_message.php to your web server and open it in your browser. If everything is set up correctly, the script should execute, and you should receive the message in your Telegram chat.

Considerations

When using Telegram Bots, always keep the following in mind:

  • Avoid exposing your bot token publicly to prevent unauthorized access.
  • Rate limits apply when sending messages. Ensure you handle errors gracefully.
  • You can expand your bot’s functionalities by exploring other methods in the Telegram Bot API documentation.

Conclusion

Integrating a Telegram bot with PHP for message automation can enhance your applications significantly. This simple implementation allows you to send text messages, and you can build upon this by adding more complex features, such as sending images, responding to user messages, and much more.

For more advanced usage, consult the official Telegram Bot API documentation.

Further Reading

Feel free to experiment with the provided script and enhance it further based on your application’s needs!