├── .gitignore
├── LICENSE.MD
├── README.MD
├── config
└── example_credentials.json
├── main.py
├── requirements.txt
└── utils
├── create_message.py
├── product_amazon.py
└── tools.py
/.gitignore:
--------------------------------------------------------------------------------
1 | __pycache__/
2 | config/credentials.json
3 |
--------------------------------------------------------------------------------
/LICENSE.MD:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 crox-safe
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.MD:
--------------------------------------------------------------------------------
1 | # AmazonBot
2 |
3 | 🤖Fast and simple bot to transform links from Amazon into a nice post with your referral link in Telegram 🛒
4 |
5 | ### Prerequisites
6 |
7 | * You need Python 3.8 or higher
8 |
9 | ```
10 | python3 -m pip install -r requirements.txt
11 | ```
12 |
13 | ### Setup
14 |
15 | In order to setup the bot you need to copy `example_credentials.json` to `credentials.json` in `config/example_credentials.json`.
16 | Then add Bot Token (get it from [@BotFather](https://t.me/Botfather)) and Amazon configuration to the file `credentials.json`.
17 | ### Running
18 |
19 | ```
20 | #If you want just run the bot
21 | python3 main.py
22 |
23 | # Or maybe run forever in to a VPS in linux
24 | nohup python3 main.py &
25 | ```
26 |
27 | ### Customizing the message ✏️
28 |
29 | Now customizing is more easy, you can go to `utils/create_message.py` and edit the variable `message` to put in whatever you want
30 |
31 | 
32 | ### Changelog
33 |
34 | ```
35 | Version 1.2.0
36 | New features:
37 | - Show the percentage if the item is discounted
38 | Updated:
39 | - python-amazon-paapi 3.3.2 --> 4.2.1
40 | - Some functions were updated due to the module upgrade
41 |
42 | Version 1.1.0
43 | New features:
44 | - Remove message from user when post is submitted successfully
45 | - Added update object to utils/create_message to create more personalized message
46 | Updated:
47 | - Updated regex top-domains
48 |
49 | Other:
50 | - Code cleaning
51 |
52 | Version 1.0.0
53 | - Initial Release
54 | ```
55 |
56 | ## Built With
57 |
58 | * [python-amazon-paapi](https://github.com/sergioteula/python-amazon-paapi)
59 | * [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot)
60 |
61 | ## License
62 |
63 | This project is licensed under the MIT License - see the [LICENSE.MD](LICENSE.MD) file for details
64 |
--------------------------------------------------------------------------------
/config/example_credentials.json:
--------------------------------------------------------------------------------
1 | {
2 | "BOT-TOKEN": "YOUR_TOKEN_BOT_HERE",
3 |
4 | "amazon": {
5 | "KEY": "KEY_AMAZON",
6 | "SECRET": "SECRET_KEY_AMAZON",
7 | "TAG": "TAG_AMAZON",
8 | "COUNTRY": "COUNTRY"}
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | import logging
2 | import json # for config
3 | import requests
4 | from telegram import Update
5 | from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
6 | # Amazon Stuff
7 | from amazon_paapi import get_asin # Get asin from url
8 | from utils.create_message import amazon_message # Create HTML template for amazon
9 | from utils.product_amazon import Product
10 | from utils.tools import check_domain
11 |
12 | with open('config/credentials.json') as config_file:
13 | config = json.load(config_file)
14 |
15 | # Enable logging
16 | logging.basicConfig(
17 | format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
18 | )
19 |
20 | logger = logging.getLogger(__name__)
21 |
22 |
23 | def start(update, context):
24 | update.message.reply_text('Send me links from Amazon! I will give you back a nice post.')
25 |
26 |
27 | def message_url(update, context):
28 |
29 | amazon_valid_urls = ['www.amzn.to', 'amzn.to',
30 | 'www.amazon.', 'amazon.']
31 |
32 | url = update.message.text
33 | domain = check_domain(update.message.text)
34 |
35 | if domain.startswith(tuple(amazon_valid_urls)):
36 |
37 | if 'amzn.to/' in domain:
38 | url = requests.get(url).url
39 |
40 | product = Product(get_asin(url))
41 | message = amazon_message(product, update)
42 | context.bot.send_message(update.message.chat_id, message[0] , reply_markup=message[1], parse_mode='HTML')
43 | context.bot.delete_message(update.message.chat_id, update.message.message_id)
44 |
45 |
46 | def main():
47 | updater = Updater(config['BOT-TOKEN'], use_context=True)
48 |
49 | dispatcher = updater.dispatcher
50 |
51 | dispatcher.add_handler(CommandHandler("start", start))
52 |
53 | dispatcher.add_handler(MessageHandler(Filters.regex('(?i)((?:https?://|www\d{0,3}[.])?[a-z0-9.\-]+[.](?:(?:com.br/)|(?:ca/)|(?:com.mx/)|(?:com/)|(?:cn/)|(?:in/)|(?:co.jp/)|(?:sg/)|(?:com.tr/)|(?:ae/)|(?:sa/)|(?:fr/)|(?:de/)|(?:it/)|(?:nl/)|(?:pl/)|(?:es/)|(?:se/)|(?:co.uk/)|(?:com.au/))(?:/[^\s()<>]+[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019])?)'), message_url))
54 |
55 | updater.start_polling()
56 | updater.idle()
57 |
58 | if __name__ == '__main__':
59 | main()
60 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | python-amazon-paapi==4.2.1
2 | amightygirl.paapi5-python-sdk==1.0.0
3 | python-telegram-bot==13.4.1
4 | requests==2.25.1
5 |
--------------------------------------------------------------------------------
/utils/create_message.py:
--------------------------------------------------------------------------------
1 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup
2 |
3 | def amazon_message(product, update):
4 |
5 | first_name = update.message.from_user['first_name']
6 |
7 | if product.get_price().savings != None:
8 | price = f"❌{product.get_price().amount + product.get_price().savings.amount} ✅{product.get_price().amount} {product.get_price().currency} (-{product.get_price().savings.percentage}%) "
9 |
10 | elif product.get_price().amount != None:
11 | price = f"{product.get_price().amount} "
12 |
13 | else:
14 | price = "Not available"
15 |
16 | message = f"""
17 | 📌{product.get_title()}
18 |
19 | 💰Price: {price}
20 |
21 | 🔗Link: Click Here
22 |
23 | 🗣 Post by: {first_name}
24 | """
25 |
26 | buttons = InlineKeyboardMarkup([[InlineKeyboardButton(text='🛒AMAZON🛒', url=product.return_url())]])
27 |
28 | return [message, buttons]
29 |
--------------------------------------------------------------------------------
/utils/product_amazon.py:
--------------------------------------------------------------------------------
1 |
2 | import json
3 | from amazon_paapi import AmazonApi
4 | from .tools import check_domain # To return url with asin
5 |
6 | with open('config/credentials.json') as config_file:
7 | config = json.load(config_file)
8 |
9 | amz_config = config['amazon']
10 | amazon = AmazonApi (amz_config['KEY'], amz_config['SECRET'], amz_config['TAG'], amz_config['COUNTRY'])
11 |
12 |
13 | class Product():
14 |
15 | def __init__(self, asin):
16 | self.asin = asin
17 | self.product = amazon.get_items(asin)[0]
18 |
19 | def get_title(self):
20 | return self.product.item_info.title.display_value
21 |
22 | def get_price(self):
23 | return self.product.offers.listings[0].price
24 |
25 | def get_image(self):
26 | return self.product.images.primary.large.url
27 |
28 | def get_info(self):
29 | return self.product
30 |
31 | def return_url(self):
32 | url = self.product.detail_page_url
33 | return 'https://' + check_domain(url) + 'dp/' + self.asin + '/?tag=' + amz_config['TAG']
34 |
--------------------------------------------------------------------------------
/utils/tools.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 | def check_domain(url):
4 | return re.search('(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9][/]', url).group(0)
5 |
--------------------------------------------------------------------------------