Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
import logging
|
| 2 |
import re
|
| 3 |
import requests
|
| 4 |
-
from fastapi import FastAPI
|
| 5 |
-
from pydantic import BaseModel,
|
| 6 |
import uvicorn
|
| 7 |
|
| 8 |
# =======================
|
| 9 |
# Configuration Settings
|
| 10 |
# =======================
|
| 11 |
-
#
|
| 12 |
-
SMTP_API_URL = "https://smtp-server-ten.vercel.app/smtp" # <--- change this to your deployed Vercel endpoint
|
| 13 |
|
| 14 |
# =======================
|
| 15 |
# Logging Configuration
|
|
@@ -18,24 +17,20 @@ logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %
|
|
| 18 |
logger = logging.getLogger("bot_server")
|
| 19 |
|
| 20 |
# =======================
|
| 21 |
-
#
|
| 22 |
# =======================
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# =======================
|
| 27 |
-
#
|
| 28 |
# =======================
|
| 29 |
-
|
| 30 |
-
EMAIL_REGEX = re.compile(r"^[^@]+@[^@]+\.[^@]+$")
|
| 31 |
-
|
| 32 |
-
def validate_email(email: str) -> bool:
|
| 33 |
-
return re.match(EMAIL_REGEX, email) is not None
|
| 34 |
|
| 35 |
def send_email_via_api(from_addr: str, to_addr: str, subject: str, body: str) -> bool:
|
| 36 |
-
"""
|
| 37 |
-
Calls the external SMTP API endpoint to send an email.
|
| 38 |
-
"""
|
| 39 |
payload = {
|
| 40 |
"from": from_addr,
|
| 41 |
"to": to_addr,
|
|
@@ -53,78 +48,14 @@ def send_email_via_api(from_addr: str, to_addr: str, subject: str, body: str) ->
|
|
| 53 |
logger.error("Error calling SMTP API: %s", e)
|
| 54 |
return False
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
class Message(BaseModel):
|
| 63 |
-
text: Text
|
| 64 |
-
|
| 65 |
-
class BotRequest(BaseModel):
|
| 66 |
-
from_: str = Field(..., alias="from")
|
| 67 |
-
message: Message
|
| 68 |
-
|
| 69 |
-
# =======================
|
| 70 |
-
# FastAPI Bot Application
|
| 71 |
-
# =======================
|
| 72 |
-
app = FastAPI()
|
| 73 |
-
|
| 74 |
-
@app.post("/bot")
|
| 75 |
-
async def bot_endpoint(request_data: BotRequest):
|
| 76 |
-
payload = request_data.dict(by_alias=True)
|
| 77 |
-
logger.debug("Received request payload: %s", payload)
|
| 78 |
-
|
| 79 |
-
sender_number = request_data.from_
|
| 80 |
-
message_body = request_data.message.text.body.strip()
|
| 81 |
-
logger.info("Processing message from %s: '%s'", sender_number, message_body)
|
| 82 |
-
|
| 83 |
-
state = conversations.get(sender_number, {})
|
| 84 |
-
|
| 85 |
-
if not state:
|
| 86 |
-
conversations[sender_number] = {"step": "from"}
|
| 87 |
-
response_msg = "Welcome to SMTP Assistant! Please provide your 'From' email address."
|
| 88 |
else:
|
| 89 |
-
|
| 90 |
-
logger.debug("Current step for %s: %s", sender_number, step)
|
| 91 |
-
if step == "from":
|
| 92 |
-
if validate_email(message_body):
|
| 93 |
-
state["from"] = message_body
|
| 94 |
-
state["step"] = "to"
|
| 95 |
-
response_msg = "Thanks! Now, please provide the recipient's email address."
|
| 96 |
-
else:
|
| 97 |
-
response_msg = "Invalid email format. Please provide a valid 'From' email address."
|
| 98 |
-
elif step == "to":
|
| 99 |
-
if validate_email(message_body):
|
| 100 |
-
state["to"] = message_body
|
| 101 |
-
state["step"] = "subject"
|
| 102 |
-
response_msg = "Great! Please provide the email subject."
|
| 103 |
-
else:
|
| 104 |
-
response_msg = "Invalid email format. Please provide a valid recipient email address."
|
| 105 |
-
elif step == "subject":
|
| 106 |
-
state["subject"] = message_body
|
| 107 |
-
state["step"] = "body"
|
| 108 |
-
response_msg = "Almost done! Please provide the email body."
|
| 109 |
-
elif step == "body":
|
| 110 |
-
state["body"] = message_body
|
| 111 |
-
from_addr = state.get("from")
|
| 112 |
-
to_addr = state.get("to")
|
| 113 |
-
subject = state.get("subject")
|
| 114 |
-
body = state.get("body")
|
| 115 |
-
logger.info("Attempting to send email from %s to %s with subject '%s'", from_addr, to_addr, subject)
|
| 116 |
-
if send_email_via_api(from_addr, to_addr, subject, body):
|
| 117 |
-
response_msg = f"Email sent successfully from {from_addr} to {to_addr}."
|
| 118 |
-
else:
|
| 119 |
-
response_msg = "Failed to send email. Please try again later."
|
| 120 |
-
del conversations[sender_number]
|
| 121 |
-
logger.info("Cleared conversation state for %s", sender_number)
|
| 122 |
-
else:
|
| 123 |
-
response_msg = "Unrecognized step. Let's start over. Please provide your 'From' email address."
|
| 124 |
-
conversations[sender_number] = {"step": "from"}
|
| 125 |
-
|
| 126 |
-
logger.debug("Response message to %s: %s", sender_number, response_msg)
|
| 127 |
-
return {"status": "success", "message": response_msg}
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
uvicorn.run("bot_server:app", host="0.0.0.0", port=5000, reload=True)
|
|
|
|
| 1 |
import logging
|
| 2 |
import re
|
| 3 |
import requests
|
| 4 |
+
from fastapi import FastAPI, HTTPException
|
| 5 |
+
from pydantic import BaseModel, EmailStr
|
| 6 |
import uvicorn
|
| 7 |
|
| 8 |
# =======================
|
| 9 |
# Configuration Settings
|
| 10 |
# =======================
|
| 11 |
+
SMTP_API_URL = "https://smtp-server-ten.vercel.app/smtp" # Change to your deployed Vercel endpoint
|
|
|
|
| 12 |
|
| 13 |
# =======================
|
| 14 |
# Logging Configuration
|
|
|
|
| 17 |
logger = logging.getLogger("bot_server")
|
| 18 |
|
| 19 |
# =======================
|
| 20 |
+
# Pydantic Model for Email Requests
|
| 21 |
# =======================
|
| 22 |
+
class EmailRequest(BaseModel):
|
| 23 |
+
from_: EmailStr = Field(..., alias="from")
|
| 24 |
+
to: EmailStr
|
| 25 |
+
subject: str
|
| 26 |
+
body: str
|
| 27 |
|
| 28 |
# =======================
|
| 29 |
+
# FastAPI Bot Application
|
| 30 |
# =======================
|
| 31 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def send_email_via_api(from_addr: str, to_addr: str, subject: str, body: str) -> bool:
|
|
|
|
|
|
|
|
|
|
| 34 |
payload = {
|
| 35 |
"from": from_addr,
|
| 36 |
"to": to_addr,
|
|
|
|
| 48 |
logger.error("Error calling SMTP API: %s", e)
|
| 49 |
return False
|
| 50 |
|
| 51 |
+
@app.post("/send-email")
|
| 52 |
+
async def send_email(request_data: EmailRequest):
|
| 53 |
+
logger.debug("Received email request: %s", request_data.dict(by_alias=True))
|
| 54 |
+
success = send_email_via_api(request_data.from_, request_data.to, request_data.subject, request_data.body)
|
| 55 |
+
if success:
|
| 56 |
+
return {"status": "success", "message": "Email sent successfully."}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
+
raise HTTPException(status_code=500, detail="Failed to send email.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
uvicorn.run("bot_server:app", host="0.0.0.0", port=5000, reload=True)
|