Enhance Your Python Telegram Bot with Custom Keyboard and Buttons
Table of Contents
- Introduction
- Setting Up the Environment
- Creating the Telegram Bot
- 3.1 Importing Packages
- 3.2 Initializing the Bot
- 3.3 Adding the Start Handler
- 3.4 Creating the Reply Keyboard
- Handling User Input
- 4.1 Adding the Message Handler
- 4.2 Filtering for Text Messages
- Performing Actions Based on User Input
- 5.1 Handling Random Person Message
- 5.2 Handling Random Image Message
- 5.3 Sending the Image to the User
- Adding an Inline Keyboard
- 6.1 Creating the Inline Keyboard Buttons
- 6.2 Sending the Message with Inline Keyboard
- Handling Like and Dislike Actions
- 7.1 Adding the Callback Query Handler
- 7.2 Updating the Like and Dislike Count
- Implementing User Restrictions
- 8.1 Defining Unallowed Usernames
- 8.2 Checking User Permissions
- Conclusion
Introduction
In this tutorial, we will learn how to Create a Telegram bot that can send random images or random person images. We will utilize features like reply keyboards, inline keyboards, and user name filtering. The tutorial will guide You through the process step by step, from setting up the environment to implementing the desired functionality.
Setting Up the Environment
Before we can start creating the Telegram bot, we need to make sure that we have the necessary tools and libraries installed. Firstly, we need to have Python installed on our machine. You can download the latest version of Python from their official Website. Additionally, we will need the requests
Package and the Python Telegram Bot library. You can find the required documentation for the library on their website, which offers a wealth of resources to help you learn and utilize it effectively.
Creating the Telegram Bot
To begin, we will obtain the token for our bot. The token is necessary to authenticate our bot and establish a connection with the Telegram API. After obtaining the token, we need to import the required packages and initialize the bot using the token. We will then get the dispatcher from the updater and start pulling updates from Telegram, allowing our bot to receive and process messages.
Adding the Start Handler
To handle user input, we will add a command handler for the /start
command. This handler will execute a function that takes the update
as a parameter, which contains the message sent by the user and information about the sender. Within the function, we will print the username of the sender and the text of the message to ensure that everything is functioning correctly.
Creating the Reply Keyboard
To provide a more user-friendly interface, we will create a reply keyboard that allows the user to select between two buttons: "Random Image" and "Random Person". Each button will trigger a specific action when clicked. We will send the reply keyboard as a message to the bot, specifying the chat ID and the button text. This will prompt the bot to display the keyboard to the user.
Handling User Input
Next, we need to handle the user's selection from the reply keyboard. We will create a message handler that filters for text messages. This handler will execute a function that checks if the received text contains specific keywords. If the text includes "Random Person", the bot will perform an action. Similarly, if the text includes "Random Image", a different action will be performed.
Performing Actions Based on User Input
If the user selects "Random Person", the bot will retrieve an image of a person generated by artificial intelligence. We will provide the URL for the "This Person Does Not Exist" website, fetch the image content, and send it to the user using the send_media_group
function. Similarly, if the user selects "Random Image", the bot will obtain a random image from a specified website and send it to the user.
Adding an Inline Keyboard
In addition to the reply keyboard, we will also implement an inline keyboard for users to express their preferences by liking or disliking the images. We will create an array of buttons and an inline keyboard markup. The buttons will contain emojis and callback data to distinguish between different button clicks. We will then send a message containing the inline keyboard markup and a question asking if the user liked the image.
Handling Like and Dislike Actions
To handle the like and dislike actions, we will add a callback query handler. This handler will execute a function that receives the data associated with the callback query. We will check if the data corresponds to a like or dislike action and send an answer to prevent the callback query from appearing again. Additionally, we will keep track of the number of likes and dislikes and print them.
Implementing User Restrictions
To restrict certain actions to specific users, we will define an array of unallowed usernames. Before performing any action, we will check if the username of the chat is present in the array. If the username is found, the bot will reply with a message stating that the user is not allowed to send messages. The array will be emptied periodically to enable changes in user permissions.
Conclusion
In this tutorial, we have learned how to create a Telegram bot that can send random images and random person images. We have explored various features of the Python Telegram Bot library, such as reply keyboards, inline keyboards, message handlers, and callback query handlers. By following the steps outlined in this tutorial, you can easily implement similar functionality in your Telegram bot and achieve impressive results.
FAQs
Q: What is a Telegram bot?
A: A Telegram bot is an automated program that interacts with users on the Telegram messaging platform. It can perform various tasks and provide information or services based on user input.
Q: Can I use any programming language to create a Telegram bot?
A: Telegram supports the creation of bots using various programming languages. The Python Telegram Bot library used in this tutorial makes it simple and convenient to develop bots using Python.
Q: Do I need to have a server to host my Telegram bot?
A: Yes, in order to run your Telegram bot continuously, you need to host it on a server or a cloud platform. This ensures that your bot remains active and can respond to user requests at any time.
Q: Can I customize the appearance of my bot's interface?
A: Yes, you can customize the appearance of your bot's interface by utilizing features like reply keyboards and inline keyboards. These allow you to create user-friendly interfaces with buttons and interactive elements.
Q: How can I implement additional functionalities in my Telegram bot?
A: The Python Telegram Bot library provides various features and functions that allow you to implement additional functionalities in your bot. You can explore the library's documentation for more information and examples.
Q: Is it possible to integrate external APIs with my Telegram bot?
A: Yes, Telegram bots can interact with external APIs to fetch data or perform specific tasks. The Python programming language offers numerous libraries and frameworks that simplify API integration.
Q: Can I deploy my Telegram bot on a Raspberry Pi?
A: Yes, deploying your Telegram bot on a Raspberry Pi is a popular choice. The Raspberry Pi can be connected to the internet, allowing your bot to run continuously and handle user requests efficiently.