prepend subject to text body

This commit is contained in:
DJ Gillespie 2025-09-10 18:36:53 -06:00
parent 1ee9b4dfbe
commit f232128e47

69
app.py
View File

@ -1,9 +1,9 @@
import os import os
import asyncio import asyncio
from fastapi import FastAPI, Form from fastapi import FastAPI, Form
from nio import AsyncClient, RoomPreset
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from starlette.requests import Request
import niobot
app = FastAPI() app = FastAPI()
@ -12,66 +12,57 @@ HOMESERVER = os.getenv("MATRIX_HOMESERVER")
USER_ID = os.getenv("MATRIX_USER_ID") USER_ID = os.getenv("MATRIX_USER_ID")
PASSWORD = os.getenv("MATRIX_PASSWORD") PASSWORD = os.getenv("MATRIX_PASSWORD")
# Initialize nio client globally and login once on startup # Initialize NioBot globally
matrix_client = AsyncClient(HOMESERVER, USER_ID) bot = niobot.NioBot(
homeserver=HOMESERVER,
user_id=USER_ID,
password=PASSWORD,
)
@app.on_event("startup") @app.on_event("startup")
async def startup_event(): async def startup_event():
await matrix_client.login(PASSWORD) await bot.login()
@app.on_event("shutdown") @app.on_event("shutdown")
async def shutdown_event(): async def shutdown_event():
await matrix_client.logout() await bot.logout()
await matrix_client.close() await bot.close()
@app.get("/health") @app.get("/health")
async def health_check(): async def health_check():
return {"status": "ok", "message": "Service is running"} return {"status": "ok", "message": "Service is running"}
async def find_dm_room(client: AsyncClient, target_jid: str):
rooms = await client.joined_rooms()
for room_id in rooms.rooms:
room = client.rooms.get(room_id)
if room is None:
await client.room_get_state(room_id)
room = client.rooms.get(room_id)
if room and room.is_direct and target_jid in room.users:
return room_id
return None
@app.post("/mailgun-webhook") @app.post("/mailgun-webhook")
async def mailgun_webhook( async def mailgun_webhook(
request: Request,
recipient: str = Form(...), recipient: str = Form(...),
subject: str = Form(""), subject: str = Form(""),
body_plain: str = Form("", alias="body-plain"), body_plain: str = Form("", alias="body-plain"),
): ):
phone_jid_localpart = recipient.split("@")[0] phone_jid_localpart = recipient.split("@")[0]
if len(phone_jid_localpart) == 9
target_jid = f"@_bifrost_=2b{phone_jid_localpart}=40cheogram.com:aria-net.org" target_jid = f"@_bifrost_=2b{phone_jid_localpart}=40cheogram.com:aria-net.org"
message = f"{'('+subject+')' if subject else ''} {body_plain}" if body_plain else "(I got a text for you from Salesforce, but it didn't tell me what it was! - Monubot)"
print(f"RECIPIENT: {recipient}\nSUBJECT: {subject}\nBODY: {body_plain}") # Try to find existing DM rooms
print(f"TARGET JID: {target_jid}") rooms = await bot.get_dm_rooms(target_jid)
if rooms:
room_id = await find_dm_room(matrix_client, target_jid) room_id = rooms[0]
if not room_id: else:
resp = await matrix_client.room_create( # Create DM room; Matrix generates the room_id
invite=[target_jid], response = await bot.create_dm_room(target_jid)
is_direct=True, room_id = response.room_id
preset=RoomPreset.private_chat, # Set human-readable room name
room_name = f"Email->SMS with {phone_jid_localpart}"
await bot.client.room_put_state(
room_id,
"m.room.name",
{"name": room_name}
) )
room_id = resp.room_id
message = f"{body_plain}" if body_plain else "(empty message)" # Send message to the direct room
await matrix_client.room_send( await bot.send_message(room_id, message)
room_id,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": message,
}
)
return JSONResponse({"status": "SMS sent", "to": target_jid}) return JSONResponse({"status": "SMS sent", "to": target_jid, "room_id": room_id})
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn import uvicorn