Setting up a Telegram bot

Configurare noua (How To)

Situatie

Setting up a Telegram bot allows you to send real-time notifications, automate tasks, and integrate with other systems. It’s easy to use, cost-effective, and provides secure communication. Bots are great for things like system alerts, user engagement, and simple automations, all within the familiar Telegram interface.

Solutie

1. Create Your Telegram Bot

To create a new bot on Telegram:

  1. Open the Telegram app.
  2. Search for “BotFather” in the search bar (BotFather is the official Telegram bot that helps you create new bots).
  3. Start a chat with BotFather and type the command /newbot to create a new bot.
  4. Follow the prompts:
    • Choose a name for your bot (e.g., “My Notification Bot”).
    • Choose a username (must end with bot, e.g., MyNotificationBot).
  5. Once created, BotFather will give you a token. It will look something like this:
    123456789:ABC-DEF1234ghIkl-zyx57W2v1u123ew11

    Note: Keep this token safe. It’s your bot’s key for authenticating API requests.

2. Obtain Your Telegram Chat ID

You need your Chat ID to send notifications to the right place. Here’s how to get it:

  1. Start a conversation with the bot you just created (search for it by its username and click “Start”).
  2. Use this URL in your browser (replace <BOT_TOKEN> with the token you received from BotFather):
    https://api.telegram.org/bot<BOT_TOKEN>/getUpdates

    Example:

    https://api.telegram.org/bot123456789:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getUpdates
  3. This will return a JSON response that includes messages sent to your bot. You’re looking for the chat.id field inside the JSON. It will look like this:
    {
    "ok": true,
    "result": [
    {
    "update_id": 123456789,
    "message": {
    "message_id": 1,
    "from": {
    "id": 123456789,
    "is_bot": false,
    "first_name": "Your Name",
    "username": "your_username",
    "language_code": "en"
    },
    "chat": {
    "id": 123456789,
    "first_name": "Your Name",
    "username": "your_username",
    "type": "private"
    },
    "date": 1599526949,
    "text": "Hello"
    }
    }
    ]
    }
  4. The chat.id in this case is 123456789. This is your chat ID, and it’s what you’ll use to send notifications.

3. Set Up Your Application

Edit your app’s configuration file to include the bot token and chat ID:

// Telegram bot settings
telegram_accesstoken=123456789:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
telegram_chatid=123456789
Replace telegram_accesstoken and telegram_chatid with your actual token and chat ID.

4. Test the Setup

  • Run your application. It should now send notifications to your Telegram bot.
  • You can check your bot in Telegram for notifications.

Tip solutie

Permanent

Voteaza

(2 din 6 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?