Skip to content

Commit

Permalink
Merge pull request #26 from SunbirdAI/webhook-module
Browse files Browse the repository at this point in the history
Cleaned the code
  • Loading branch information
yigagilbert authored Jun 14, 2024
2 parents 49296e8 + 326b246 commit 9a49119
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 59 deletions.
55 changes: 0 additions & 55 deletions app/inference_services/whats_app_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import mimetypes
import requests
import logging
# from requests_toolbelt.multipart.encoder import MultipartEncoder
from requests_toolbelt import MultipartEncoder
from typing import Dict, Any, List, Union

Expand All @@ -15,7 +14,6 @@



# token = os.getenv("WHATSAPP_TOKEN")
base_url = "https://graph.facebook.com/v12.0"
v15_base_url = "https://graph.facebook.com/v15.0"

Expand Down Expand Up @@ -544,38 +542,6 @@ def query_media_url(media_id: str):
logging.info(f"Status code: {r.status_code}")
logging.info(f"Response: {r.json()}")
return None

# def download_media(media_url: str, mime_type: str, file_path: str = "temp"):
# """
# Download media from media url obtained either by manually uploading media or received media

# Args:
# media_url[str]: Media url of the media
# mime_type[str]: Mime type of the media
# file_path[str]: Path of the file to be downloaded to. Default is "temp"
# Do not include the file extension. It will be added automatically.

# Returns:
# str: Media url

# """
# r = requests.get(media_url, headers=headers)
# content = r.content
# extension = mime_type.split("/")[1]
# # create a temporary file
# try:

# save_file_here = (
# f"{file_path}.{extension}" if file_path else f"temp.{extension}"
# )
# with open(save_file_here, "wb") as f:
# f.write(content)
# logging.info(f"Media downloaded to {save_file_here}")
# return f.name
# except Exception as e:
# print(e)
# logging.info(f"Error downloading media to {save_file_here}")
# return None

def download_media(media_url, access_token, file_path="downloaded_media_file"):
"""
Expand Down Expand Up @@ -912,24 +878,3 @@ def get_media_url(media_id, token):
return media_url
else:
raise Exception(f"Failed to retrieve media URL. HTTP Status: {response.status_code}, Response: {response.text}")


# def download_media(media_url, access_token, file_path="downloaded_media_file"):
# """
# Download the media from the media URL obtained from the WhatsApp Business API.

# :param media_url: The URL of the media file to download.
# :param access_token: The access token for authenticating with the WhatsApp Business API.
# :param file_path: The local file path where the media should be saved.
# :return: The path to the downloaded media file.
# """
# headers = {'Authorization': f'Bearer {access_token}'}
# response = requests.get(media_url, headers=headers, stream=True)

# if response.status_code == 200:
# with open(file_path, 'wb') as f:
# for chunk in response.iter_content(chunk_size=8192):
# f.write(chunk)
# return file_path
# else:
# raise Exception(f"Failed to download media. HTTP Status: {response.status_code}")
5 changes: 1 addition & 4 deletions app/routers/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,6 @@ def get_phone_number_id(payload):
def get_from_number(payload):
return payload["entry"][0]["changes"][0]["value"]["messages"][0]["from"]

# def get_message(payload):
# return payload["entry"][0]["changes"][0]["value"]["messages"][0]["text"]["body"]


def detect_language(text):
endpoint = runpod.Endpoint(os.getenv("RUNPOD_ENDPOINT_ID"))
Expand All @@ -383,7 +380,7 @@ def detect_language(text):
"text": text,
}
},
timeout=60, # Timeout in seconds.
timeout=60,
)

logging.info(f"Request response: {request_response}")
Expand Down

0 comments on commit 9a49119

Please sign in to comment.