remove fallback telegram chat id
This commit is contained in:
parent
bf1c6688a8
commit
0f45896d79
@ -1,5 +1,4 @@
|
|||||||
TELEGRAM_API_KEY=''
|
TELEGRAM_API_KEY=''
|
||||||
TELEGRAM_CHAT_ID=''
|
|
||||||
TELEGRAM_DOMAIN='telegram.local'
|
TELEGRAM_DOMAIN='telegram.local'
|
||||||
|
|
||||||
|
|
||||||
|
35
relay.py
35
relay.py
@ -14,7 +14,6 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
TELEGRAM_API_KEY = os.getenv('TELEGRAM_API_KEY')
|
TELEGRAM_API_KEY = os.getenv('TELEGRAM_API_KEY')
|
||||||
DEFAULT_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID') # Fallback chat ID
|
|
||||||
NTFY_TOKEN = os.getenv('NTFY_TOKEN')
|
NTFY_TOKEN = os.getenv('NTFY_TOKEN')
|
||||||
NTFY_URL = os.getenv('NTFY_URL')
|
NTFY_URL = os.getenv('NTFY_URL')
|
||||||
TELEGRAM_DOMAIN = os.getenv('TELEGRAM_DOMAIN', 'telegram.local')
|
TELEGRAM_DOMAIN = os.getenv('TELEGRAM_DOMAIN', 'telegram.local')
|
||||||
@ -34,19 +33,19 @@ class SMTPHandler:
|
|||||||
logger.info("Handling DATA command")
|
logger.info("Handling DATA command")
|
||||||
parser = BytesParser(policy=default)
|
parser = BytesParser(policy=default)
|
||||||
message = parser.parsebytes(envelope.content)
|
message = parser.parsebytes(envelope.content)
|
||||||
|
|
||||||
subject = message.get('subject', 'No subject')
|
subject = message.get('subject', 'No subject')
|
||||||
body = message.get_body(preferencelist=('plain', 'html')).get_content()
|
body = message.get_body(preferencelist=('plain', 'html')).get_content()
|
||||||
full_message = f"Subject: {subject}\n\n{body}"
|
full_message = f"Subject: {subject}\n\n{body}"
|
||||||
|
|
||||||
logger.info(f"Parsed email - Subject: {subject}")
|
logger.info(f"Parsed email - Subject: {subject}")
|
||||||
logger.debug(f"Email body: {body[:100]}...") # Log first 100 chars of body
|
logger.debug(f"Email body: {body[:100]}...") # Log first 100 chars of body
|
||||||
|
|
||||||
# Extract destination from the recipient email address
|
# Extract destination from the recipient email address
|
||||||
destination, service = self.extract_destination(envelope.rcpt_tos[0])
|
destination, service = self.extract_destination(envelope.rcpt_tos[0])
|
||||||
|
|
||||||
logger.info(f"Extracted destination: {destination}, service: {service}")
|
logger.info(f"Extracted destination: {destination}, service: {service}")
|
||||||
|
|
||||||
if service == 'telegram':
|
if service == 'telegram':
|
||||||
await self.send_telegram(destination, full_message)
|
await self.send_telegram(destination, full_message)
|
||||||
elif service == 'ntfy':
|
elif service == 'ntfy':
|
||||||
@ -60,21 +59,25 @@ class SMTPHandler:
|
|||||||
logger.info(f"Extracting destination from email: {email}")
|
logger.info(f"Extracting destination from email: {email}")
|
||||||
telegram_pattern = rf'^(.+)@{re.escape(TELEGRAM_DOMAIN)}$'
|
telegram_pattern = rf'^(.+)@{re.escape(TELEGRAM_DOMAIN)}$'
|
||||||
ntfy_pattern = rf'^(.+)@{re.escape(NTFY_DOMAIN)}$'
|
ntfy_pattern = rf'^(.+)@{re.escape(NTFY_DOMAIN)}$'
|
||||||
|
|
||||||
telegram_match = re.match(telegram_pattern, email)
|
telegram_match = re.match(telegram_pattern, email)
|
||||||
if telegram_match:
|
if telegram_match:
|
||||||
logger.info(f"Matched Telegram pattern: {telegram_match.group(1)}")
|
logger.info(f"Matched Telegram pattern: {telegram_match.group(1)}")
|
||||||
return telegram_match.group(1), 'telegram'
|
return telegram_match.group(1), 'telegram'
|
||||||
|
|
||||||
ntfy_match = re.match(ntfy_pattern, email)
|
ntfy_match = re.match(ntfy_pattern, email)
|
||||||
if ntfy_match:
|
if ntfy_match:
|
||||||
logger.info(f"Matched ntfy pattern: {ntfy_match.group(1)}")
|
logger.info(f"Matched ntfy pattern: {ntfy_match.group(1)}")
|
||||||
return ntfy_match.group(1), 'ntfy'
|
return ntfy_match.group(1), 'ntfy'
|
||||||
|
|
||||||
logger.warning(f"Invalid email format: {email}. Using default Telegram chat ID.")
|
logger.error(f"Invalid email format: {email}. Unable to determine service.")
|
||||||
return DEFAULT_CHAT_ID, 'telegram'
|
return None, None
|
||||||
|
|
||||||
async def send_telegram(self, chat_id, message):
|
async def send_telegram(self, chat_id, message):
|
||||||
|
if not chat_id:
|
||||||
|
logger.error("No valid Telegram chat ID provided. Message not sent.")
|
||||||
|
return
|
||||||
|
|
||||||
logger.info(f"Sending message to Telegram chat ID: {chat_id}")
|
logger.info(f"Sending message to Telegram chat ID: {chat_id}")
|
||||||
bot = telegram.Bot(TELEGRAM_API_KEY)
|
bot = telegram.Bot(TELEGRAM_API_KEY)
|
||||||
try:
|
try:
|
||||||
@ -82,14 +85,12 @@ class SMTPHandler:
|
|||||||
logger.info(f"Message sent successfully to Telegram chat ID: {chat_id}")
|
logger.info(f"Message sent successfully to Telegram chat ID: {chat_id}")
|
||||||
except telegram.error.BadRequest as e:
|
except telegram.error.BadRequest as e:
|
||||||
logger.error(f"Failed to send message to Telegram chat ID: {chat_id}. Error: {str(e)}")
|
logger.error(f"Failed to send message to Telegram chat ID: {chat_id}. Error: {str(e)}")
|
||||||
logger.info("Attempting to send to default Telegram chat ID.")
|
|
||||||
try:
|
|
||||||
await bot.send_message(chat_id=DEFAULT_CHAT_ID, text=message)
|
|
||||||
logger.info(f"Message sent successfully to default Telegram chat ID: {DEFAULT_CHAT_ID}")
|
|
||||||
except telegram.error.BadRequest as e:
|
|
||||||
logger.error(f"Failed to send message to default Telegram chat ID. Error: {str(e)}")
|
|
||||||
|
|
||||||
async def send_ntfy(self, topic, subject, body):
|
async def send_ntfy(self, topic, subject, body):
|
||||||
|
if not topic:
|
||||||
|
logger.error("No valid ntfy topic provided. Message not sent.")
|
||||||
|
return
|
||||||
|
|
||||||
logger.info(f"Sending message to ntfy topic: {topic}")
|
logger.info(f"Sending message to ntfy topic: {topic}")
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {NTFY_TOKEN}",
|
"Authorization": f"Bearer {NTFY_TOKEN}",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user