Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,21 +11,19 @@ import uvicorn
|
|
| 11 |
# =======================
|
| 12 |
# Configuration Settings
|
| 13 |
# =======================
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
SMTP_SERVER_PORT = 1025 # Port for our SMTP server
|
| 18 |
|
| 19 |
# SMTP client settings (used by the bot to send emails via our SMTP server)
|
| 20 |
-
CLIENT_SMTP_HOST = "localhost"
|
| 21 |
-
SMTP_PORT = 1025
|
| 22 |
-
SMTP_USER = "noreply@yourdomain.com"
|
| 23 |
-
SMTP_PASSWORD = "password"
|
| 24 |
|
| 25 |
# =======================
|
| 26 |
# Logging Configuration
|
| 27 |
# =======================
|
| 28 |
-
|
| 29 |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 30 |
logger = logging.getLogger("bot_brain")
|
| 31 |
|
|
@@ -38,7 +36,6 @@ conversations = {}
|
|
| 38 |
# =======================
|
| 39 |
# Helper Functions
|
| 40 |
# =======================
|
| 41 |
-
|
| 42 |
# Regex for validating email addresses
|
| 43 |
EMAIL_REGEX = re.compile(r"^[^@]+@[^@]+\.[^@]+$")
|
| 44 |
|
|
@@ -49,7 +46,7 @@ def validate_email(email: str) -> bool:
|
|
| 49 |
def send_email(from_addr: str, to_addr: str, subject: str, body: str) -> bool:
|
| 50 |
"""
|
| 51 |
Sends an email using the SMTP client.
|
| 52 |
-
|
| 53 |
"""
|
| 54 |
msg = MIMEText(body)
|
| 55 |
msg['Subject'] = subject
|
|
@@ -59,9 +56,9 @@ def send_email(from_addr: str, to_addr: str, subject: str, body: str) -> bool:
|
|
| 59 |
logger.debug(f"Preparing to send email from {from_addr} to {to_addr} with subject '{subject}'")
|
| 60 |
|
| 61 |
try:
|
|
|
|
| 62 |
with smtplib.SMTP(CLIENT_SMTP_HOST, SMTP_PORT) as server:
|
| 63 |
-
server.starttls()
|
| 64 |
-
server.login(SMTP_USER, SMTP_PASSWORD)
|
| 65 |
server.sendmail(from_addr, [to_addr], msg.as_string())
|
| 66 |
logger.info(f"Email sent successfully from {from_addr} to {to_addr}")
|
| 67 |
return True
|
|
@@ -72,7 +69,6 @@ def send_email(from_addr: str, to_addr: str, subject: str, body: str) -> bool:
|
|
| 72 |
# =======================
|
| 73 |
# Pydantic Models
|
| 74 |
# =======================
|
| 75 |
-
|
| 76 |
class Text(BaseModel):
|
| 77 |
body: str
|
| 78 |
|
|
@@ -86,12 +82,10 @@ class BotRequest(BaseModel):
|
|
| 86 |
# =======================
|
| 87 |
# FastAPI Application
|
| 88 |
# =======================
|
| 89 |
-
|
| 90 |
app = FastAPI()
|
| 91 |
|
| 92 |
@app.post("/bot")
|
| 93 |
async def bot_endpoint(request_data: BotRequest):
|
| 94 |
-
# Convert payload to dictionary using aliases
|
| 95 |
payload = request_data.dict(by_alias=True)
|
| 96 |
logger.debug("Received request payload: %s", payload)
|
| 97 |
|
|
@@ -150,7 +144,6 @@ async def bot_endpoint(request_data: BotRequest):
|
|
| 150 |
# =======================
|
| 151 |
# SMTP Server (using aiosmtpd)
|
| 152 |
# =======================
|
| 153 |
-
|
| 154 |
def start_smtp_server():
|
| 155 |
"""
|
| 156 |
Start our own SMTP server using aiosmtpd.
|
|
@@ -165,10 +158,9 @@ def start_smtp_server():
|
|
| 165 |
# =======================
|
| 166 |
# Main Execution
|
| 167 |
# =======================
|
| 168 |
-
|
| 169 |
if __name__ == "__main__":
|
| 170 |
# Start the SMTP server
|
| 171 |
smtp_controller = start_smtp_server()
|
| 172 |
|
| 173 |
-
# Run the FastAPI app
|
| 174 |
uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True)
|
|
|
|
| 11 |
# =======================
|
| 12 |
# Configuration Settings
|
| 13 |
# =======================
|
| 14 |
+
# SMTP server settings for our own SMTP server (running locally)
|
| 15 |
+
SMTP_SERVER_HOST = "localhost" # Hostname for our SMTP server
|
| 16 |
+
SMTP_SERVER_PORT = 1025 # Port for our SMTP server (local testing port)
|
|
|
|
| 17 |
|
| 18 |
# SMTP client settings (used by the bot to send emails via our SMTP server)
|
| 19 |
+
CLIENT_SMTP_HOST = "localhost" # Must match SMTP_SERVER_HOST for local testing
|
| 20 |
+
SMTP_PORT = 1025 # Must match SMTP_SERVER_PORT
|
| 21 |
+
SMTP_USER = "noreply@yourdomain.com" # For local testing with a simple SMTP server, these values are not used
|
| 22 |
+
SMTP_PASSWORD = "password"
|
| 23 |
|
| 24 |
# =======================
|
| 25 |
# Logging Configuration
|
| 26 |
# =======================
|
|
|
|
| 27 |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 28 |
logger = logging.getLogger("bot_brain")
|
| 29 |
|
|
|
|
| 36 |
# =======================
|
| 37 |
# Helper Functions
|
| 38 |
# =======================
|
|
|
|
| 39 |
# Regex for validating email addresses
|
| 40 |
EMAIL_REGEX = re.compile(r"^[^@]+@[^@]+\.[^@]+$")
|
| 41 |
|
|
|
|
| 46 |
def send_email(from_addr: str, to_addr: str, subject: str, body: str) -> bool:
|
| 47 |
"""
|
| 48 |
Sends an email using the SMTP client.
|
| 49 |
+
For local testing with our own SMTP server (plain text, no TLS), we remove starttls and login.
|
| 50 |
"""
|
| 51 |
msg = MIMEText(body)
|
| 52 |
msg['Subject'] = subject
|
|
|
|
| 56 |
logger.debug(f"Preparing to send email from {from_addr} to {to_addr} with subject '{subject}'")
|
| 57 |
|
| 58 |
try:
|
| 59 |
+
# Connect to the local SMTP server (plain text)
|
| 60 |
with smtplib.SMTP(CLIENT_SMTP_HOST, SMTP_PORT) as server:
|
| 61 |
+
# For a plain SMTP server running locally (e.g. aiosmtpd Sink), do not call starttls() or login()
|
|
|
|
| 62 |
server.sendmail(from_addr, [to_addr], msg.as_string())
|
| 63 |
logger.info(f"Email sent successfully from {from_addr} to {to_addr}")
|
| 64 |
return True
|
|
|
|
| 69 |
# =======================
|
| 70 |
# Pydantic Models
|
| 71 |
# =======================
|
|
|
|
| 72 |
class Text(BaseModel):
|
| 73 |
body: str
|
| 74 |
|
|
|
|
| 82 |
# =======================
|
| 83 |
# FastAPI Application
|
| 84 |
# =======================
|
|
|
|
| 85 |
app = FastAPI()
|
| 86 |
|
| 87 |
@app.post("/bot")
|
| 88 |
async def bot_endpoint(request_data: BotRequest):
|
|
|
|
| 89 |
payload = request_data.dict(by_alias=True)
|
| 90 |
logger.debug("Received request payload: %s", payload)
|
| 91 |
|
|
|
|
| 144 |
# =======================
|
| 145 |
# SMTP Server (using aiosmtpd)
|
| 146 |
# =======================
|
|
|
|
| 147 |
def start_smtp_server():
|
| 148 |
"""
|
| 149 |
Start our own SMTP server using aiosmtpd.
|
|
|
|
| 158 |
# =======================
|
| 159 |
# Main Execution
|
| 160 |
# =======================
|
|
|
|
| 161 |
if __name__ == "__main__":
|
| 162 |
# Start the SMTP server
|
| 163 |
smtp_controller = start_smtp_server()
|
| 164 |
|
| 165 |
+
# Run the FastAPI app using Uvicorn
|
| 166 |
uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True)
|