From deb54e4c492deb76d8eef293cda339e778754cfa Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 24 Jun 2024 19:13:14 +0200 Subject: [PATCH] feat: added additional commands for cats and dogs to nerf traps --- main.py | 67 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index 9368f0f..06ff146 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ from datetime import datetime, time, timedelta # Set up environment variable for the phone number PHONE_NUMBER = os.getenv('PHONE_NUMBER', '1234567890') # Default to '1234567890' if not set RECEIVE_URL = f"http://localhost:9922/v1/receive/{PHONE_NUMBER}" +REMOVE_ATTACHMENT_URL = f"http://localhost:9922/v1/attachments/" SEND_URL = 'http://localhost:9922/v2/send' GROUP_ID = os.getenv('GROUP_ID', '') GROUP_ID_SEND = os.getenv('GROUP_ID_SEND', '') @@ -40,7 +41,6 @@ def fetch_and_download_image(api_url, image_url_key): response.raise_for_status() # Ensure the request was successful # Parse the response JSON to get the image URL - print("response: ", response.json) data = response.json() # Retrieve the image URL based on the provided key @@ -53,14 +53,14 @@ def fetch_and_download_image(api_url, image_url_key): return download_image(image_url) -def send_image(base64_attachments, recipients=GROUP_ID_SEND): +async def send_image(base64_attachments, recipients=GROUP_ID_SEND): data = { "base64_attachments": [base64_attachments], "number": PHONE_NUMBER, "recipients": [recipients] } response = requests.post(SEND_URL, json=data) - if response.status_code == 200: + if response.status_code == 200 or response.status_code == 201: print("Request was successful.") else: print(f"Request failed with status code: {response.status_code}") @@ -97,7 +97,7 @@ def extract_message_content(message): def extract_source_uuid(message): - #message_json = json.loads(message) + message_json = json.loads(message) #inside_message = message_json.get('envelope', {}).get('sourceUuid', {}) return "" @@ -110,9 +110,9 @@ def update_string_count(string, mapping): command_map = { - "!kot": lambda recipient: send_image(fetch_and_download_image("https://api.thecatapi.com/v1/images/search", [0, 'url']), recipient), - "!pies": lambda recipient: send_image(fetch_and_download_image("https://dog.ceo/api/breeds/image/random", 'message'), recipient), - "!traps": lambda recipient: send_image(download_image(((rule34Py().random_post(["trap"])).sample)), recipient) + ("!kot", "!koty", "!cat", "!cats", "!meow"): lambda recipient: send_image(fetch_and_download_image("https://api.thecatapi.com/v1/images/search", [0, 'url']), recipient), + ("!pies", "!psy", "!dog", "!dogs", "!woof"): lambda recipient: send_image(fetch_and_download_image("https://dog.ceo/api/breeds/image/random", 'message'), recipient), + ("!traps"): lambda recipient: send_image(download_image(((rule34Py().random_post(["trap"])).sample)), recipient) } @@ -149,6 +149,38 @@ async def scheduled_task(queue): send_message(message_count) queue.task_done() +async def trigger_command(message_content, recipient): + message_value = message_message(message_content) + for command_triggers, command_function in command_map.items(): + if message_value in command_triggers: + await command_function(recipient) + break + +async def send_to_group(message_content): + if message_group_id(message_content) == GROUP_ID: + #await count_messages(message_content, queue) + trigger_command(message_content, GROUP_ID_SEND) + +async def remove_attachment(attachment_id): + response = requests.delete(REMOVE_ATTACHMENT_URL + attachment_id) + if response.status_code == 200 or response.status_code == 204: + print("Request remove_attachment was successful.") + else: + print(f"Request remove_attachment failed with status code: {response.status_code}") + print(response) + +async def get_attachments(): + response = requests.get(REMOVE_ATTACHMENT_URL) + if response.status_code == 200: + attachments = json.loads(response.content) + print("attachments: ", attachments) + for attachment in attachments: + print("attachment: ", attachment) + await remove_attachment(attachment) + else: + print(f"Request failed with status code: {response.status_code}") + print(response) + async def listen_to_server(queue): uri = f"ws://localhost:9922/v1/receive/{PHONE_NUMBER}?send_read_receipts=false" async with websockets.connect(uri) as websocket: @@ -156,23 +188,10 @@ async def listen_to_server(queue): try: async for message in websocket: message_content = extract_message_content(message) - print(message_content) - if message_group_id(message_content) == GROUP_ID: - await count_messages(message_content, queue) - message_value = message_message(message_content) - if message_value in command_map: - # Call the corresponding function - command_map[message_value](GROUP_ID_SEND) - else: - print("Unknown command") - elif message_content.get('destinationNumber', {}) == PHONE_NUMBER: - await count_messages(message_content, queue) - message_value = message_message(message_content) - if message_value in command_map: - # Call the corresponding function - command_map[message_value](PHONE_NUMBER) - else: - print("Unknown command") + #send_to_group(message_content) + if message_content.get('destinationNumber', {}) == PHONE_NUMBER: + #await count_messages(message_content, queue) + await trigger_command(message_content, PHONE_NUMBER) except websockets.ConnectionClosed as e: print(f"Connection closed: {e}")