Samfredoly commited on
Commit
4b6b291
·
verified ·
1 Parent(s): bdcdf69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -2,10 +2,13 @@ import logging
2
  import smtplib
3
  import re
4
  from email.mime.text import MIMEText
5
- from fastapi import FastAPI, HTTPException
6
  from pydantic import BaseModel, Field
7
  from dotenv import load_dotenv
8
  import os
 
 
 
9
 
10
  # Load environment variables
11
  load_dotenv()
@@ -14,10 +17,13 @@ load_dotenv()
14
  logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
15
  logger = logging.getLogger("bot_brain")
16
 
17
- # SMTP Server Configuration
18
- SMTP_HOST = os.getenv("SMTP_HOST", "0.0.0.0")
19
- SMTP_PORT = int(os.getenv("SMTP_PORT", "587"))
20
- CLIENT_SMTP_HOST = os.getenv("CLIENT_SMTP_HOST", "localhost")
 
 
 
21
  SMTP_USER = os.getenv("SMTP_USER", "noreply@yourdomain.com")
22
  SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "password")
23
 
@@ -64,15 +70,14 @@ class BotRequest(BaseModel):
64
  from_: str = Field(..., alias="from")
65
  message: Message
66
 
67
- # FastAPI app instance
68
  app = FastAPI()
69
 
70
  @app.post("/bot")
71
  async def bot_endpoint(request_data: BotRequest):
72
- # Log the received request payload
73
  payload = request_data.dict(by_alias=True)
74
  logger.debug("Received request payload: %s", payload)
75
-
76
  sender_number = request_data.from_
77
  message_body = request_data.message.text.body.strip()
78
  logger.info("Processing message from %s: '%s'", sender_number, message_body)
@@ -80,7 +85,6 @@ async def bot_endpoint(request_data: BotRequest):
80
  state = conversations.get(sender_number, {})
81
 
82
  if not state:
83
- # Start a new conversation
84
  conversations[sender_number] = {"step": "from"}
85
  response_msg = "Welcome to SMTP Assistant! Please provide your 'From' email address."
86
  else:
@@ -125,6 +129,20 @@ async def bot_endpoint(request_data: BotRequest):
125
  logger.debug("Response message to %s: %s", sender_number, response_msg)
126
  return {"status": "success", "message": response_msg}
127
 
 
 
 
 
 
 
 
 
 
 
 
128
  if __name__ == "__main__":
129
- import uvicorn
 
 
 
130
  uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True)
 
2
  import smtplib
3
  import re
4
  from email.mime.text import MIMEText
5
+ from fastapi import FastAPI
6
  from pydantic import BaseModel, Field
7
  from dotenv import load_dotenv
8
  import os
9
+ from aiosmtpd.controller import Controller
10
+ from aiosmtpd.handlers import Sink
11
+ import uvicorn
12
 
13
  # Load environment variables
14
  load_dotenv()
 
17
  logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
18
  logger = logging.getLogger("bot_brain")
19
 
20
+ # SMTP server configuration for our own SMTP server
21
+ SMTP_SERVER_HOST = os.getenv("SMTP_SERVER_HOST", "localhost")
22
+ SMTP_SERVER_PORT = int(os.getenv("SMTP_SERVER_PORT", "1025"))
23
+
24
+ # Configuration for the SMTP client (sending emails via our own SMTP server)
25
+ CLIENT_SMTP_HOST = os.getenv("CLIENT_SMTP_HOST", SMTP_SERVER_HOST)
26
+ SMTP_PORT = int(os.getenv("SMTP_PORT", SMTP_SERVER_PORT))
27
  SMTP_USER = os.getenv("SMTP_USER", "noreply@yourdomain.com")
28
  SMTP_PASSWORD = os.getenv("SMTP_PASSWORD", "password")
29
 
 
70
  from_: str = Field(..., alias="from")
71
  message: Message
72
 
73
+ # Create FastAPI app
74
  app = FastAPI()
75
 
76
  @app.post("/bot")
77
  async def bot_endpoint(request_data: BotRequest):
 
78
  payload = request_data.dict(by_alias=True)
79
  logger.debug("Received request payload: %s", payload)
80
+
81
  sender_number = request_data.from_
82
  message_body = request_data.message.text.body.strip()
83
  logger.info("Processing message from %s: '%s'", sender_number, message_body)
 
85
  state = conversations.get(sender_number, {})
86
 
87
  if not state:
 
88
  conversations[sender_number] = {"step": "from"}
89
  response_msg = "Welcome to SMTP Assistant! Please provide your 'From' email address."
90
  else:
 
129
  logger.debug("Response message to %s: %s", sender_number, response_msg)
130
  return {"status": "success", "message": response_msg}
131
 
132
+ def start_smtp_server():
133
+ """
134
+ Start our own SMTP server using aiosmtpd.
135
+ For simplicity, we use the Sink handler which accepts and prints all emails.
136
+ """
137
+ handler = Sink()
138
+ controller = Controller(handler, hostname=SMTP_SERVER_HOST, port=SMTP_SERVER_PORT)
139
+ controller.start()
140
+ logger.info(f"SMTP server started at {SMTP_SERVER_HOST}:{SMTP_SERVER_PORT}")
141
+ return controller
142
+
143
  if __name__ == "__main__":
144
+ # Start the SMTP server (our own SMTP server)
145
+ smtp_controller = start_smtp_server()
146
+
147
+ # Run the FastAPI app using Uvicorn
148
  uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=True)