Telegram Bot Of Amazon Search
As the idea seams a little complex its very simple as we go through the steps.
But first lets see what we are talkin about.
Telegram Bot Of Amazon Search is a boot that returns best 5 products from amazon from the user’s input / search.
The things you need are
- A bot from botfather of telegram.
- Text editor (VS recommended)
- Python installed in your system.
Steps
- Now the first step is to import libraries of python.
import os
import telebot // Used for conecting your python with your telegram boot
import requests // Used to access the request wether its a command or txt
from bs4 import BeautifulSoup // Used for scraping webpage
2. Add the token of your boot
BOT_TOKEN = os.environ.get('YOUR TOKEN')
bot = telebot.TeleBot('YOUR TOKEN')
3. Create a function to display welcome message for new users.
@bot.message_handler(commands=['start', 'hello'])
def send_welcome(message):
bot.reply_to(message, "Hello, How are you doing?
4. Create a function to accept users message and scrap the request from amazons webpage. Then scrap as follows.
@bot.message_handler (content_types = ['text'])
def after_text (message):
url = f'<https://www.amazon.ae/s?k={message.text}>'
response = requests.get(url)
content = response.content
soup = BeautifulSoup(content, 'html.parser')
full_items = soup.find_all('div', {'class': 'a-section a-spacing-base'})
result_item = []
5. Now we got every data we need we just need to filter it to what we need to do that first select all the attributes u need from the product using for loop
NOTE you still must be in the function.
for item in full_items:
try:
item_image = item.find('img', {'data-image-latency':"s-product-image"}).attrs.get('src')
item_model = item.find('span', {'class':"a-size-base-plus a-color-base a-text-normal"}).text
item_price = item.find('span', {'class':"a-offscreen"}).text
item_star = item.find('span', {'class':'a-icon-alt'}).text
except:
item_star = '0.0 out of 5 starts'
item_image = '<https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.vecteezy.com%2Fvector-art%2F500564-add-to-cart-icon-design&psig=AOvVaw2PEXgMBRZGAjFRqD66KyH7&ust=1681988012619000&source=images&cd=vfe&ved=0CBEQjRxqFwoTCNCKnuHjtf4CFQAAAAAdAAAAABAh>'
item_model = ' '
item_price = ' '
item_link_last = item.find('a', {'class':'a-link-normal s-underline-text s-underline-link-text s-link-style a-text-normal'}).attrs.get('href')
item_full_link = '<https://www.amazon.ae/'+item_link_last>
item_full = {
'item_model' : item_model,
'item_image' : item_image,
'item_price' : item_price,
'item_star' : item_star,
'item_link' : item_full_link
}
result_item.append(item_full)
The try and catch is used if some products has no the desired attribute.
7. Finally collect the top data’s in a tuple and display (return) the result to the bot.
for i in range(5):
item_image = result_item[i]['item_image']
item_model = result_item[i]['item_model']
item_price = result_item[i]['item_price']
item_star = result_item[i]['item_star']
item_link = result_item[i]['item_link']
full_detail = "-->Model: "+item_model + "\\n-->Price: " + item_price + "\\n-->Star: "+ item_star + "\\n-->Link: "+item_link
bot.send_photo(message.chat.id, photo=item_image, caption=full_detail, reply_markup=None)
**NOTE you still must be in the function.**
Don’t Forget to call the function.
bot.polling(none_stop = True, interval = 0)
This is out side the function.