Added wallamanta code

This commit is contained in:
Joan
2023-03-01 00:39:51 +01:00
parent de5f8aa742
commit a76680147c
10 changed files with 274 additions and 3 deletions

92
wallamanta/alert.py Normal file
View File

@@ -0,0 +1,92 @@
import json
import os
import threading
import logging
from worker import Worker
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
TELEGRAM_CHANNEL_ID = os.getenv("TELEGRAM_CHANNEL_ID")
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
LATITUDE = os.getenv("LATITUDE")
LONGITUDE = os.getenv("LONGITUDE")
SLEEP_TIME = os.getenv("SLEEP_TIME")
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
def parse_json_file():
f = open("args.json")
return json.load(f)
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /help is issued."""
message = "Add with /add product;min_price;max_price"
await update.message.reply_text(message)
async def add_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
parsed = update.message.text.split(";")
logging.info(parsed)
if len(parsed) != 3:
message = "You must put the correct number of arguments: /add product;min_price;max_price"
else:
argument = {"product_name": f"{parsed[0][5:]}", #removes "/add "
"distance": "0",
"latitude": f"{LATITUDE}",
"longitude": f"{LONGITUDE}",
"condition": "all",
"min_price": f"{parsed[1]}",
"max_price": f"{parsed[2]}",
"title_keyword_exclude" : [],
"exclude": []
}
p = threading.Thread(target=Worker.run, args=(argument, ))
p.start()
message = f"Added {parsed[0][5:]}"
await update.message.reply_text(message)
async def remove_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Help!")
async def list_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
args = parse_json_file()
product_list = ""
for product in args:
product_list = f"{product_list}\n{product['product_name']};{product['min_price']}-{product['max_price']}"
await update.message.reply_text(product_list)
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Echo the user message."""
await update.message.reply_text(update.message.text)
def main()->None:
args = parse_json_file()
for argument in args:
logging.info(argument)
p = threading.Thread(target=Worker.run, args=(argument, ))
p.start()
"""Start the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token(TELEGRAM_TOKEN).build()
# on different commands - answer in Telegram
application.add_handler(CommandHandler("help", help_command))
application.add_handler(CommandHandler("add", add_command))
application.add_handler(CommandHandler("remove", remove_command))
application.add_handler(CommandHandler("list", list_command))
# on non command i.e message - echo the message on Telegram
#application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
# Run the bot until the user presses Ctrl-C
application.run_polling()
if __name__ == "__main__":
main()