Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,65 +1,38 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
-
import logging
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
from heyoo import WhatsApp
|
| 6 |
-
import random
|
| 7 |
-
import shutil
|
| 8 |
-
from tempfile import NamedTemporaryFile
|
| 9 |
-
from langchain_experimental.agents import create_pandas_dataframe_agent
|
| 10 |
-
import pandas as pd
|
| 11 |
-
from langchain_google_genai import GoogleGenerativeAI
|
| 12 |
-
from langchain_google_genai.chat_models import ChatGoogleGenerativeAI
|
| 13 |
-
import requests
|
| 14 |
-
import base64
|
| 15 |
-
import uuid
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# load env data
|
| 19 |
-
load_dotenv()
|
| 20 |
|
| 21 |
|
| 22 |
-
import re
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
messenger = WhatsApp(
|
| 31 |
os.environ["whatsapp_token"],
|
| 32 |
phone_number_id=os.environ["phone_number_id"] )
|
| 33 |
-
|
| 34 |
-
aai.settings.api_key = os.environ["aai_key"]
|
| 35 |
-
transcriber = aai.Transcriber()
|
| 36 |
|
| 37 |
g_api_key = os.environ["google_api_key"]
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=g_api_key, temperature=0.2)
|
| 44 |
-
|
| 45 |
-
def generateResponse(prompt):
|
| 46 |
-
answer = "I only understand images of receipts"
|
| 47 |
-
return answer
|
| 48 |
-
|
| 49 |
-
img_ID = "344744a88ad1098"
|
| 50 |
-
img_secret = "3c542a40c215327045d7155bddfd8b8bc84aebbf"
|
| 51 |
-
|
| 52 |
-
url = "https://api.imgur.com/3/image"
|
| 53 |
-
headers = {"Authorization": f"Client-ID {img_ID}"}
|
| 54 |
-
|
| 55 |
-
def respond(query_str:str):
|
| 56 |
-
response = "hello, I don't have a brain yet"
|
| 57 |
-
return response
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
@app.route("/", methods=["GET", "POST"])
|
| 61 |
-
def hook():
|
| 62 |
-
if request.method == "GET":
|
| 63 |
if request.args.get("hub.verify_token") == VERIFY_TOKEN:
|
| 64 |
logging.info("Verified webhook")
|
| 65 |
response = make_response(request.args.get("hub.challenge"), 200)
|
|
@@ -68,62 +41,81 @@ def hook():
|
|
| 68 |
logging.error("Webhook Verification failed")
|
| 69 |
return "Invalid verification token"
|
| 70 |
|
| 71 |
-
# get message update..
|
| 72 |
-
data = request.get_json()
|
| 73 |
-
changed_field = messenger.changed_field(data)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
# Handle greetings
|
| 88 |
-
if message.lower() in ("hi", "hello", "help", "how are you"):
|
| 89 |
-
response = "Hi there! My name is Petty Cash assistant. Upload an image of a receipt for me to record"
|
| 90 |
-
messenger.send_message(message=f"{response}, phone:{mobile}",recipient_id=mobile)
|
| 91 |
-
else:
|
| 92 |
-
response = str(generateResponse(message))
|
| 93 |
-
print("Response:", response)
|
| 94 |
-
logging.info(f"\nAnswer: {response}\n")
|
| 95 |
-
# Check if response is a string and represents a valid image path
|
| 96 |
-
if isinstance(response, str) and os.path.isfile(os.path.join(response)):
|
| 97 |
-
image_path = os.path.join(response)
|
| 98 |
-
print("My image path:", image_path)
|
| 99 |
-
with open(image_path, "rb") as file:
|
| 100 |
-
data = file.read()
|
| 101 |
-
base64_data = base64.b64encode(data)
|
| 102 |
-
|
| 103 |
-
# Upload image to Imgur and get URL
|
| 104 |
-
response = requests.post(url, headers=headers, data={"image": base64_data})
|
| 105 |
-
url1= response.json()["data"]["link"]
|
| 106 |
-
print(url1)
|
| 107 |
-
|
| 108 |
-
messenger.send_image(image=url1, recipient_id=mobile)
|
| 109 |
-
else:
|
| 110 |
-
# Handle cases where response is not a valid image path
|
| 111 |
-
messenger.send_message(message=f"{response}", recipient_id=mobile)
|
| 112 |
-
|
| 113 |
-
elif message_type == "image":
|
| 114 |
-
image = messenger.get_image(data)
|
| 115 |
-
image_id, mime_type = image["id"], image["mime_type"]
|
| 116 |
-
image_url = await messenger.query_media_url(image_id)
|
| 117 |
-
image_filename = await messenger.download_media(image_url, mime_type)
|
| 118 |
-
print(f"{mobile} sent image {image_filename}")
|
| 119 |
-
logging.info(f"{mobile} sent image {image_filename}")
|
| 120 |
-
|
| 121 |
-
else:
|
| 122 |
-
messenger.send_message(message="Please send me only images of receipts",recipient_id=mobile)
|
| 123 |
-
|
| 124 |
-
#else:messenger.send_message(message="Your mobile number is not registered with Quantilytix",recipient_id=mobile)
|
| 125 |
-
|
| 126 |
-
return "ok"
|
| 127 |
-
|
| 128 |
-
if __name__ == '__main__':
|
| 129 |
app.run(debug=True,host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
# messenger object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
+
import os
|
| 11 |
+
import logging
|
| 12 |
+
from heyoo import WhatsApp
|
| 13 |
+
from dotenv import load_dotenv
|
| 14 |
+
from flask import Flask, request, make_response
|
| 15 |
+
|
| 16 |
+
# Initialize Flask App
|
| 17 |
+
app = Flask(__name__)
|
| 18 |
+
|
| 19 |
+
# Load .env file
|
| 20 |
+
load_dotenv()
|
| 21 |
messenger = WhatsApp(
|
| 22 |
os.environ["whatsapp_token"],
|
| 23 |
phone_number_id=os.environ["phone_number_id"] )
|
| 24 |
+
VERIFY_TOKEN = "30cca545-3838-48b2-80a7-9e43b1ae8ce4"
|
|
|
|
|
|
|
| 25 |
|
| 26 |
g_api_key = os.environ["google_api_key"]
|
| 27 |
|
| 28 |
+
# Logging
|
| 29 |
+
logging.basicConfig(
|
| 30 |
+
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
| 31 |
+
)
|
| 32 |
|
| 33 |
|
| 34 |
+
@app.get("/")
|
| 35 |
+
async def verify_token():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if request.args.get("hub.verify_token") == VERIFY_TOKEN:
|
| 37 |
logging.info("Verified webhook")
|
| 38 |
response = make_response(request.args.get("hub.challenge"), 200)
|
|
|
|
| 41 |
logging.error("Webhook Verification failed")
|
| 42 |
return "Invalid verification token"
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
@app.post("/")
|
| 46 |
+
async def hook():
|
| 47 |
+
# Handle Webhook Subscriptions
|
| 48 |
+
data = request.get_json()
|
| 49 |
+
logging.info("Received webhook data: %s", data)
|
| 50 |
+
changed_field = messenger.changed_field(data)
|
| 51 |
+
if changed_field == "messages":
|
| 52 |
+
new_message = messenger.is_message(data)
|
| 53 |
+
if new_message:
|
| 54 |
+
mobile = messenger.get_mobile(data)
|
| 55 |
+
name = messenger.get_name(data)
|
| 56 |
+
message_type = messenger.get_message_type(data)
|
| 57 |
+
logging.info(
|
| 58 |
+
f"New Message; sender:{mobile} name:{name} type:{message_type}"
|
| 59 |
+
)
|
| 60 |
+
if message_type == "text":
|
| 61 |
+
message = messenger.get_message(data)
|
| 62 |
+
name = messenger.get_name(data)
|
| 63 |
+
logging.info("Message: %s", message)
|
| 64 |
+
await messenger.send_message(f"Hi {name}, nice to connect with you", mobile)
|
| 65 |
+
|
| 66 |
+
elif message_type == "interactive":
|
| 67 |
+
message_response = messenger.get_interactive_response(data)
|
| 68 |
+
interactive_type = message_response.get("type")
|
| 69 |
+
message_id = message_response[interactive_type]["id"]
|
| 70 |
+
message_text = message_response[interactive_type]["title"]
|
| 71 |
+
logging.info(f"Interactive Message; {message_id}: {message_text}")
|
| 72 |
+
|
| 73 |
+
elif message_type == "location":
|
| 74 |
+
message_location = messenger.get_location(data)
|
| 75 |
+
message_latitude = message_location["latitude"]
|
| 76 |
+
message_longitude = message_location["longitude"]
|
| 77 |
+
logging.info("Location: %s, %s", message_latitude, message_longitude)
|
| 78 |
+
|
| 79 |
+
elif message_type == "image":
|
| 80 |
+
image = messenger.get_image(data)
|
| 81 |
+
image_id, mime_type = image["id"], image["mime_type"]
|
| 82 |
+
image_url = await messenger.query_media_url(image_id)
|
| 83 |
+
image_filename = await messenger.download_media(image_url, mime_type)
|
| 84 |
+
logging.info(f"{mobile} sent image {image_filename}")
|
| 85 |
+
|
| 86 |
+
elif message_type == "video":
|
| 87 |
+
video = messenger.get_video(data)
|
| 88 |
+
video_id, mime_type = video["id"], video["mime_type"]
|
| 89 |
+
video_url = await messenger.query_media_url(video_id)
|
| 90 |
+
video_filename = await messenger.download_media(video_url, mime_type)
|
| 91 |
+
logging.info(f"{mobile} sent video {video_filename}")
|
| 92 |
+
|
| 93 |
+
elif message_type == "audio":
|
| 94 |
+
audio = messenger.get_audio(data)
|
| 95 |
+
audio_id, mime_type = audio["id"], audio["mime_type"]
|
| 96 |
+
audio_url = await messenger.query_media_url(audio_id)
|
| 97 |
+
audio_filename = await messenger.download_media(audio_url, mime_type)
|
| 98 |
+
logging.info(f"{mobile} sent audio {audio_filename}")
|
| 99 |
+
|
| 100 |
+
elif message_type == "document":
|
| 101 |
+
file = messenger.get_document(data)
|
| 102 |
+
file_id, mime_type = file["id"], file["mime_type"]
|
| 103 |
+
file_url = await messenger.query_media_url(file_id)
|
| 104 |
+
file_filename = await messenger.download_media(file_url, mime_type)
|
| 105 |
+
logging.info(f"{mobile} sent file {file_filename}")
|
| 106 |
+
else:
|
| 107 |
+
logging.info(f"{mobile} sent {message_type} ")
|
| 108 |
+
logging.info(data)
|
| 109 |
+
else:
|
| 110 |
+
delivery = messenger.get_delivery(data)
|
| 111 |
+
if delivery:
|
| 112 |
+
logging.info(f"Message : {delivery}")
|
| 113 |
+
else:
|
| 114 |
+
logging.info("No new message")
|
| 115 |
+
return "OK", 200
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
app.run(debug=True,host="0.0.0.0", port=7860)
|