Telegram Bot Using Python - Linux Carnival

Telegram Bot Using Python - Linux Carnival

Prerequisite:

Telegram App or Telegram Web

VS Code (Any IDE)

Python Installed Required

The Calculator Telegram bot is a Python-based bot that allows users to perform mathematical calculations within the Telegram messaging platform. Users can interact with the bot by sending mathematical expressions as messages. The bot evaluates the expressions and provides the computed result as a reply.

The bot supports the following features:

Start command: Users can initiate the bot and receive a welcome message.

Help command: Users can request instructions and information about the bot's functionality, including examples of valid mathematical expressions.

Custom messages: The bot handles any other messages sent by users as potential mathematical expressions. It tries to evaluate the expressions and replies with the computed result. If the expression cannot be evaluated, the bot informs the user about the error.

The Calculator Telegram bot simplifies mathematical calculations by providing a convenient interface within the Telegram platform. Users can quickly perform calculations and receive instant results without leaving the Telegram app.

In this blog we will create telegram bot for simple calculations using python script. it is as easy as it will conclude in 2 steps so lets begun!

step 1: Get Telegram Bot Token

1.1. Open the Telegram app and search for "BotFather" in the search bar.

Telegram Bot Using Python - BotFather -Linux Carnival

1.2. Start a chat with BotFather by opening its profile and tapping on the "Start" button.

1.3. In the chat with BotFather, send the command "/newbot" to create a new bot.

1.4. Choose a name for your bot. Make sure it is unique and descriptive.

1.5. Select a unique username for your bot. It must end with the word "bot".

1.6. BotFather will generate a token for your bot. Save this token securely as it provides access to your bot's functionality.

Telegram Bot Using Python - BotToken -Linux Carnival

That's it! You have successfully created a Telegram bot using BotFather. Remember to save the generated token as it will be needed when programming your bot.

Step 2: Programming/Coding For The Telegram Bot

2.1. Install required packages: Install the pyTelegramBotAPI package, which provides a Python wrapper for the Telegram Bot API. Run the following command in the terminal:

pip install pyTelegramBotAPI

2.2. Create a Python script: Create a new Python script file in your project directory, such as bot.py, and open it in VS Code.

2.3. Import the required module: In the bot.py file, import the necessary module:

import telebot

2.4. Set up the bot token: Below the imports, assign your bot token to a variable:

Token = "YOUR_BOT_TOKEN"

2.5. Create the bot instance: Instantiate the telebot.TeleBot class with your token:

bot = telebot.TeleBot(Token)

2.6. Define bot functionality: Write the code to handle different commands and messages for your bot.

@bot.message_handler(['start'])
def start(message):
bot.reply_to(message,"Welecome to calculator")

@bot.message_handler(['help'])
def help(message):
bot.reply_to(message,"The calculator is a Telegram bot that allows users to perform mathematical calculations by entering expressions and provides the evaluated result as a reply.To do that Simply enter mathematical expressions in mesaagebox For example, try sending '2+2' or '3*5'.")

@bot.message_handler()
def custom(message):
try:
msg=eval(message.text.strip())
except Exception as e:
msg="this can't be evaluated"
bot.reply_to(message,msg)

2.7. Start the bot: Add the following code at the end of the bot.py file to start the bot and listen for incoming updates:

bot.polling()

2.8. Run the bot: Save the bot.py file and run it by executing the following command in the terminal:

python bot.py

2.9 At end your final script should look like this

import telebot

Token = "YOUR_BOT_TOKEN"

bot = telebot.TeleBot(Token)

@bot.message_handler(['start'])
def start(message):
bot.reply_to(message,"Welecome to calculator")

@bot.message_handler(['help'])
def help(message):
bot.reply_to(message,"The calculator is a Telegram bot that allows users to perform mathematical calculations by entering expressions and provides the evaluated result as a reply.To do that Simply enter mathematical expressions in mesaagebox For example, try sending '2+2' or '3*5'.")

@bot.message_handler()
def custom(message):
try:
msg=eval(message.text.strip())
except Exception as e:
msg="this can't be evaluated"
bot.reply_to(message,msg)

bot.polling()

That's it! You have implemented your Telegram bot using Python in VS Code. You can expand the functionality of your bot by adding more message handlers or customizing the existing ones to suit your needs

.

Post a Comment

If you have any questions, feel free to ask here!

Previous Post Next Post
Advertising Space