v3.8 or higher
(Tested with 3.11.5
)API Token
.channel ID
from the channel settings.pip install -r requirements.txt
.connection string
from the MongoDB dashboard.Clone the repository:
git clone https://github.com/alexjamesmx/e-commerce-offers-telegram-bot.git
Change the directory and install the required libraries:
cd e-commerce-offers-telegram-bot
pip install -r requirements.txt
Define the /consts.py
file with your values. Use a secret file, environment variables, or another method to store your secrets.
Deploy the bot.
You can deploy it on any cloud or self-hosted service. If deploying on Heroku, refer to the /Procfile
. Learn more about Heroku here. Otherwise, remove the Procfile as it is not useful for other deployment methods.
Run the bot:
python bot.py
Ensure you read the next section to know how to get the cookie and CSRF token.
# Telegram bot token
API_TOKEN = YOUR_API_TOKEN
# Telegram channel ID
CHANNEL_ID = YOUR_CHANNEL_ID
# Mercado Libre affiliation ID
AFFILIATE_ID = YOUR_AFFILIATE_ID
# MongoDB configuration
DB_CONFIG_MONGO = {
'uri': YOUR_DB_CONFIG_MONGO['uri'], # connection string
'database': YOUR_DB_CONFIG_MONGO['database'] # name of the database
}
# Mercado Libre cookie
COOKIE = YOUR_COOKIE
# CSRF token
X_CSRF_TOKEN = YOUR_X_CSRF_TOKEN
# User agent
USER_AGENT = YOUR_USER_AGENT
Mercado libre opens their API to fetch products. These are the endpoints I use:
Getting the categories: https://api.mercadolibre.com/sites/MLM/categories
Getting the products from a category: https://api.mercadolibre.com/sites/SITE/search?category=CATEGORY_ID&attributes=id,title,price,currency_id,original_price,permalink
MLM is the site for Mexico, you can change it to the site you want to get offers from.
See documentation here: https://developers.mercadolibre.com.mx/es_ar
Sadly, there is no public endpoint for generating affiliation links, so I had to use the browser developer tool to get the cookie and csrf token. This step is not necessary if you don't want to use affiliation links and benefit from the potential commissions.
Go to the Mercado libre product generation link: https://www.mercadolibre.com.mx/afiliados/linkbuilder
Open the browser developer tool, go to the network tab and filter by Fetch/XHR requests.
Click on the "Generar enlace" button, you'll see a request called "createUrls", click on it and go to the request headers.
You'll see the cookie and csrf token in the request headers.
Copy the cookie and csrf token and paste them in the /consts.py
file.
For the user agent, you can use the one in the request headers.
Feel free to contribute to this project, I'll be happy to review your pull requests. Hope you enjoy it! and star the repo ⭐ if you like it.
This project is licensed under the MIT License - see the LICENSE file for details. In a nutshell, you can do whatever you want with the code.