Updated to ptb 21.0.1 and added socks5 proxy support
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
FROM python:3.7
|
||||
FROM python:3.11
|
||||
|
||||
# Adding trusting keys to apt for repositories
|
||||
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
|
||||
|
||||
23
bot/bot.py
23
bot/bot.py
@@ -14,7 +14,7 @@ from selenium.webdriver.common.by import By
|
||||
from selenium.webdriver.chrome.options import Options
|
||||
from selenium import webdriver
|
||||
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from telegram.ext import Updater, MessageHandler, Filters, CallbackContext
|
||||
from telegram.ext import ApplicationBuilder, MessageHandler, filters, CallbackContext
|
||||
from amazon_product import AmazonProduct
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ def scrape_data(page_html: str) -> AmazonProduct:
|
||||
return AmazonProduct(title=title, price=price, image=image) if title != "" else None
|
||||
|
||||
|
||||
def find_amazon_link(update: Update, context: CallbackContext) -> None:
|
||||
async def find_amazon_link(update: Update, context: CallbackContext) -> None:
|
||||
try:
|
||||
msg = update.message.text
|
||||
except AttributeError:
|
||||
@@ -131,30 +131,33 @@ def find_amazon_link(update: Update, context: CallbackContext) -> None:
|
||||
|
||||
if not product_data: #if after applying the captcha we don't have any data yet, stop the execution and reply to the user
|
||||
logging.info("Unable to get the product information")
|
||||
context.bot.send_message(chat_id=chat_id, text="Unable to get product attributes from the provided url", reply_to_message_id=message_id)
|
||||
await context.bot.send_message(chat_id=chat_id, text="Unable to get product attributes from the provided url", reply_to_message_id=message_id)
|
||||
return
|
||||
|
||||
logging.info(f"Product information found: {product_data}")
|
||||
|
||||
context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
|
||||
await context.bot.deleteMessage(chat_id=chat_id, message_id=message_id)
|
||||
product_id = dbhelper.check_product(amazon_url_with_referer, product_data.price)
|
||||
if not product_id:
|
||||
product_id = dbhelper.add_product(user['username'], chat['title'], user['id'], chat_id, msg, amazon_url_with_referer, product_data.title, product_data.price, product_data.image)
|
||||
helpers.create_image(product_id, product_data.price)
|
||||
keyboard = [[InlineKeyboardButton("Ir a Amazon", url=f"{amazon_url_with_referer}")]]
|
||||
markup = InlineKeyboardMarkup(keyboard)
|
||||
context.bot.send_photo(chat_id=chat_id, photo=open(f"/app/data/images/products/{product_id}_composed.png", 'rb'), caption=f"URL enviada por @{user['username']}: \n\n{product_data.title}{original_message}", reply_markup=markup)
|
||||
await context.bot.send_photo(chat_id=chat_id, photo=open(f"/app/data/images/products/{product_id}_composed.png", 'rb'), caption=f"URL enviada por @{user['username']}: \n\n{product_data.title}{original_message}", reply_markup=markup)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
dbhelper.setup_db()
|
||||
|
||||
updater = Updater(constants.TELEGRAM_API_KEY)
|
||||
dispatcher = updater.dispatcher
|
||||
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, find_amazon_link))
|
||||
updater.start_polling()
|
||||
updater.idle()
|
||||
if constants.telegram_proxy:
|
||||
logging.info("Creating application with socks5 proxy")
|
||||
application = ApplicationBuilder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_API_KEY).proxy(constants.proxy_url).get_updates_proxy(constants.proxy_url).build()
|
||||
else:
|
||||
logging.info("Creating application without socks5 proxy")
|
||||
application = ApplicationBuilder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_API_KEY).build()
|
||||
|
||||
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, find_amazon_link))
|
||||
application.run_polling()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -8,6 +8,14 @@ HEADERS = ({'User-Agent':
|
||||
'Accept-Language': 'en-US, en;q=0.5'})
|
||||
DB = '/app/data/amazon.db'
|
||||
|
||||
telegram_proxy = os.environ.get("TELEGRAM_PROXY") == '1'
|
||||
if telegram_proxy:
|
||||
telegram_socks_user = os.environ.get("TELEGRAM_SOCKS_USER")
|
||||
telegram_socks_password = os.environ.get("TELEGRAM_SOCKS_PASSWORD")
|
||||
telegram_socks_server = os.environ.get("TELEGRAM_SOCKS_SERVER")
|
||||
telegram_socks_port = os.environ.get("TELEGRAM_SOCKS_PORT")
|
||||
proxy_url = f"socks5://{telegram_socks_user}:{telegram_socks_password}@{telegram_socks_server}:{telegram_socks_port}"
|
||||
|
||||
if baseURL.startswith("https://www."):
|
||||
searchURL = baseURL[12:]
|
||||
elif baseURL.startswith("http://www."):
|
||||
|
||||
@@ -10,6 +10,9 @@ logging.basicConfig(
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
httpx_logger = logging.getLogger('httpx')
|
||||
httpx_logger.setLevel(logging.WARNING)
|
||||
|
||||
def create_image(product_id, price):
|
||||
width = 1280
|
||||
height = 800
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
python-telegram-bot==13.13
|
||||
python-telegram-bot[socks]==21.0.1
|
||||
requests==2.28.1
|
||||
beautifulsoup4==4.11.1
|
||||
lxml==4.9.1
|
||||
|
||||
@@ -7,8 +7,13 @@ services:
|
||||
container_name: bot-amazon-telegram
|
||||
volumes:
|
||||
- ./data:/app/data
|
||||
restart: unless-stopped
|
||||
restart: always
|
||||
environment:
|
||||
- TELEGRAM_API_KEY=${TELEGRAM_API_KEY}
|
||||
- baseURL=${baseURL}
|
||||
- affiliate_tag=${affiliate_tag}
|
||||
- TELEGRAM_PROXY=${TELEGRAM_PROXY}
|
||||
- TELEGRAM_SOCKS_USER=${TELEGRAM_SOCKS_USER}
|
||||
- TELEGRAM_SOCKS_PASSWORD=${TELEGRAM_SOCKS_PASSWORD}
|
||||
- TELEGRAM_SOCKS_SERVER=${TELEGRAM_SOCKS_SERVER}
|
||||
- TELEGRAM_SOCKS_PORT=${TELEGRAM_SOCKS_PORT}
|
||||
|
||||
Reference in New Issue
Block a user