|
from flask import Flask, request, jsonify |
|
import requests |
|
import os |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
EXTERNAL_CHATBOT_URL = "https://your-external-chatbot-url.com/api/chat" |
|
|
|
@app.route("/send_message", methods=["POST"]) |
|
def send_message(): |
|
try: |
|
|
|
user_message = request.json.get("message", "") |
|
|
|
|
|
external_response = requests.post( |
|
EXTERNAL_CHATBOT_URL, |
|
json={"message": user_message}, |
|
headers={"Content-Type": "application/json"} |
|
) |
|
|
|
|
|
if external_response.status_code == 200: |
|
|
|
chatbot_response = external_response.json().get("response", "No response.") |
|
else: |
|
chatbot_response = f"Error from external chatbot: {external_response.status_code}" |
|
|
|
|
|
return jsonify({"response": chatbot_response}) |
|
|
|
except Exception as e: |
|
return jsonify({"error": str(e)}), 500 |
|
|
|
if __name__ == "__main__": |
|
|
|
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860))) |
|
|