Introduction
In this article, I will discuss Telegram bots, where we will create a simple calculator using Python.
Telegram has been a place where many players are developing their applications, such as games, ordering apps, planner apps, etc.
What are Telegram Bots?
Telegram bots are automated programs that operate within the Telegram messaging platform using the Telegram Bot API. They can be created to perform various tasks, interact with users, and integrate with other services and APIs to deliver information or functionalities. Here are some key aspects of Telegram bots:
Key Features of Telegram Bots
- Automation: Bots can automatically respond to user messages, execute commands, and perform tasks without human intervention.
- Interactivity: Bots can engage users through text messages, images, buttons, and inline keyboards, allowing for a conversational user experience.
- Commands: Users can interact with bots using commands (prefixed with a slash, e.g.,
/start
, /help
). Bots can be programmed to recognize and respond to these commands.
- User Input Handling: Bots can collect data from users through prompts and replies, allowing for interactive experiences like surveys and questionnaires.
- Integration: Bots can connect to external APIs and databases, enabling them to fetch data from other sources, send notifications, or trigger external processes.
- Inline Mode: Bots can operate in inline mode, allowing users to request information by typing the bot's username and then directly querying in any chat. The bot can then send a response without the need to initiate a private chat.
- Custom Keyboards: Bots can use custom keyboards to present users with buttons for quick responses, making interactions easier and more intuitive.
- Security and Privacy: Bots can be designed to protect user privacy. For example, they do not have access to users’ personal information unless explicitly shared by the user.
Setting up the environment
Step 1. Start by creating a folder where the bot's code will be stored.
Step 2. Open the code editor and create a new Python file (e.g., bot.py) to write the bot's code.
Step 3. Navigate to Telegram and search for BotFather. Make sure to select one that has a blue tick; others may be spam.
Step 4: Send "/start", to start interacting with BotFather.
Step 5: Post that accept the terms and conditions, as given in the screenshot.
Step 6: Since we are creating our first bot, we need to execute the "/newbot" command and give it a name.
Step 7: We are done giving a name, now we need to provide a username, the username must end with 'bot' or have an underscore. In my case, i choose csharpcor_bot. Once done you will be provided with an API key, it should be kept secure.
Congratulations, an empty bot is created. Now, we will be adding functionality to it.
Development of functionality
Step 1: Go to the folder that we created earlier.
Step 2: Install PythonTelegramBot using the command
pip install python-telegram-bot
This package allows interaction with Telegram's Bot API, enabling the bot to send and receive messages.
Step 3: Paste the following code in the file you created
import telebot
Token = "7915809408:AAEash0ZfZ67tQ5UMG5-8X99vLl9MV885YQ" #Token we got from BotFather
bot = telebot.TeleBot(Tokens) #Supplied the TeleBot instance with the token we got from BotFather
@bot.message_handler(["start]) #Decorators used to let TeleBot instance know which function to execute when "/start" is sent
def start(message):
bot.reply_to(message, "Welcome to CSHARP Bot")
# Function to start the interaction between our code and the Telegram Bot
bot.pooling()
Post which executes the code. You will notice that without the program running, our bot does not reply.
Step 4: Add the following code for /help and /eval. Adding these will complete your calculator.
@bot.message_handler(["help])
def help(message):
bot.reply_to(message, """/start -> Greeting
/help -> will give you the command list
/eval -> Calculator""")
@bot.message_handler(["eval])
def Ceval(message):
try:
msg = eval(message.text.strip())
except Exception as e:
msg = "This can't be evaluated"
bot.reply_to(message,msg)
Our Calculator Telegram Bot is ready. You can create more such bots.
Conclusion
In this article, we saw how easy it is to create a telegram bot.