54 lines
1.2 KiB
Python
54 lines
1.2 KiB
Python
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
|
|
"""
|
|
# Send a text back via WhatsApp HTTP API
|
|
response = requests.post(
|
|
"http://localhost:3000/api/sendText",
|
|
json={
|
|
"chatId": chat_id,
|
|
"text": text,
|
|
"session": "default",
|
|
},
|
|
)
|
|
response.raise_for_status()
|
|
|
|
|
|
@app.route("/")
|
|
def whatsapp_echo():
|
|
return "WhatsApp Echo Bot is ready!"
|
|
|
|
|
|
@app.route("/bot", methods=["GET", "POST"])
|
|
def whatsapp_webhook():
|
|
if request.method == "GET":
|
|
return "WhatsApp Echo Bot is ready!"
|
|
|
|
data = request.get_json()
|
|
pprint(data)
|
|
if data["event"] != "message":
|
|
# We can't process other event yet
|
|
return f"Unknown event {data['event']}"
|
|
|
|
# Payload that we've got
|
|
payload = data["payload"]
|
|
# The text
|
|
text = payload["body"]
|
|
# Number in format 1231231231@c.us
|
|
from_ = payload["from"]
|
|
# Send a text back via WhatsApp HTTP API
|
|
send_message(chat_id=from_, text=text)
|
|
# Send OK back
|
|
return "OK"
|