add support for ntfy
This commit is contained in:
parent
da46260455
commit
e7c5f60fbe
@ -4,6 +4,6 @@ WORKDIR /app
|
|||||||
|
|
||||||
COPY relay.py .
|
COPY relay.py .
|
||||||
|
|
||||||
RUN pip install --no-cache-dir aiosmtpd python-telegram-bot
|
RUN pip install --no-cache-dir aiosmtpd python-telegram-bot httpx
|
||||||
|
|
||||||
CMD ["python", "relay.py"]
|
CMD ["python", "relay.py"]
|
||||||
|
@ -1,2 +1,5 @@
|
|||||||
TELEGRAM_API_KEY=
|
TELEGRAM_API_KEY=''
|
||||||
TELEGRAM_CHAT_ID=
|
TELEGRAM_CHAT_ID=''
|
||||||
|
|
||||||
|
NTFY_TOKEN='your_ntfy_token_here'
|
||||||
|
NTFY_URL='https://ntfy.sh'
|
||||||
|
60
relay.py
60
relay.py
@ -5,10 +5,13 @@ from email.parser import BytesParser
|
|||||||
from email.policy import default
|
from email.policy import default
|
||||||
import telegram
|
import telegram
|
||||||
import re
|
import re
|
||||||
|
import httpx
|
||||||
|
|
||||||
# Telegram 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
|
DEFAULT_CHAT_ID = os.getenv('TELEGRAM_CHAT_ID') # Fallback chat ID
|
||||||
|
NTFY_TOKEN = os.getenv('NTFY_TOKEN')
|
||||||
|
NTFY_URL = os.getenv('NTFY_URL')
|
||||||
|
|
||||||
# SMTP configuration
|
# SMTP configuration
|
||||||
SMTP_HOST = '0.0.0.0' # Listen on all interfaces
|
SMTP_HOST = '0.0.0.0' # Listen on all interfaces
|
||||||
@ -25,29 +28,50 @@ class SMTPHandler:
|
|||||||
|
|
||||||
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}"
|
||||||
|
|
||||||
telegram_message = f"Subject: {subject}\n\n{body}"
|
# Extract destination from the recipient email address
|
||||||
|
destination, service = self.extract_destination(envelope.rcpt_tos[0])
|
||||||
|
|
||||||
# Extract chat ID from the recipient email address
|
if service == 'telegram':
|
||||||
chat_id = self.extract_chat_id(envelope.rcpt_tos[0])
|
await self.send_telegram(destination, full_message)
|
||||||
|
elif service == 'ntfy':
|
||||||
bot = telegram.Bot(TELEGRAM_API_KEY)
|
await self.send_ntfy(destination, subject, body)
|
||||||
try:
|
else:
|
||||||
await bot.send_message(chat_id=chat_id, text=telegram_message)
|
print(f"Invalid service: {service}. Message not sent.")
|
||||||
print(f"Message sent to Telegram chat ID: {chat_id}")
|
|
||||||
except telegram.error.BadRequest as e:
|
|
||||||
print(f"Failed to send message to chat ID: {chat_id}. Error: {str(e)}")
|
|
||||||
print("Attempting to send to default chat ID.")
|
|
||||||
await bot.send_message(chat_id=DEFAULT_CHAT_ID, text=telegram_message)
|
|
||||||
|
|
||||||
return '250 Message accepted for delivery'
|
return '250 Message accepted for delivery'
|
||||||
|
|
||||||
def extract_chat_id(self, email):
|
def extract_destination(self, email):
|
||||||
match = re.match(r'(-?\d+)@telegram-relay\.local', email)
|
match = re.match(r'(.+)@(telegram|ntfy)$', email)
|
||||||
if match:
|
if match:
|
||||||
return match.group(1)
|
return match.group(1), match.group(2)
|
||||||
print(f"Invalid email format: {email}. Using default chat ID.")
|
print(f"Invalid email format: {email}. Using default Telegram chat ID.")
|
||||||
return DEFAULT_CHAT_ID
|
return DEFAULT_CHAT_ID, 'telegram'
|
||||||
|
|
||||||
|
async def send_telegram(self, chat_id, message):
|
||||||
|
bot = telegram.Bot(TELEGRAM_API_KEY)
|
||||||
|
try:
|
||||||
|
await bot.send_message(chat_id=chat_id, text=message)
|
||||||
|
print(f"Message sent to Telegram chat ID: {chat_id}")
|
||||||
|
except telegram.error.BadRequest as e:
|
||||||
|
print(f"Failed to send message to Telegram chat ID: {chat_id}. Error: {str(e)}")
|
||||||
|
print("Attempting to send to default Telegram chat ID.")
|
||||||
|
await bot.send_message(chat_id=DEFAULT_CHAT_ID, text=message)
|
||||||
|
|
||||||
|
async def send_ntfy(self, topic, subject, body):
|
||||||
|
headers = {
|
||||||
|
"Authorization": f"Bearer {NTFY_TOKEN}",
|
||||||
|
"Title": subject,
|
||||||
|
"Content-Type": "text/plain",
|
||||||
|
}
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
try:
|
||||||
|
response = await client.post(f"{NTFY_URL}/{topic}", content=body, headers=headers)
|
||||||
|
response.raise_for_status()
|
||||||
|
print(f"Message sent to ntfy topic: {topic}")
|
||||||
|
except httpx.HTTPStatusError as e:
|
||||||
|
print(f"Failed to send message to ntfy topic: {topic}. Error: {str(e)}")
|
||||||
|
|
||||||
async def start_smtp_server():
|
async def start_smtp_server():
|
||||||
controller = Controller(SMTPHandler(), hostname=SMTP_HOST, port=SMTP_PORT)
|
controller = Controller(SMTPHandler(), hostname=SMTP_HOST, port=SMTP_PORT)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user