A Telegram bot token is a unique identifier that allows your application to communicate with Telegram's API, enabling you to send messages, respond to user inputs, and automate various tasks. This guide will help you create a Telegram bot, obtain the bot token, and set it up in your project.
-
Open Telegram: Make sure you have the Telegram app installed on your device. You can also use the Telegram Web version.
-
Start a Chat with BotFather:
- In the Telegram search bar, type BotFather and select the verified account with a blue checkmark.
- BotFather is the official bot used to manage all Telegram bots.
-
Create a New Bot:
- Type
/newbotand send the command to BotFather. - You will be prompted to enter a name for your bot (e.g.,
Chatz Bot). This is the display name of your bot. - Next, you will be asked to enter a username for your bot. The username must be unique and end with
bot(e.g.,chatz_bot).
- Type
-
Get the Bot Token:
- After creating your bot, BotFather will generate a message containing your new bot’s API token. The token will look something like this:
123456789:ABCDefGhIjKlMnOpQRsTuVwXyZ123456789 - Copy this token, as it will be used to authenticate and connect your bot with the Telegram API.
- After creating your bot, BotFather will generate a message containing your new bot’s API token. The token will look something like this:
-
Set a Profile Picture:
- Send
/setuserpicto BotFather, select your bot, and upload an image file to set as the bot’s profile picture.
- Send
-
Set Commands for Your Bot:
- You can define specific commands that your bot can handle using
/setcommands. - Enter commands in the format:
command1 - Description for command1 command2 - Description for command2
- You can define specific commands that your bot can handle using
-
Enable Privacy Mode:
- By default, bots cannot see messages sent by users unless they are commands starting with
/. Use/setprivacyto adjust this setting.
- By default, bots cannot see messages sent by users unless they are commands starting with
-
Set Up Environment Variables:
- Store the Bot Token in an environment variable to keep it secure. Add the following line to your
~/.chatz.inifile:[default] PROVIDER=telegram TOKEN=123456789:ABCDefGhIjKlMnOpQRsTuVwXyZ123456789 CHAT_ID=123456789
- Store the Bot Token in an environment variable to keep it secure. Add the following line to your
-
Example Configuration in Chatz:
- Make sure your application reads the token correctly from the environment variables and is set up to send messages via Telegram.
-
Start a Chat with Your Bot:
- Search for your bot by its username in Telegram and start a chat.
-
Send a Test Message:
chatz -o "Test message."
- Invalid Token Error: Double-check your bot token and make sure there are no spaces or missing characters.
- Bot Not Responding: Make sure your bot is started and has the correct permissions to read and send messages.
- Incorrect Chat ID: Make sure you are using the correct chat ID when sending messages. You can obtain the chat ID by interacting with your bot and checking the updates received via the Telegram API.
- Keep your Telegram bot token secret. Never expose it in public repositories or share it openly.
- Regenerate the token via BotFather if you suspect it has been compromised.