How to Build a Telegram Bot to Get Prices with One Click

Build a Telegram Bot to Get Prices

Simplify your shopping experience with a Telegram bot that fetches the latest prices for any item in just one tap!

Introduction

Do you ever find yourself searching for prices on multiple websites ? What if you could get all that information in just one click? Well, now you can!

In this guide, I’ll walk you through building a simple yet effective Telegram bot that fetches real-time prices with just one click. It’s easy to set up and your users will love how quickly they can access price info!

Telegram BotFather
Photo by Lana Codes / Unsplash

Step 1: Set Up Your Telegram Bot

To get started, you need to create a Telegram bot using the BotFather and get the API token.

  1. Open Telegram and search for BotFather.
  2. Start a Chat with BotFather and use the command /newbot .
  3. Follow the instructions to name your bot and get your API token.
  4. Save the token securely; you’ll need it for your Python script.

Explanation: BotFather will generate your API token, which will allow your Python script to interact with Telegram.

Step 2: Install Required Libraries

Next, you need to install the necessary Python libraries with Telegram.

pip install python-telegram-bot requests
  1. python-telegram-bot : This is the library that will help you interact with the Telegram Bot API.
  2. requests : We’ll use this library to fetch real-time price data from websites.
Fetching Bitcoin price Data From a Website
Photo by CardMapr.nl / Unsplash

Step 3: Fetch Price Data from a Website

Now let’s write a Python function that fetches the price of a product from a website.

For this example, we’ll use a placeholder API or a mock data source, but in a real-world application, you can pull this data from any e-commerce website or API.

import requests

def get_price(product_name):
  # You should replace this with a real API call or scraping method
  # For demo purpose , we'll return a fixed price
  price = 95.000 #Bitcoin pice
  return f"The price of {product_name} is ${price}."

Explanation: This function simulates the process of fetching a price for a given product. You could modify it to scrape websites or call APIs to get live data.

Step 4: Handle User Commands in the Bot

Now that you have a function to fetch prices, let’s set up your Telegram bot to handle user commands.

The bot will respond to a command like /getprice followed by a product name.

from telegram.ext import Updater , CommandHandler

def start(update , context):
  update.message.reply_text("Welcome! Send me the product name and I will fetch its price.")
  
def get_price_command(update, context):
  product_name = ' '.join(context.args) # Get product name from the user's message
  if not product_name:
    update.message.reply_text("Please provide the product name.")
    return
  price = get_price(product_name)
  update.message.reply_text(price)

def main():
  # Replace Your_API_Token with the token you got from BotFather
  updater = Updater("Your_API_Token" , use_context=True)
  
  # Register handlers
  updater.dispatcher.add_handler(CommandHandler("Start",start))
  updater.dispatcher.add_handler(CommandHandler("getprice",get_price_command))

  # Start the bot
  updater.start_polling()
  updater.idle()

if __name__ == '__main__':
  main()

Explanation:

  1. start : Welcomes the user and explains how to use the bot.
  2. get_price_command : Handles the /getprice command, fetches the price using the get_price() function and sends the response to the user.

Step 5: Start the Bot and Test It

Once you’ve set up the bot , run your Python script. Your bot will now be live, waiting for users to interact with it.

  1. Open your Telegram bot.
  2. Search for your bot by its name (created via BotFather).
  3. Send the command /getprice [product_name] (e.g., /getprice laptop )
  4. The bot will reply with the price.

Conclusion

Congratulations! You’ve now built a Telegram bot that allows users to get real-time prices with just one click.

Whether you’re helping users compare prices or just offering a quick price-checking service, this bot can add significant value to your Telegram group or channel.

Feel free to expand this bot by connecting it to more APIs or scraping multiple e-commerce sites to get the best prices.

The possibilities are endless!

Optional Extra Tips / Resources

  • Bot Customization: You can enhance the bot by adding more commands for users , such as /help to display the bot’s functionality.
  • Live Data: To fetch real-time prices, you can integrate APIs from platforms like Amazon , eBay or use web scraping tools like beautifulSoup.
  • Bot Styling: Customize the bot’s responses using rich formatting like markdown for bold , italics and liks.

Closing Thoughts

Thanks for reading! If you’ve found this article helpful , feel free to share it with others or leave a comment below. Have fun building your Telegram Bots , and don’t hesitate to reach out if you have any question!

You can fuel my next data science deep dive by buying me a coffee ☕!