|
from os.path import abspath |
|
from pprint import pprint |
|
|
|
import requests |
|
from flask import Flask |
|
from flask import request |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
def send_message(chat_id, text): |
|
""" |
|
Send message to chat_id. |
|
:param chat_id: Phone number + "@c.us" suffix - 1231231231@c.us |
|
:param text: Message for the recipient |
|
""" |
|
|
|
response = requests.post( |
|
"http://localhost:7860/api/sendText", |
|
json={ |
|
"chatId": chat_id, |
|
"text": text, |
|
"session": "default", |
|
}, |
|
) |
|
response.raise_for_status() |
|
|
|
def send_seen(chat_id, message_id, participant): |
|
response = requests.post( |
|
"http://localhost:7860/api/sendSeen", |
|
json={ |
|
"session": "default", |
|
"chatId": chat_id, |
|
"messageId": message_id, |
|
"participant": participant, |
|
}, |
|
) |
|
response.raise_for_status() |
|
|
|
@app.route("/") |
|
def whatsapp_echo(): |
|
return "WhatsApp Download Files Bot is ready!" |
|
|
|
|
|
@app.route("/bot", methods=["GET", "POST"]) |
|
def whatsapp_webhook(): |
|
if request.method == "GET": |
|
return "WhatsApp Download Files Bot is ready!" |
|
|
|
data = request.get_json() |
|
pprint(data) |
|
if data["event"] != "message": |
|
|
|
return f"Unknown event {data['event']}" |
|
|
|
payload = data["payload"] |
|
|
|
if not payload.get("mediaUrl", None): |
|
return "No files in the message" |
|
|
|
|
|
chat_id = payload["from"] |
|
|
|
message_id = payload['id'] |
|
|
|
participant = payload.get('participant') |
|
|
|
send_seen(chat_id=chat_id, message_id=message_id, participant=participant) |
|
|
|
|
|
client_url = payload["mediaUrl"] |
|
filename = client_url.split("/")[-1] |
|
path = abspath("./" + filename) |
|
r = requests.get(client_url) |
|
with open(path, "wb") as f: |
|
f.write(r.content) |
|
|
|
|
|
text = f"We have downloaded file here: {path}" |
|
print(text) |
|
send_message(chat_id=chat_id, text=text) |
|
|
|
|
|
return "OK" |
|
|