Step-by-Step Guide to Setting Up a Telegram Bot on Linux

BhGh...2bB9
8 Jan 2024
227

Setting up a Telegram bot on a Linux system can be a straightforward process if you follow these detailed steps. This guide will take you through cloning the necessary repository, configuring your API and User Keys, creating a new bot via BotFather, and obtaining your User ID or Chat ID.

Step 1: Clone the Repository

First, you need to clone the repository that contains the Telegram bot code. Open your Linux terminal and enter the following command:

git clone https://github.com/samsulmaarif/telegram-notify.git 

This command clones the telegram-notify repository onto your machine.

Step 2: Specify the Path for API & User Key

Next, you need to specify the path where your API and User Keys are stored. Run the following command in your terminal:

sed -i "s|/path/to/telegram-notify.conf|/root/telegram-notify/telegram-notify.conf|g" /root/telegram-notify/send-message 

This command sets the correct path for the telegram-notify.conf file in your send-message script.

Step 3: Create a New Bot via BotFather

To create a new Telegram bot, follow these steps:

  1. Open Telegram and search for "BotFather".
  2. Start a chat with BotFather and send the /newbot command.
  3. Follow the instructions to create a bot. Make sure your bot's username ends with Bot.
  4. BotFather will provide you with a token to access the HTTP API. This is your API key.

Once you have your API key, update the telegram-notify.conf file with this key:

sed -i "s|api-key=Your-bot-telegram-api-key|api-key=YourActualApiKey|g" /root/telegram-notify/telegram-notify.conf 

Replace YourActualApiKey with the API key given by BotFather.

Step 4: Obtaining Your User ID or Chat ID

To find your User ID or Chat ID:

  1. In Telegram, search for "RawDataBot".
  2. Start a chat with this bot to initiate it.
  3. Look for the id under the chat section - this is your User ID. Alternatively, create a group and repeat the process to get a group ID.

Update your configuration file with this ID:

sed -i "s|user-id=chat-id-or-Channel-id|user-id=YourChatIdHere|g" /root/telegram-notify/telegram-notify.conf 

Replace YourChatIdHere with the ID you obtained.

Example Command for Sending Messages

To send messages through your bot, use the following command format:

bash /root/telegram-notify/send-message --text YourMessageHere --icon EmojiCode 

Replace YourMessageHere with your desired message and EmojiCode with the appropriate emoji code.

Real life Example

I have setup this bot while I was mining Chia. I have created a cron job to check if the harvesters up and running. When the script run by cron job detects the harvester is not running, it was sending me a message to notify me through Telegram. Use your imagination to integrate this bot to your projects :)

FAQ: Setting Up a Telegram Bot on Linux

Q1: Do I need any specific Linux version to set up the Telegram bot?

A1: No specific version is required. However, you should have a stable and relatively recent version of Linux to ensure compatibility and security. Most common distributions like Ubuntu, Fedora, or Debian should work fine.

Q2: What is the purpose of the telegram-notify repository?

A2: The telegram-notify repository contains scripts and configuration files necessary for setting up and managing a Telegram bot on Linux. It simplifies the process of sending notifications or messages through a Telegram bot.

Q3: How do I obtain an API key for my Telegram bot?

A3: You can obtain an API key by creating a bot through the BotFather in Telegram. After sending the /newbot command and following the prompts, BotFather will provide you with the API key.

Q4: Is it safe to share my Telegram bot's API key?

A4: No, you should not share your bot's API key. This key is used to control your bot and should be kept confidential to prevent unauthorized access.

Q5: How can I find my User ID or Chat ID in Telegram?

A5: You can find your User ID or Chat ID by initiating a chat with the 'RawDataBot' on Telegram. This bot will provide you with your ID information, which you can use in your bot's configuration.

Q6: Can I use this setup for a group chat or channel on Telegram?

A6: Yes, you can obtain a group chat or channel ID in a similar manner to your User ID and configure the bot to send messages to these chats or channels.

Q7: What programming language is used in the telegram-notify scripts?

A7: The scripts in the telegram-notify repository are primarily written in Bash, which is commonly used for scripting on Linux systems.

Q8: Can I customize the messages sent by the Telegram bot?

A8: Yes, you can customize the messages by modifying the command used to send messages. You can change the text and emoji codes as needed.

Q9: What should I do if I encounter errors during the setup process?

A9: Check for common issues such as incorrect paths, missing files, or syntax errors in commands. If the problem persists, consider seeking help from online forums or communities related to Linux and Telegram bots.

Q10: Is there any limit to the number of messages my Telegram bot can send?

A10: Telegram may impose limits to prevent spam, but under normal usage for notifications or alerts, you are unlikely to hit these limits. However, it's important to use the bot responsibly to avoid being flagged for spamming.
Please comment below if you have any questions.
Have fun!

Get fast shipping, movies & more with Amazon Prime

Start free trial

Enjoy this blog? Subscribe to tfntrn

6 Comments