notification_service.py 133 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256
  1. """Notification service for sending push notifications via various providers."""
  2. import asyncio
  3. import html
  4. import json
  5. import logging
  6. import re
  7. import smtplib
  8. from collections.abc import Callable
  9. from datetime import datetime, timedelta, timezone
  10. from email.mime.image import MIMEImage
  11. from email.mime.multipart import MIMEMultipart
  12. from email.mime.text import MIMEText
  13. from typing import Any
  14. from urllib.parse import quote
  15. import httpx
  16. from sqlalchemy import select
  17. from sqlalchemy.ext.asyncio import AsyncSession
  18. from backend.app.core.logging_filters import redact_refused_headers
  19. from backend.app.models.archive import PrintArchive
  20. from backend.app.models.notification import (
  21. NotificationDigestQueue,
  22. NotificationLog,
  23. NotificationProvider,
  24. TelegramPendingVerdict,
  25. )
  26. from backend.app.models.notification_template import NotificationTemplate
  27. from backend.app.services.email_service import add_required_headers
  28. from backend.app.services.notify_client import NotifyClient, NotifyError, notify_credentials, notify_supports_photos
  29. from backend.app.services.print_confirmation import one_tap_url
  30. from backend.app.utils.notification_photos import save_notification_photo
  31. from backend.app.utils.tray_ids import EXTERNAL_AMS_ID, is_ams_ht_id, unit_label
  32. logger = logging.getLogger(__name__)
  33. # How long a digest event is kept for: through one failed daily digest, so it
  34. # goes out with the next one, then dropped (FO-C4).
  35. DIGEST_RETRY_HOURS = 48
  36. # Honest User-Agent — matches the convention used by every other outbound
  37. # httpx client in the codebase (bambu_cloud, makerworld, firmware_check,
  38. # inventory). Previously this client leaked python-httpx/<version>, which
  39. # was both inconsistent with the rest of the project and a more obvious
  40. # bot signature for upstream WAFs.
  41. _USER_AGENT = "Bambuddy/1.0 (+https://github.com/maziggy/bambuddy)"
  42. # Appended to a Telegram outcome prompt in reaction mode (#3046); the message
  43. # itself carries no buttons there, so the user needs telling how to answer.
  44. TELEGRAM_REACTION_HINT = "React with \U0001f44d or \U0001f44e to record the outcome."
  45. def telegram_markdown_escape(message: str) -> str:
  46. """Escape underscores in the message body so Telegram Markdown parsing
  47. doesn't break on job names like "A1_plate_8" or error codes like
  48. "0300_0001". The title is already wrapped in *bold* markers, so only
  49. escape after the first newline. Shared with the reaction poller, which
  50. re-sends the stored body when it edits the prompt (#3046)."""
  51. if "\n" in message:
  52. title_part, body_part = message.split("\n", 1)
  53. body_part = body_part.replace("_", "\\_")
  54. return f"{title_part}\n{body_part}"
  55. return message
  56. def _looks_like_cloudflare_challenge(response: httpx.Response) -> bool:
  57. """Return True if ``response`` looks like a Cloudflare mitigation
  58. interstitial (JS challenge / managed challenge / block page) rather
  59. than a legitimate response passed through Cloudflare.
  60. Self-hosted servers behind Cloudflare (Tunnel, "Bot Fight Mode", or
  61. "Under Attack" mode) intercept non-browser clients at the edge and
  62. return a challenge HTML page instead of forwarding to the origin —
  63. so we never reach the user's actual ntfy / webhook backend.
  64. Cloudflare cannot be defeated from a Python client; the user has to
  65. add a security-skip rule on their side. We detect the shape so the
  66. UI can tell them that, instead of dumping the raw HTML.
  67. Detection deliberately does NOT rely on ``Server: cloudflare`` alone
  68. — Cloudflare adds that header to every response it proxies (success
  69. AND legitimate origin errors), so a real 401 "wrong token" from a
  70. CF-fronted ntfy would false-positive into a misleading "your CF is
  71. blocking" message. Reliable signals: the ``cf-mitigated`` header
  72. (set only when CF actively mitigates) and the challenge body shape.
  73. """
  74. if response.headers.get("cf-mitigated"):
  75. return True
  76. content_type = (response.headers.get("content-type") or "").lower()
  77. if "html" not in content_type:
  78. return False
  79. body = (response.text or "")[:1024].lower()
  80. # "Just a moment..." is Cloudflare's universal challenge-page title
  81. # (managed challenge, JS challenge, Under Attack mode). Combined with
  82. # an HTML content-type this is unambiguous — no legitimate ntfy or
  83. # webhook backend returns HTML with that title. ``cf-chl-*`` and
  84. # ``challenge-platform`` cover newer / non-default CF templates.
  85. return "just a moment" in body or "cf-chl-bypass" in body or "cf-chl-opt" in body or "challenge-platform" in body
  86. def _assert_safe_provider_url(url: str, *, label: str) -> str | None:
  87. """Validate a provider URL taken from user-supplied config.
  88. Returns an error message on rejection, or None when the URL is
  89. acceptable — the ``_send_*`` methods return ``tuple[bool, str]`` rather
  90. than raising, so a message is more useful here than an exception.
  91. Uses the LAN-service policy: self-hosting ntfy, Bark, Gotify or a webhook
  92. receiver on the home LAN is normal and must keep working, so loopback and
  93. RFC-1918 stay permitted. Cloud-metadata endpoints, numeric-encoded IPs and
  94. non-HTTP schemes are rejected.
  95. """
  96. from backend.app.api.routes._url_safety import assert_safe_lan_service_url
  97. try:
  98. assert_safe_lan_service_url(url, label=label)
  99. except ValueError as exc:
  100. return str(exc)
  101. return None
  102. def _event_priority_level(config: dict, event_type: str | None) -> int | None:
  103. """The 1-5 priority level mapped to this event in config.event_priorities.
  104. 1=min, 2=low, 3=default, 4=high, 5=urgent, as the dialog writes them for
  105. ntfy (#990) and Gotify (#2743). None when the event has no valid entry.
  106. """
  107. event_priorities = config.get("event_priorities") or {}
  108. if not event_type or not isinstance(event_priorities, dict):
  109. return None
  110. raw = event_priorities.get(event_type)
  111. if raw is None and not event_type.startswith("on_"):
  112. raw = event_priorities.get(f"on_{event_type}")
  113. try:
  114. level = int(raw) if raw is not None else None
  115. except (TypeError, ValueError):
  116. return None
  117. if level is not None and 1 <= level <= 5:
  118. return level
  119. return None
  120. # Gotify priorities run 0-10; its Android app stays silent below 4 and pops
  121. # the notification up from 8. The dialog's five levels map onto those bands.
  122. _GOTIFY_PRIORITY_BY_LEVEL = {1: 0, 2: 2, 3: 5, 4: 8, 5: 10}
  123. def _opaque_http_failure(response: httpx.Response, *, label: str) -> str:
  124. """Failure message for a provider whose destination host the user supplies.
  125. The response body is deliberately **not** returned to the caller. Provider
  126. URLs are configurable by anyone holding ``NOTIFICATIONS_CREATE`` — which
  127. the default Operators group carries and which does not imply
  128. ``SETTINGS_UPDATE`` — and ``POST /notifications/test-config`` accepts a URL
  129. straight from the request body without persisting anything. Echoing the
  130. response body there turned an intended "does my webhook work?" check into
  131. an authenticated read primitive against any host the Bambuddy process can
  132. reach, including services that are not exposed to the network at all.
  133. Providers whose host Bambuddy hardcodes (Pushover, Telegram, CallMeBot)
  134. keep returning the upstream body — there is no trust boundary to cross
  135. when the destination cannot be influenced.
  136. The body is logged at debug level, where it stays available to whoever
  137. already administers the host without being handed back over the API.
  138. """
  139. logger.debug(
  140. "%s delivery failed with HTTP %s; body: %s",
  141. label,
  142. response.status_code,
  143. (response.text or "")[:200],
  144. )
  145. return f"HTTP {response.status_code} from the configured {label} (see server logs at debug level for details)"
  146. _sample_notification_image: bytes | None = None
  147. _sample_notification_image_loaded = False
  148. def _load_sample_notification_image() -> bytes | None:
  149. """Render the bundled app screenshot as a small JPEG.
  150. Stands in for a real camera snapshot on Test Configuration / test-all,
  151. since there's no live print to capture one from. Cached after the first
  152. call so we're not re-reading and resizing the file on every click.
  153. """
  154. global _sample_notification_image, _sample_notification_image_loaded
  155. if _sample_notification_image_loaded:
  156. return _sample_notification_image
  157. _sample_notification_image_loaded = True
  158. try:
  159. from io import BytesIO
  160. from PIL import Image
  161. from backend.app.core.config import settings
  162. source_path = settings.static_dir / "img" / "screenshot-desktop.png"
  163. with Image.open(source_path) as img:
  164. resized = img.convert("RGB")
  165. resized.thumbnail((640, 640))
  166. buffer = BytesIO()
  167. resized.save(buffer, format="JPEG", quality=85)
  168. _sample_notification_image = buffer.getvalue()
  169. except Exception as e:
  170. logger.warning("Failed to build sample notification image: %s", e)
  171. _sample_notification_image = None
  172. return _sample_notification_image
  173. class NotificationService:
  174. """Service for sending notifications through various providers."""
  175. def __init__(self):
  176. self._http_client: httpx.AsyncClient | None = None
  177. self._template_cache: dict[str, NotificationTemplate] = {}
  178. self._digest_scheduler_task: asyncio.Task | None = None
  179. self._last_digest_check: str = "" # "HH:MM" to avoid duplicate checks
  180. async def _get_client(self) -> httpx.AsyncClient:
  181. """Get or create HTTP client.
  182. The connect timeout is deliberately far shorter than the rest. A flat
  183. 30 s meant that when a site's internet went down, every alarm spent a
  184. full 30 s inside ``connect`` — longer than SQLite's 15 s
  185. ``busy_timeout`` — and any other task that wanted to write during that
  186. window failed with "database is locked" (#2770). Reaching a host either
  187. works in a couple of seconds or is not going to; sending the body is the
  188. part that legitimately takes time, so read/write keep the old 30 s and
  189. an image upload on a slow uplink is unaffected.
  190. """
  191. if self._http_client is None or self._http_client.is_closed:
  192. self._http_client = httpx.AsyncClient(
  193. timeout=httpx.Timeout(30.0, connect=5.0),
  194. headers={"User-Agent": _USER_AGENT},
  195. )
  196. return self._http_client
  197. async def close(self):
  198. """Close HTTP client."""
  199. if self._http_client and not self._http_client.is_closed:
  200. await self._http_client.aclose()
  201. def _is_in_quiet_hours(self, provider: NotificationProvider) -> bool:
  202. """Check if current time is within provider's quiet hours."""
  203. if not provider.quiet_hours_enabled:
  204. return False
  205. if not provider.quiet_hours_start or not provider.quiet_hours_end:
  206. return False
  207. try:
  208. now = datetime.now()
  209. current_time = now.hour * 60 + now.minute
  210. start_parts = provider.quiet_hours_start.split(":")
  211. end_parts = provider.quiet_hours_end.split(":")
  212. start_minutes = int(start_parts[0]) * 60 + int(start_parts[1])
  213. end_minutes = int(end_parts[0]) * 60 + int(end_parts[1])
  214. # Handle overnight quiet hours (e.g., 22:00 to 07:00)
  215. if start_minutes > end_minutes:
  216. # Quiet hours span midnight
  217. return current_time >= start_minutes or current_time < end_minutes
  218. else:
  219. # Same day quiet hours
  220. return start_minutes <= current_time < end_minutes
  221. except (ValueError, TypeError, AttributeError):
  222. logger.warning("Invalid quiet hours format for provider %s", provider.name)
  223. return False
  224. async def _get_template(self, db: AsyncSession, event_type: str) -> NotificationTemplate | None:
  225. """Get a notification template by event type.
  226. ``no_autoflush`` for the same reason as ``_get_providers_for_event``:
  227. this read runs before the provider is contacted, and must not be the
  228. thing that opens a write transaction on the caller's session (#2770).
  229. """
  230. # Check cache first
  231. if event_type in self._template_cache:
  232. return self._template_cache[event_type]
  233. with db.no_autoflush:
  234. result = await db.execute(select(NotificationTemplate).where(NotificationTemplate.event_type == event_type))
  235. template = result.scalar_one_or_none()
  236. if template:
  237. self._template_cache[event_type] = template
  238. return template
  239. def _render_template(self, template_str: str, variables: dict[str, Any]) -> str:
  240. """Render a template string with variables. Missing variables become empty."""
  241. result = template_str
  242. for key, value in variables.items():
  243. result = result.replace("{" + key + "}", str(value) if value is not None else "")
  244. # Remove any remaining unreplaced placeholders
  245. result = re.sub(r"\{[a-z_]+\}", "", result)
  246. return result
  247. async def _format_eta(self, seconds: int | None, db: AsyncSession) -> str:
  248. """Format ETA as wall-clock time, respecting user's time_format setting."""
  249. if not seconds or seconds <= 0:
  250. return "Unknown"
  251. from backend.app.api.routes.settings import get_setting
  252. eta_time = datetime.now() + timedelta(seconds=seconds)
  253. time_format = await get_setting(db, "time_format")
  254. if time_format == "12h":
  255. return eta_time.strftime("%I:%M %p").lstrip("0")
  256. # Default to 24h for "24h", "system", or unset
  257. return eta_time.strftime("%H:%M")
  258. def _format_duration(self, seconds: int | None) -> str:
  259. """Format duration in seconds to human-readable string."""
  260. if seconds is None:
  261. return "Unknown"
  262. hours = seconds // 3600
  263. minutes = (seconds % 3600) // 60
  264. if hours > 0:
  265. return f"{hours}h {minutes}m"
  266. return f"{minutes}m"
  267. def _clean_filename(self, filename: str) -> str:
  268. """Extract filename and remove file extensions."""
  269. import os
  270. # Strip path prefix (e.g., /data/Metadata/plate_5.gcode -> plate_5.gcode)
  271. filename = os.path.basename(filename)
  272. # Remove common extensions
  273. if filename.endswith(".gcode.3mf"):
  274. return filename[:-10]
  275. elif filename.endswith(".gcode"):
  276. return filename[:-6]
  277. elif filename.endswith(".3mf"):
  278. return filename[:-4]
  279. return filename
  280. async def _build_message_from_template(
  281. self, db: AsyncSession, event_type: str, variables: dict[str, Any]
  282. ) -> tuple[str, str]:
  283. """Build notification title and body from template."""
  284. # Add common variables
  285. variables["timestamp"] = datetime.now().strftime("%Y-%m-%d %H:%M")
  286. variables["app_name"] = "Bambuddy"
  287. template = await self._get_template(db, event_type)
  288. if not template:
  289. # Fallback to simple message
  290. logger.warning("Template not found for event type: %s", event_type)
  291. return event_type.replace("_", " ").title(), str(variables)
  292. title = self._render_template(template.title_template, variables)
  293. body = self._render_template(template.body_template, variables)
  294. return title, body
  295. async def send_test_notification(
  296. self,
  297. provider_type: str,
  298. config: dict[str, Any],
  299. db: AsyncSession | None = None,
  300. attach_photo: bool = True,
  301. ) -> tuple[bool, str]:
  302. """Send a test notification to verify configuration.
  303. attach_photo mirrors the provider's own toggle (see _send_to_provider)
  304. — when set, a bundled sample image stands in for a real snapshot.
  305. """
  306. success, message = await self._send_test_notification(provider_type, config, db, attach_photo)
  307. # The text goes back to the caller: an HTTP client refusing a header
  308. # quotes its value, which may be a credential the caller never sees.
  309. return success, redact_refused_headers(message) or message
  310. async def _send_test_notification(
  311. self,
  312. provider_type: str,
  313. config: dict[str, Any],
  314. db: AsyncSession | None,
  315. attach_photo: bool,
  316. ) -> tuple[bool, str]:
  317. if db:
  318. title, message = await self._build_message_from_template(db, "test", {})
  319. else:
  320. title = "Bambuddy Test"
  321. message = "This is a test notification. If you see this, notifications are working!"
  322. if provider_type == "notify" and not notify_supports_photos(config.get("device_id")):
  323. attach_photo = False
  324. image_data = await asyncio.to_thread(_load_sample_notification_image) if attach_photo else None
  325. try:
  326. if provider_type == "callmebot":
  327. return await self._send_callmebot(config, f"{title}\n{message}")
  328. elif provider_type == "ntfy":
  329. return await self._send_ntfy(config, title, message, image_data=image_data)
  330. elif provider_type == "pushover":
  331. return await self._send_pushover(config, title, message, image_data=image_data)
  332. elif provider_type == "telegram":
  333. return await self._send_telegram(config, f"*{title}*\n{message}", image_data=image_data)
  334. elif provider_type == "email":
  335. return await self._send_email(config, title, message, image_data=image_data)
  336. elif provider_type == "discord":
  337. return await self._send_discord(config, title, message, image_data=image_data)
  338. elif provider_type == "webhook":
  339. photo_url = None
  340. if attach_photo and (config.get("payload_format") or "generic").strip() == "slack":
  341. photo_url = await self._get_or_build_photo_url(db, image_data, "test")
  342. return await self._send_webhook(
  343. config, title, message, image_data=image_data, event_type="test", image_url=photo_url
  344. )
  345. elif provider_type == "homeassistant":
  346. photo_url = await self._get_or_build_photo_url(db, image_data, "test") if attach_photo else None
  347. return await self._send_homeassistant(config, title, message, db=db, image_url=photo_url)
  348. elif provider_type == "bark":
  349. photo_url = await self._get_or_build_photo_url(db, image_data, "test") if attach_photo else None
  350. return await self._send_bark(config, title, message, image_url=photo_url)
  351. elif provider_type == "gotify":
  352. photo_url = await self._get_or_build_photo_url(db, image_data, "test") if attach_photo else None
  353. return await self._send_gotify(config, title, message, event_type="test", image_url=photo_url)
  354. elif provider_type == "notify":
  355. photo_url = await self._get_or_build_photo_url(db, image_data, "test") if attach_photo else None
  356. return await self._send_notify(config, title, message, image_url=photo_url)
  357. else:
  358. return False, f"Unknown provider type: {provider_type}"
  359. except Exception as e:
  360. logger.exception("Error sending test notification via %s", provider_type)
  361. return False, str(e)
  362. async def _send_notify(
  363. self,
  364. config: dict,
  365. title: str,
  366. message: str,
  367. image_url: str | None = None,
  368. event_type: str | None = None,
  369. printer_id: int | None = None,
  370. ) -> tuple[bool, str]:
  371. """Ordinary Notify! alerts use the existing event, digest and quiet-hours rules."""
  372. try:
  373. device_id, token = notify_credentials(config)
  374. client = NotifyClient(await self._get_client())
  375. group_type = str(config.get("group_type") or "").strip()
  376. if not group_type:
  377. group_type = f"bambuddy-printer-{printer_id}" if printer_id is not None else "bambuddy"
  378. await client.send_notification(
  379. device_id,
  380. token,
  381. title=title,
  382. text=message,
  383. group_type=group_type,
  384. icon_url=config.get("icon_url"),
  385. image_url=image_url,
  386. time_sensitive=config.get("time_sensitive") is True
  387. and event_type in {"print_failed", "print_stopped", "printer_error", "ai_failure_detection"},
  388. )
  389. return True, "Message sent successfully"
  390. except NotifyError as exc:
  391. return False, str(exc)
  392. async def _send_callmebot(self, config: dict, message: str) -> tuple[bool, str]:
  393. """Send notification via CallMeBot (WhatsApp)."""
  394. phone = config.get("phone", "").strip()
  395. apikey = config.get("apikey", "").strip()
  396. if not phone or not apikey:
  397. return False, "Phone number and API key are required"
  398. # Every value URL-encoded: a phone number written "+49 151 ..." would
  399. # otherwise lose its "+", which a query string reads as a space.
  400. url = (
  401. "https://api.callmebot.com/whatsapp.php"
  402. f"?phone={quote(phone, safe='')}&text={quote(message, safe='')}&apikey={quote(apikey, safe='')}"
  403. )
  404. client = await self._get_client()
  405. response = await client.get(url)
  406. if response.status_code == 200:
  407. return True, "Message sent successfully"
  408. else:
  409. return False, f"HTTP {response.status_code}: {response.text[:200]}"
  410. async def _send_bark(
  411. self, config: dict, title: str, message: str, url: str | None = None, image_url: str | None = None
  412. ) -> tuple[bool, str]:
  413. """Send notification via Bark, the self-hostable iOS push service (#1495).
  414. POSTs JSON to {server}/push. Defaults to the official api.day.app
  415. relay; a self-hosted bark-server works by overriding the server URL.
  416. ``url`` opens on tap — the outcome confirmation (#1898) deep-links
  417. into the archive's confirmation dialog with it.
  418. """
  419. server = (config.get("server") or "https://api.day.app").strip().rstrip("/")
  420. device_key = (config.get("device_key") or "").strip()
  421. if not device_key:
  422. return False, "Device key is required"
  423. url_error = _assert_safe_provider_url(server, label="Bark server URL")
  424. if url_error:
  425. return False, url_error
  426. payload: dict[str, Any] = {
  427. "device_key": device_key,
  428. "title": title,
  429. "body": message,
  430. }
  431. group = (config.get("group") or "").strip()
  432. if group:
  433. payload["group"] = group
  434. sound = (config.get("sound") or "").strip()
  435. if sound:
  436. payload["sound"] = sound
  437. level = (config.get("level") or "").strip()
  438. if level in ("active", "timeSensitive", "critical", "passive"):
  439. payload["level"] = level
  440. if url:
  441. payload["url"] = url
  442. if image_url:
  443. # Closest thing Bark has to a photo attachment — shows as a
  444. # round icon on iOS.
  445. payload["icon"] = image_url
  446. client = await self._get_client()
  447. response = await client.post(f"{server}/push", json=payload)
  448. if response.status_code == 200:
  449. # bark-server can report failures inside an HTTP 200 body
  450. # ({"code": 400, "message": ...}), so the status alone isn't proof.
  451. try:
  452. body = response.json()
  453. except ValueError:
  454. body = None
  455. if isinstance(body, dict) and body.get("code") not in (200, None):
  456. # Only the numeric code is echoed. A server chosen by the caller
  457. # controls this body too, so the free-text message is a (narrow)
  458. # read channel of the same kind _opaque_http_failure closes.
  459. logger.debug("Bark reported error %s: %s", body.get("code"), str(body.get("message"))[:200])
  460. return False, f"Bark error {body.get('code')} (see server logs at debug level for details)"
  461. return True, "Message sent successfully"
  462. return False, _opaque_http_failure(response, label="Bark server")
  463. async def _send_gotify(
  464. self,
  465. config: dict,
  466. title: str,
  467. message: str,
  468. event_type: str | None = None,
  469. url: str | None = None,
  470. image_url: str | None = None,
  471. ) -> tuple[bool, str]:
  472. """Send notification via a self-hosted Gotify server (#2743).
  473. POSTs JSON to {server}/message with the application token in the
  474. X-Gotify-Key header, so the token never ends up in a URL or a log line.
  475. Gotify takes no uploads: a photo goes in as a URL its Android app
  476. fetches itself, and ``url`` opens on tap.
  477. """
  478. server = (config.get("server") or "").strip().rstrip("/")
  479. # Gotify's own docs show the full endpoint, so a pasted ".../message"
  480. # must not turn into ".../message/message".
  481. server = server.removesuffix("/message")
  482. app_token = (config.get("app_token") or "").strip()
  483. if not server or not app_token:
  484. return False, "Server URL and app token are required"
  485. url_error = _assert_safe_provider_url(server, label="Gotify server URL")
  486. if url_error:
  487. return False, url_error
  488. # Events without a mapped level go out at Gotify's middle band (5), the
  489. # "Default" the dialog shows for them -- sound on, no pop-up.
  490. level = _event_priority_level(config, event_type) or 3
  491. payload: dict[str, Any] = {
  492. "title": title,
  493. "message": message,
  494. "priority": _GOTIFY_PRIORITY_BY_LEVEL[level],
  495. }
  496. notification_extras: dict[str, Any] = {}
  497. if url:
  498. notification_extras["click"] = {"url": url}
  499. if image_url:
  500. notification_extras["bigImageUrl"] = image_url
  501. if notification_extras:
  502. payload["extras"] = {"client::notification": notification_extras}
  503. client = await self._get_client()
  504. response = await client.post(f"{server}/message", json=payload, headers={"X-Gotify-Key": app_token})
  505. if response.status_code == 200:
  506. return True, "Message sent successfully"
  507. return False, _opaque_http_failure(response, label="Gotify server")
  508. async def _send_ntfy(
  509. self,
  510. config: dict,
  511. title: str,
  512. message: str,
  513. image_data: bytes | None = None,
  514. event_type: str | None = None,
  515. actions: str | None = None,
  516. ) -> tuple[bool, str]:
  517. """Send notification via ntfy.
  518. ``actions`` is a pre-built value for ntfy's Actions header (simple
  519. format), used by the outcome-confirmation event (#1898) to put
  520. one-tap Good/Reject buttons directly into the push notification.
  521. """
  522. server = config.get("server", "https://ntfy.sh").rstrip("/")
  523. topic = config.get("topic", "").strip()
  524. auth_token = config.get("auth_token", "").strip()
  525. if not topic:
  526. return False, "Topic is required"
  527. url_error = _assert_safe_provider_url(server, label="ntfy server URL")
  528. if url_error:
  529. return False, url_error
  530. url = f"{server}/{topic}"
  531. # ntfy reads Title/Message from HTTP headers. httpx enforces ASCII
  532. # for str header values, but printer names and filenames can contain
  533. # non-ASCII characters (e.g. accented letters, CJK). Passing bytes
  534. # bypasses the ASCII check — ntfy handles UTF-8 headers correctly.
  535. headers: dict[str, str | bytes] = {"Title": title.encode("utf-8")}
  536. # Per-event Priority header (#990). Only set when the user has
  537. # explicitly mapped this event to a 1-5 value; otherwise fall through
  538. # to the ntfy server's default so existing setups stay unchanged.
  539. #
  540. # The map is keyed by the provider's toggle column ("on_print_failed"),
  541. # because that is what the dialog builds its rows from -- but every
  542. # sender is called with the bare event name ("print_failed"), so the
  543. # lookup used to miss for every real notification and hit only in tests
  544. # that called this method with the prefixed name (issue #3139). Both
  545. # spellings are accepted, which also leaves stored configs untouched.
  546. priority = _event_priority_level(config, event_type)
  547. if priority is not None:
  548. headers["Priority"] = str(priority)
  549. if auth_token:
  550. headers["Authorization"] = f"Bearer {auth_token}"
  551. if actions:
  552. headers["Actions"] = actions
  553. client = await self._get_client()
  554. if image_data:
  555. # ntfy supports image attachments via multipart form-data.
  556. # HTTP headers cannot contain newlines, but ntfy interprets
  557. # literal \n (backslash-n) as newlines in the Message header.
  558. headers["Filename"] = "photo.jpg"
  559. headers["Message"] = message.replace("\n", "\\n").encode("utf-8")
  560. response = await client.put(url, content=image_data, headers=headers)
  561. if response.status_code == 400 and "attachments not allowed" in response.text:
  562. # Server has attachments disabled — retry without the image
  563. headers.pop("Filename", None)
  564. headers.pop("Message", None)
  565. response = await client.post(url, content=message.encode("utf-8"), headers=headers)
  566. else:
  567. response = await client.post(url, content=message.encode("utf-8"), headers=headers)
  568. if response.status_code in (200, 204):
  569. return True, "Message sent successfully"
  570. if _looks_like_cloudflare_challenge(response):
  571. return False, (
  572. f"HTTP {response.status_code} — ntfy server is behind a Cloudflare "
  573. "challenge. Bambuddy was served the JS challenge page instead of "
  574. "reaching ntfy. Cloudflare cannot be solved from a backend; add a "
  575. "Cloudflare security-skip rule for this hostname, disable Bot "
  576. "Fight Mode, or front the server with Cloudflare Access using a "
  577. "service token. (#1534)"
  578. )
  579. return False, _opaque_http_failure(response, label="ntfy server")
  580. async def _send_pushover(
  581. self,
  582. config: dict,
  583. title: str,
  584. message: str,
  585. image_data: bytes | None = None,
  586. url: str | None = None,
  587. url_title: str | None = None,
  588. ) -> tuple[bool, str]:
  589. """Send notification via Pushover.
  590. Args:
  591. config: Provider configuration with user_key, app_token, priority
  592. title: Notification title
  593. message: Notification body
  594. image_data: Optional JPEG image bytes to attach (max 2.5MB)
  595. url: Optional supplementary URL shown under the message
  596. url_title: Optional label for that URL
  597. """
  598. user_key = config.get("user_key", "").strip()
  599. app_token = config.get("app_token", "").strip()
  600. try:
  601. priority = int(config.get("priority", 0))
  602. except (TypeError, ValueError):
  603. priority = 0
  604. if not user_key or not app_token:
  605. return False, "User key and app token are required"
  606. api_url = "https://api.pushover.net/1/messages.json"
  607. data = {
  608. "token": app_token,
  609. "user": user_key,
  610. "title": title,
  611. "message": message,
  612. "priority": priority,
  613. }
  614. if url:
  615. data["url"] = url
  616. if url_title:
  617. data["url_title"] = url_title
  618. # Emergency priority (2) keeps re-alerting until acknowledged, so
  619. # Pushover *requires* retry (how often, >= 30s) and expire (when to
  620. # give up, <= 10800s). Without them the API rejects the message. Only
  621. # send them at priority 2 — Pushover ignores them at other priorities.
  622. if priority == 2:
  623. try:
  624. retry = int(config.get("retry", 60))
  625. except (TypeError, ValueError):
  626. retry = 60
  627. try:
  628. expire = int(config.get("expire", 3600))
  629. except (TypeError, ValueError):
  630. expire = 3600
  631. data["retry"] = max(30, min(retry, 10800))
  632. data["expire"] = max(30, min(expire, 10800))
  633. client = await self._get_client()
  634. if image_data:
  635. # Pushover supports image attachments via multipart form-data
  636. files = {"attachment": ("photo.jpg", image_data, "image/jpeg")}
  637. response = await client.post(api_url, data=data, files=files)
  638. else:
  639. response = await client.post(api_url, data=data)
  640. if response.status_code == 200:
  641. return True, "Message sent successfully"
  642. else:
  643. try:
  644. error_data = response.json()
  645. errors = error_data.get("errors", [])
  646. return False, f"Pushover error: {', '.join(errors)}"
  647. except Exception:
  648. return False, f"HTTP {response.status_code}: {response.text[:200]}"
  649. async def _send_telegram(
  650. self,
  651. config: dict,
  652. message: str,
  653. image_data: bytes | None = None,
  654. buttons: list[dict] | None = None,
  655. link_preview: bool = True,
  656. ) -> tuple[bool, str]:
  657. """Send notification via Telegram bot.
  658. ``buttons`` is one row of inline URL buttons (``{"text", "url"}``
  659. entries), used by the outcome-confirmation event (#1898) to put
  660. one-tap Good/Reject under the message. ``link_preview=False`` asks
  661. Telegram not to fetch the first URL in the text for a preview card.
  662. """
  663. ok, status, _ = await self._send_telegram_message(
  664. config, message, image_data=image_data, buttons=buttons, link_preview=link_preview
  665. )
  666. return ok, status
  667. async def _send_telegram_message(
  668. self,
  669. config: dict,
  670. message: str,
  671. image_data: bytes | None = None,
  672. buttons: list[dict] | None = None,
  673. link_preview: bool = True,
  674. ) -> tuple[bool, str, dict | None]:
  675. """Send via Telegram and also hand back the Bot API ``result`` (the sent Message).
  676. The outcome confirmation in reaction mode (#3046) needs the
  677. ``message_id`` from it to recognise the reaction later; every other
  678. caller goes through ``_send_telegram`` and ignores it.
  679. """
  680. bot_token = config.get("bot_token", "").strip()
  681. chat_id = config.get("chat_id", "").strip()
  682. if not bot_token or not chat_id:
  683. return False, "Bot token and chat ID are required", None
  684. # Optional forum topic (#1518). Telegram expects message_thread_id as an
  685. # integer in the JSON sendMessage body — a string 400s there even though
  686. # the multipart sendPhoto call below would happily accept one. Coerce it
  687. # once, up front, so both call sites agree and a bad value fails loudly
  688. # instead of silently breaking only the text notifications.
  689. thread_id_raw = str(config.get("message_thread_id") or "").strip()
  690. message_thread_id: int | None = None
  691. if thread_id_raw:
  692. try:
  693. message_thread_id = int(thread_id_raw)
  694. except ValueError:
  695. return False, f"Invalid message thread ID: {thread_id_raw!r} is not a number", None
  696. message = telegram_markdown_escape(message)
  697. client = await self._get_client()
  698. async def _post(with_buttons: bool):
  699. if image_data:
  700. # Use sendPhoto to attach the thumbnail with the caption
  701. url = f"https://api.telegram.org/bot{bot_token}/sendPhoto"
  702. form: dict[str, Any] = {"chat_id": chat_id, "caption": message, "parse_mode": "Markdown"}
  703. if message_thread_id is not None:
  704. form["message_thread_id"] = message_thread_id
  705. if with_buttons:
  706. # Multipart form fields are strings — reply_markup goes JSON-encoded.
  707. form["reply_markup"] = json.dumps({"inline_keyboard": [buttons]})
  708. return await client.post(
  709. url,
  710. data=form,
  711. files={"photo": ("photo.jpg", image_data, "image/jpeg")},
  712. )
  713. url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
  714. payload: dict[str, Any] = {
  715. "chat_id": chat_id,
  716. "text": message,
  717. "parse_mode": "Markdown",
  718. }
  719. if not link_preview:
  720. payload["disable_web_page_preview"] = True
  721. if message_thread_id is not None:
  722. payload["message_thread_id"] = message_thread_id
  723. if with_buttons:
  724. payload["reply_markup"] = {"inline_keyboard": [buttons]}
  725. return await client.post(url, json=payload)
  726. def _failure(resp) -> str | None:
  727. """What Telegram objected to, or None when the send went through."""
  728. if resp.status_code != 200:
  729. return f"HTTP {resp.status_code}: {resp.text[:200]}"
  730. result = resp.json()
  731. if result.get("ok"):
  732. return None
  733. return f"Telegram error: {result.get('description', 'Unknown error')}"
  734. response = await _post(bool(buttons))
  735. failure = _failure(response)
  736. if failure and buttons:
  737. # Telegram validates every inline-keyboard URL and refuses the whole
  738. # send when one of them is not a URL it accepts — which is what an
  739. # install without a public external_url produces for the #1898
  740. # verdict links. Dropping the buttons is survivable; dropping the
  741. # message the user is waiting for is not.
  742. logger.warning("Telegram refused the message with inline buttons (%s); retrying without them", failure)
  743. response = await _post(False)
  744. failure = _failure(response)
  745. if failure:
  746. return False, failure, None
  747. sent = response.json().get("result")
  748. return True, "Message sent successfully", sent if isinstance(sent, dict) else None
  749. async def _send_telegram_confirm_request(
  750. self,
  751. provider: NotificationProvider,
  752. config: dict,
  753. message: str,
  754. db: AsyncSession | None,
  755. image_data: bytes | None,
  756. buttons: list[dict] | None,
  757. archive_id: int | None,
  758. *,
  759. run_id: int | None = None,
  760. ) -> tuple[bool, str]:
  761. """Deliver the outcome prompt the way the provider's verdict mode asks (#3046).
  762. "buttons" is the plain #1898 delivery. In "reactions" the inline
  763. keyboard is dropped and the user answers with a thumbs-up/down on the
  764. message itself; "both" keeps the keyboard as well. In either of those
  765. the sent message is remembered in telegram_pending_verdicts, together
  766. with the confirm token its links carry, so the reaction poller can map
  767. the reaction back to the archive for as long as that token is live.
  768. """
  769. mode = provider.telegram_verdict_mode or "buttons"
  770. # Telegram's servers GET the first URL in the text to build a preview
  771. # card. An outcome prompt whose edited body still carries {good_url}
  772. # would have that fetch answer the question before the operator saw
  773. # it, so the preview is off for the prompt in every mode.
  774. if mode == "buttons":
  775. return await self._send_telegram(
  776. config, message, image_data=image_data, buttons=buttons, link_preview=False
  777. )
  778. if mode == "reactions":
  779. buttons = None
  780. message = f"{message}\n\n{TELEGRAM_REACTION_HINT}"
  781. ok, status, sent = await self._send_telegram_message(
  782. config, message, image_data=image_data, buttons=buttons, link_preview=False
  783. )
  784. if not ok:
  785. return ok, status
  786. message_id = (sent or {}).get("message_id")
  787. if not isinstance(message_id, int) or message_id <= 0:
  788. logger.warning("Telegram did not return a message_id for the outcome prompt; reactions cannot be matched")
  789. return ok, status
  790. if db is None or archive_id is None:
  791. return ok, status
  792. try:
  793. # dispatch_outcome_confirmation mints a live token right before
  794. # sending. Without one the prompt's links are dead too, and a
  795. # reaction must not outlive them, so there is nothing to remember.
  796. # The run's token when the prompt is for a run, the archive's when
  797. # it was sent the way it was before runs had their own.
  798. archive = await db.get(PrintArchive, archive_id)
  799. holder = archive
  800. if run_id is not None:
  801. from backend.app.models.print_log import PrintLogEntry
  802. holder = await db.get(PrintLogEntry, run_id)
  803. if (
  804. archive is None
  805. or holder is None
  806. or not holder.confirm_token
  807. or holder.confirm_token_used_at is not None
  808. ):
  809. logger.warning("Outcome prompt for archive %s went out without a live confirm token", archive_id)
  810. return ok, status
  811. db.add(
  812. TelegramPendingVerdict(
  813. provider_id=provider.id,
  814. chat_id=str(((sent or {}).get("chat") or {}).get("id") or config.get("chat_id", "")).strip(),
  815. message_id=message_id,
  816. archive_id=archive_id,
  817. confirm_token=holder.confirm_token,
  818. has_caption=image_data is not None,
  819. message_text=message,
  820. )
  821. )
  822. await db.commit()
  823. except Exception as e:
  824. # The prompt went out; losing the reaction mapping is a degraded
  825. # outcome, not a failed notification.
  826. logger.warning("Failed to record the Telegram outcome prompt for reactions: %s", e)
  827. await db.rollback()
  828. return ok, status
  829. async def _send_email(
  830. self,
  831. config: dict,
  832. subject: str,
  833. body: str,
  834. image_data: bytes | None = None,
  835. finish_photo_url: str | None = None,
  836. ) -> tuple[bool, str]:
  837. """Send notification via email (SMTP).
  838. Inline finish-photo embed is opt-in via the template: when the rendered
  839. ``body`` contains the substituted ``{finish_photo_url}`` value AND the
  840. finish-photo bytes are present, the message is built as
  841. ``multipart/related`` wrapping a ``multipart/alternative`` (plain + HTML)
  842. plus an inline ``MIMEImage`` with ``Content-ID: <bambuddy-finish-photo>``.
  843. The HTML part replaces the URL with ``<img src="cid:...">``; the plain-
  844. text part keeps the URL as a clickable link. When the template doesn't
  845. reference ``{finish_photo_url}`` (or image bytes aren't available), the
  846. original single-part text shape is used — no attachment, no surprise
  847. inline image (#1792).
  848. """
  849. smtp_server = config.get("smtp_server", "").strip()
  850. smtp_port = int(config.get("smtp_port", 587))
  851. username = config.get("username", "").strip()
  852. password = config.get("password", "").strip()
  853. from_email = config.get("from_email", "").strip()
  854. to_email = config.get("to_email", "").strip()
  855. # Security: "starttls" (port 587), "ssl" (port 465), "none" (port 25)
  856. security = config.get("security", "starttls")
  857. # Authentication: "true" or "false"
  858. auth_enabled = config.get("auth_enabled", "true").lower() == "true"
  859. if not all([smtp_server, from_email, to_email]):
  860. return False, "SMTP server, from email, and to email are required"
  861. if auth_enabled and not all([username, password]):
  862. return False, "Username and password are required when authentication is enabled"
  863. # Template-driven: only inline-embed when the user's template explicitly
  864. # referenced {finish_photo_url} (so the URL appears in the rendered body)
  865. # AND the photo bytes are available. Falls back to text-only otherwise.
  866. inline_photo = bool(image_data and finish_photo_url and finish_photo_url in body)
  867. try:
  868. if inline_photo:
  869. # multipart/related → (multipart/alternative → text, html) + inline image
  870. msg = MIMEMultipart("related")
  871. msg["From"] = from_email
  872. msg["To"] = to_email
  873. msg["Subject"] = f"[Bambuddy] {subject}"
  874. alt = MIMEMultipart("alternative")
  875. alt.attach(MIMEText(body, "plain"))
  876. # Build HTML body: escape the rendered body, then swap the
  877. # escaped URL substring for an inline <img> referencing the
  878. # MIMEImage we attach below. Done AFTER escape so the cid: URL
  879. # we inject isn't re-escaped.
  880. escaped_body = html.escape(body).replace("\n", "<br>\n")
  881. escaped_url = html.escape(finish_photo_url)
  882. img_tag = (
  883. '<img src="cid:bambuddy-finish-photo" '
  884. 'alt="Printer camera snapshot" '
  885. 'style="max-width:100%;height:auto;border:1px solid #ddd;border-radius:4px;">'
  886. )
  887. html_body = f"<html><body><p>{escaped_body.replace(escaped_url, img_tag)}</p></body></html>"
  888. alt.attach(MIMEText(html_body, "html"))
  889. msg.attach(alt)
  890. img = MIMEImage(image_data, _subtype="jpeg")
  891. # Angle-bracketed Content-ID per RFC 2392, referenced from HTML
  892. # without the brackets via ``cid:bambuddy-finish-photo``.
  893. img.add_header("Content-ID", "<bambuddy-finish-photo>")
  894. img.add_header("Content-Disposition", "inline", filename="finish-photo.jpg")
  895. msg.attach(img)
  896. else:
  897. msg = MIMEMultipart()
  898. msg["From"] = from_email
  899. msg["To"] = to_email
  900. msg["Subject"] = f"[Bambuddy] {subject}"
  901. msg.attach(MIMEText(body, "plain"))
  902. add_required_headers(msg, from_email)
  903. # smtplib is synchronous and blocking: a wedged / greylisting /
  904. # firewall-dropped relay leaves recv() stuck. Two problems, two
  905. # fixes (#2572):
  906. # 1. No timeout — smtplib defaults to the global socket timeout,
  907. # which this app never sets, so a stuck relay blocks forever.
  908. # Pass an explicit timeout to every connect.
  909. # 2. Run on the event loop — a stuck (or merely slow) send freezes
  910. # every other coroutine, including a DB session a caller is
  911. # holding open across this notification. Offload to a worker
  912. # thread so the loop stays live and the connection is released
  913. # on schedule.
  914. smtp_timeout = 30.0
  915. msg_str = msg.as_string()
  916. def _blocking_send() -> None:
  917. if security == "ssl":
  918. # Direct SSL connection (typically port 465)
  919. server = smtplib.SMTP_SSL(smtp_server, smtp_port, timeout=smtp_timeout)
  920. elif security == "starttls":
  921. # STARTTLS upgrade (typically port 587)
  922. server = smtplib.SMTP(smtp_server, smtp_port, timeout=smtp_timeout)
  923. server.starttls()
  924. else:
  925. # No encryption (typically port 25) - use with caution
  926. server = smtplib.SMTP(smtp_server, smtp_port, timeout=smtp_timeout)
  927. try:
  928. if auth_enabled:
  929. server.login(username, password)
  930. server.sendmail(from_email, to_email, msg_str)
  931. finally:
  932. # quit() in finally so a send error doesn't leak the socket.
  933. try:
  934. server.quit()
  935. except Exception: # noqa: BLE001 — closing a broken connection is best-effort
  936. pass
  937. await asyncio.to_thread(_blocking_send)
  938. return True, "Email sent successfully"
  939. except smtplib.SMTPAuthenticationError:
  940. return False, "SMTP authentication failed - check username/password"
  941. except smtplib.SMTPException as e:
  942. return False, f"SMTP error: {str(e)}"
  943. except Exception as e:
  944. return False, f"Email error: {str(e)}"
  945. async def _send_discord(
  946. self, config: dict, title: str, message: str, image_data: bytes | None = None
  947. ) -> tuple[bool, str]:
  948. """Send notification via Discord webhook."""
  949. webhook_url = config.get("webhook_url", "").strip()
  950. if not webhook_url:
  951. return False, "Webhook URL is required"
  952. if not (
  953. webhook_url.startswith("https://discord.com/api/webhooks/")
  954. or webhook_url.startswith("https://discordapp.com/api/webhooks/")
  955. ):
  956. return False, "Invalid Discord webhook URL"
  957. # Discord embed format for nicer messages
  958. embed = {
  959. "title": title,
  960. "description": message,
  961. "color": 0x00AE42, # Bambu green
  962. }
  963. client = await self._get_client()
  964. if image_data:
  965. # Attach image via multipart form-data and reference in embed
  966. embed["image"] = {"url": "attachment://photo.jpg"}
  967. payload = {"embeds": [embed]}
  968. response = await client.post(
  969. webhook_url,
  970. data={"payload_json": json.dumps(payload)},
  971. files={"files[0]": ("photo.jpg", image_data, "image/jpeg")},
  972. )
  973. else:
  974. response = await client.post(webhook_url, json={"embeds": [embed]})
  975. if response.status_code in (200, 204):
  976. return True, "Message sent successfully"
  977. else:
  978. return False, f"HTTP {response.status_code}: {response.text[:200]}"
  979. async def _send_webhook(
  980. self,
  981. config: dict,
  982. title: str,
  983. message: str,
  984. image_data: bytes | None = None,
  985. event_type: str | None = None,
  986. variables: dict | None = None,
  987. image_url: str | None = None,
  988. ) -> tuple[bool, str]:
  989. """Send notification via generic webhook (POST JSON).
  990. Supports two payload formats:
  991. - generic: Custom field names with timestamp/source metadata + structured event data
  992. - slack: Slack/Mattermost compatible format (just {"text": "..."})
  993. """
  994. webhook_url = config.get("webhook_url", "").strip()
  995. auth_header = config.get("auth_header", "").strip()
  996. payload_format = config.get("payload_format", "generic").strip()
  997. if not webhook_url:
  998. return False, "Webhook URL is required"
  999. url_error = _assert_safe_provider_url(webhook_url, label="Webhook URL")
  1000. if url_error:
  1001. return False, url_error
  1002. # Build payload based on format
  1003. if payload_format == "slack":
  1004. # Incoming webhooks can't take a byte upload, so a photo has to
  1005. # be a URL Slack/Mattermost fetch themselves.
  1006. data = {"text": f"*{title}*\n{message}"}
  1007. if event_type == "print_confirm_request":
  1008. # Slack and Mattermost fetch every URL in the text to build
  1009. # preview cards, and the outcome prompt (#1898) is the one
  1010. # message whose links are single-use capabilities — that fetch
  1011. # would be a machine answering the operator's question. Off
  1012. # here for the same reason Telegram's preview is.
  1013. #
  1014. # Only here: the slack payload never attaches image bytes (the
  1015. # base64 attach below is generic-format only), so unfurling is
  1016. # the only way a {finish_photo_url} in a print_complete body
  1017. # can render as a photo in the channel. Switching it off for
  1018. # every event would quietly take that away with no setting to
  1019. # get it back.
  1020. data["unfurl_links"] = False
  1021. data["unfurl_media"] = False
  1022. if image_url:
  1023. data["attachments"] = [{"fallback": title, "image_url": image_url}]
  1024. else:
  1025. # Generic format with custom field names
  1026. custom_field_title = config.get("field_title", "title").strip() or "title"
  1027. custom_field_message = config.get("field_message", "message").strip() or "message"
  1028. data = {
  1029. custom_field_title: title,
  1030. custom_field_message: message,
  1031. "timestamp": datetime.now().isoformat(),
  1032. "source": "Bambuddy",
  1033. }
  1034. # For generic format, include structured event data for automation tools
  1035. if payload_format != "slack":
  1036. if event_type:
  1037. data["event"] = event_type
  1038. if variables:
  1039. for key, value in variables.items():
  1040. if key not in data: # Don't overwrite title/message/timestamp/source
  1041. data[key] = value
  1042. # Attach base64-encoded image when available (generic format only)
  1043. if image_data and payload_format != "slack":
  1044. import base64
  1045. data["image"] = base64.b64encode(image_data).decode("ascii")
  1046. headers = {"Content-Type": "application/json"}
  1047. if auth_header:
  1048. # Support "Bearer token" or just "token" format
  1049. if " " in auth_header:
  1050. headers["Authorization"] = auth_header
  1051. else:
  1052. headers["Authorization"] = f"Bearer {auth_header}"
  1053. client = await self._get_client()
  1054. try:
  1055. response = await client.post(webhook_url, json=data, headers=headers)
  1056. if response.status_code in (200, 201, 202, 204):
  1057. return True, "Webhook delivered successfully"
  1058. else:
  1059. return False, _opaque_http_failure(response, label="webhook endpoint")
  1060. except Exception as e:
  1061. return False, f"Webhook error: {str(e)}"
  1062. async def _send_homeassistant(
  1063. self,
  1064. config: dict,
  1065. title: str,
  1066. message: str,
  1067. db: AsyncSession | None = None,
  1068. image_url: str | None = None,
  1069. ) -> tuple[bool, str]:
  1070. """Send notification via Home Assistant.
  1071. Uses the globally configured HA URL/token from settings.
  1072. Defaults to persistent_notification/create, but supports
  1073. custom services via config["service"] (e.g. notify.mobile_app_myphone).
  1074. """
  1075. # Get HA connection settings from global config
  1076. ha_url = ""
  1077. ha_token = ""
  1078. if db:
  1079. from backend.app.api.routes.settings import get_homeassistant_settings
  1080. try:
  1081. ha_settings = await get_homeassistant_settings(db)
  1082. ha_url = ha_settings.get("ha_url", "")
  1083. ha_token = ha_settings.get("ha_token", "")
  1084. except Exception as e:
  1085. logger.warning("Failed to read HA settings from database: %s", e)
  1086. else:
  1087. # Fallback: read directly from environment if no DB session
  1088. import os
  1089. ha_url = os.environ.get("HA_URL", "")
  1090. ha_token = os.environ.get("HA_TOKEN", "")
  1091. if not ha_url or not ha_token:
  1092. return False, (
  1093. "Home Assistant is not configured. Please set HA URL and token in Settings → Network → Home Assistant."
  1094. )
  1095. # Determine which HA service to call - Default: persistent_notification.create
  1096. service = (config.get("service") or "").strip()
  1097. if service:
  1098. # Allow in different forms:
  1099. # - notify.mobile_app_<device>
  1100. # - notify/mobile_app_<device>
  1101. # - api/services/notify/mobile_app_<device>
  1102. service_str = service.lstrip("/")
  1103. if service_str.startswith("api/services/"):
  1104. endpoint = service_str
  1105. elif "/" in service_str:
  1106. endpoint = f"api/services/{service_str}"
  1107. elif "." in service_str:
  1108. domain, svc = service_str.split(".", 1)
  1109. endpoint = f"api/services/{domain}/{svc}"
  1110. else:
  1111. return False, (
  1112. "Invalid Home Assistant service name. Use e.g. 'notify.mobile_app_yourdevice' or 'notify/your_service'."
  1113. )
  1114. if not re.match(r"^api/services/[a-zA-Z0-9_]+/[a-zA-Z0-9_]+$", endpoint):
  1115. return False, (
  1116. "Invalid Home Assistant service name. Domain and service must only contain letters, numbers, and underscores."
  1117. )
  1118. else:
  1119. endpoint = "api/services/persistent_notification/create"
  1120. url = f"{ha_url.rstrip('/')}/{endpoint}"
  1121. headers = {
  1122. "Authorization": f"Bearer {ha_token}",
  1123. "Content-Type": "application/json",
  1124. }
  1125. payload = {
  1126. "title": title,
  1127. "message": message,
  1128. }
  1129. # Optional custom service-data (#1441), forwarded as HA's nested "data"
  1130. # object so mobile-app push options (priority, ttl, channel, group, ...)
  1131. # reach the notify service. Only included when configured — the default
  1132. # persistent_notification.create schema rejects unknown keys.
  1133. raw_data = config.get("data")
  1134. data_payload: dict = {}
  1135. if raw_data:
  1136. if isinstance(raw_data, str):
  1137. try:
  1138. parsed_data = json.loads(raw_data)
  1139. except json.JSONDecodeError as e:
  1140. return False, f"Invalid JSON in the Data field: {e}"
  1141. else:
  1142. parsed_data = raw_data
  1143. if not isinstance(parsed_data, dict):
  1144. return False, 'The Data field must be a JSON object, e.g. {"priority": "high", "ttl": 0}'
  1145. data_payload = parsed_data
  1146. if image_url and service:
  1147. # HA's notify.* services fetch data.image themselves. Gated on a
  1148. # custom service being set: the default persistent_notification
  1149. # .create has a strict schema and 400s on fields it doesn't
  1150. # recognize, so we'd break plain notifications for anyone who
  1151. # never asked for a photo. setdefault so a user's own "image"
  1152. # key still wins.
  1153. data_payload.setdefault("image", image_url)
  1154. if data_payload:
  1155. payload["data"] = data_payload
  1156. client = await self._get_client()
  1157. response = await client.post(url, json=payload, headers=headers)
  1158. if response.status_code in (200, 201):
  1159. return True, "Notification sent via Home Assistant"
  1160. elif response.status_code == 401:
  1161. return False, "Home Assistant authentication failed - check your token"
  1162. else:
  1163. # ha_url comes from global settings (SETTINGS_UPDATE, admin-only), so
  1164. # this is a narrower channel than the per-request provider URLs — but
  1165. # it lands in the same NOTIFICATIONS_CREATE-gated test response, so it
  1166. # gets the same treatment.
  1167. return False, _opaque_http_failure(response, label="Home Assistant endpoint")
  1168. async def _get_or_build_photo_url(
  1169. self,
  1170. db: AsyncSession | None,
  1171. image_data: bytes | None,
  1172. event_type: str | None,
  1173. photo_cache: dict | None = None,
  1174. ) -> str | None:
  1175. """Return a URL for a notification photo, for providers that fetch it themselves.
  1176. Writes *image_data* under an unguessable filename and builds the URL
  1177. from the ``external_url`` setting. The filename is the only credential
  1178. (see notification_photos.py), so the URL opens this one photo and
  1179. nothing else -- no camera token, which would open every printer's live
  1180. stream. Print Complete goes through here too rather than reusing the
  1181. archive's ``finish_photo_url``: that route needs a media token, so with
  1182. auth on HA/Bark/Slack would get a 401.
  1183. Returns None with no photo or no ``external_url`` set: HA and Bark
  1184. fetch this URL themselves, so a relative path does them no good.
  1185. *photo_cache* is shared by every provider in one send, so only the
  1186. first HA/Bark/Slack provider pays for the disk write. It's kept apart
  1187. from the template variables so the URL never lands in a generic
  1188. webhook's payload.
  1189. """
  1190. if photo_cache is not None and "url" in photo_cache:
  1191. return photo_cache["url"]
  1192. if not image_data or db is None:
  1193. return None
  1194. from backend.app.api.routes.settings import get_setting
  1195. external_url = (await get_setting(db, "external_url") or "").strip()
  1196. if not external_url:
  1197. return None
  1198. try:
  1199. filename = await asyncio.to_thread(save_notification_photo, image_data, event_type or "event")
  1200. except Exception as e:
  1201. logger.warning("Failed to persist notification photo: %s", e)
  1202. return None
  1203. url = f"{external_url.rstrip('/')}/api/v1/notifications/photos/{filename}"
  1204. if photo_cache is not None:
  1205. photo_cache["url"] = url
  1206. return url
  1207. async def _send_to_provider(
  1208. self,
  1209. provider: NotificationProvider,
  1210. title: str,
  1211. message: str,
  1212. db: AsyncSession | None = None,
  1213. image_data: bytes | None = None,
  1214. event_type: str | None = None,
  1215. variables: dict | None = None,
  1216. photo_cache: dict | None = None,
  1217. printer_id: int | None = None,
  1218. bypass_quiet_hours: bool = False,
  1219. ) -> tuple[bool, str]:
  1220. """Send notification to a specific provider.
  1221. *photo_cache* lets the providers of one send share a single persisted
  1222. photo URL (see _get_or_build_photo_url). *bypass_quiet_hours* is for
  1223. the daily digest, which goes out at the time chosen for it.
  1224. """
  1225. # Check quiet hours
  1226. if not bypass_quiet_hours and self._is_in_quiet_hours(provider):
  1227. logger.info("Skipping notification to %s - quiet hours active", provider.name)
  1228. return True, "Skipped - quiet hours"
  1229. config = json.loads(provider.config) if isinstance(provider.config, str) else provider.config
  1230. # attach_photo is a per-provider opt-out, photo_skip_events narrows it
  1231. # to some events (#2877). Clearing image_data covers the byte-upload
  1232. # providers below; HA/Bark/Slack/Gotify/Notify! check attach_photo
  1233. # again further down, since they could otherwise get the URL an
  1234. # earlier provider in the same send left in photo_cache.
  1235. attach_photo = bool(provider.attach_photo) and event_type not in (provider.photo_skip_events or ())
  1236. if not attach_photo:
  1237. image_data = None
  1238. try:
  1239. if provider.provider_type == "callmebot":
  1240. return await self._send_callmebot(config, f"{title}\n{message}")
  1241. elif provider.provider_type == "ntfy":
  1242. # Outcome confirmation (#1898): render the verdict capability
  1243. # links as one-tap buttons on the notification itself. http so
  1244. # no browser needs to open; POST because that is the method
  1245. # that records — a GET only opens the confirmation page, which
  1246. # is what keeps unfurlers from answering the prompt.
  1247. # clear=true dismisses the notification once a button was tapped.
  1248. ntfy_actions = None
  1249. good_url = (variables or {}).get("good_url")
  1250. reject_url = (variables or {}).get("reject_url")
  1251. # Buttons need absolute URLs; without a configured external_url
  1252. # the links are relative, and the body's deep link into the
  1253. # archive has to do.
  1254. if (
  1255. event_type == "print_confirm_request"
  1256. and good_url
  1257. and reject_url
  1258. and good_url.startswith("http")
  1259. and reject_url.startswith("http")
  1260. ):
  1261. ntfy_actions = (
  1262. f"http, Good, {good_url}, method=POST, clear=true; "
  1263. f"http, Reject, {reject_url}, method=POST, clear=true"
  1264. )
  1265. return await self._send_ntfy(
  1266. config, title, message, image_data=image_data, event_type=event_type, actions=ntfy_actions
  1267. )
  1268. elif provider.provider_type == "pushover":
  1269. # Outcome confirmation (#1898): Pushover has no arbitrary
  1270. # buttons, but supports one supplementary URL — deep-link into
  1271. # the archive's confirmation dialog.
  1272. supplement_url = None
  1273. supplement_url_title = None
  1274. _confirm_url = (variables or {}).get("confirm_url")
  1275. if event_type == "print_confirm_request" and _confirm_url and _confirm_url.startswith("http"):
  1276. supplement_url = _confirm_url
  1277. supplement_url_title = "Confirm print outcome"
  1278. return await self._send_pushover(
  1279. config, title, message, image_data=image_data, url=supplement_url, url_title=supplement_url_title
  1280. )
  1281. elif provider.provider_type == "telegram":
  1282. if event_type == "print_confirm_request":
  1283. # Outcome confirmation (#1898): inline URL buttons under
  1284. # the message — one tap records the verdict via the
  1285. # capability link. Same absolute-URL requirement as the
  1286. # ntfy actions. Telegram has no way to POST, so this is the
  1287. # one affordance that opens a browser, and it is the one
  1288. # that gets the one-tap marker: the page submits itself
  1289. # only for a URL that came off a button. Telegram does not
  1290. # fetch inline-keyboard URLs and nothing else can read
  1291. # them, so the marker never reaches a scanner — which is
  1292. # the difference between this and trusting the User-Agent.
  1293. tg_buttons = None
  1294. _tg_good = (variables or {}).get("good_url")
  1295. _tg_reject = (variables or {}).get("reject_url")
  1296. if _tg_good and _tg_reject and _tg_good.startswith("http") and _tg_reject.startswith("http"):
  1297. tg_buttons = [
  1298. {"text": "\U0001f44d Good", "url": one_tap_url(_tg_good)},
  1299. {"text": "\U0001f44e Reject", "url": one_tap_url(_tg_reject)},
  1300. ]
  1301. # Telegram verdict mode (#3046): buttons, a reaction on
  1302. # the message, or both. The link preview is off in all of
  1303. # them (see _send_telegram_confirm_request).
  1304. _archive_id = (variables or {}).get("archive_id")
  1305. _run_id = (variables or {}).get("run_id")
  1306. return await self._send_telegram_confirm_request(
  1307. provider,
  1308. config,
  1309. f"*{title}*\n{message}",
  1310. db,
  1311. image_data,
  1312. tg_buttons,
  1313. _archive_id if isinstance(_archive_id, int) else None,
  1314. run_id=_run_id if isinstance(_run_id, int) else None,
  1315. )
  1316. # Every other event keeps Telegram's link preview, for the same
  1317. # reason as the Slack unfurl in _send_webhook: when the finish
  1318. # photo is too large to attach, the preview is how a
  1319. # {finish_photo_url} in a print_complete body still shows up as
  1320. # a photo in the chat.
  1321. return await self._send_telegram(config, f"*{title}*\n{message}", image_data=image_data)
  1322. elif provider.provider_type == "email":
  1323. # finish_photo_url is pulled from the rendered template variables
  1324. # so _send_email can detect whether the template referenced the
  1325. # URL and inline-embed the photo only in that case.
  1326. finish_photo_url = (variables or {}).get("finish_photo_url")
  1327. return await self._send_email(
  1328. config, title, message, image_data=image_data, finish_photo_url=finish_photo_url
  1329. )
  1330. elif provider.provider_type == "discord":
  1331. return await self._send_discord(config, title, message, image_data=image_data)
  1332. elif provider.provider_type == "webhook":
  1333. # Only Slack format needs a URL — generic format gets the
  1334. # base64 image field instead.
  1335. photo_url = None
  1336. if attach_photo and (config.get("payload_format") or "generic").strip() == "slack":
  1337. photo_url = await self._get_or_build_photo_url(db, image_data, event_type, photo_cache)
  1338. return await self._send_webhook(
  1339. config,
  1340. title,
  1341. message,
  1342. image_data=image_data,
  1343. event_type=event_type,
  1344. variables=variables,
  1345. image_url=photo_url,
  1346. )
  1347. elif provider.provider_type == "homeassistant":
  1348. photo_url = (
  1349. await self._get_or_build_photo_url(db, image_data, event_type, photo_cache)
  1350. if attach_photo
  1351. else None
  1352. )
  1353. return await self._send_homeassistant(config, title, message, db=db, image_url=photo_url)
  1354. elif provider.provider_type == "bark":
  1355. # Outcome confirmation (#1898): Bark opens one URL on tap —
  1356. # deep-link into the confirmation dialog, like Pushover.
  1357. bark_url = None
  1358. _bark_confirm = (variables or {}).get("confirm_url")
  1359. if event_type == "print_confirm_request" and _bark_confirm and _bark_confirm.startswith("http"):
  1360. bark_url = _bark_confirm
  1361. photo_url = (
  1362. await self._get_or_build_photo_url(db, image_data, event_type, photo_cache)
  1363. if attach_photo
  1364. else None
  1365. )
  1366. return await self._send_bark(config, title, message, url=bark_url, image_url=photo_url)
  1367. elif provider.provider_type == "notify":
  1368. photo_url = (
  1369. await self._get_or_build_photo_url(db, image_data, event_type, photo_cache)
  1370. if attach_photo and notify_supports_photos(config.get("device_id"))
  1371. else None
  1372. )
  1373. return await self._send_notify(
  1374. config, title, message, image_url=photo_url, event_type=event_type, printer_id=printer_id
  1375. )
  1376. elif provider.provider_type == "gotify":
  1377. # Outcome confirmation (#1898): like Bark, Gotify opens one URL
  1378. # on tap -- deep-link into the confirmation dialog.
  1379. gotify_url = None
  1380. _gotify_confirm = (variables or {}).get("confirm_url")
  1381. if event_type == "print_confirm_request" and _gotify_confirm and _gotify_confirm.startswith("http"):
  1382. gotify_url = _gotify_confirm
  1383. photo_url = (
  1384. await self._get_or_build_photo_url(db, image_data, event_type, photo_cache)
  1385. if attach_photo
  1386. else None
  1387. )
  1388. return await self._send_gotify(
  1389. config, title, message, event_type=event_type, url=gotify_url, image_url=photo_url
  1390. )
  1391. else:
  1392. return False, f"Unknown provider type: {provider.provider_type}"
  1393. except Exception as e:
  1394. logger.exception("Error sending notification via %s", provider.provider_type)
  1395. return False, str(e)
  1396. async def _update_provider_status(
  1397. self, db: AsyncSession, provider_id: int, success: bool, error: str | None = None
  1398. ):
  1399. """Update provider status after sending notification."""
  1400. result = await db.execute(select(NotificationProvider).where(NotificationProvider.id == provider_id))
  1401. provider = result.scalar_one_or_none()
  1402. if provider:
  1403. if success:
  1404. provider.last_success = datetime.now(timezone.utc)
  1405. else:
  1406. # Read by every notification reader: no refused header's value.
  1407. provider.last_error = redact_refused_headers(error)
  1408. provider.last_error_at = datetime.now(timezone.utc)
  1409. await db.commit()
  1410. async def _get_providers_for_event(
  1411. self,
  1412. db: AsyncSession,
  1413. event_field: str,
  1414. printer_id: int | None = None,
  1415. *,
  1416. fleet_wide: bool = False,
  1417. ) -> list[NotificationProvider]:
  1418. """Get all enabled providers that want a specific event type.
  1419. A provider limited to one printer gets that printer's events. An event
  1420. with no printer -- a job for "Any <model>" not yet assigned -- reaches
  1421. only "All printers" providers, unless it is about the whole fleet
  1422. (``fleet_wide``: the queue finishing, stock, app messages), which
  1423. every provider that wants it gets.
  1424. Runs under ``no_autoflush`` (#2770). Callers routinely hold pending
  1425. writes when they raise an event — the AMS sensor loop does
  1426. ``db.add(history)`` and only commits after the alarms have gone out — and
  1427. without this, autoflush satisfies this SELECT by writing those rows,
  1428. which opens a write transaction on SQLite. The provider is then contacted
  1429. over the network with that transaction still open, so a site whose
  1430. internet is down holds the single SQLite writer for the whole connect
  1431. timeout and unrelated background tasks fail with "database is locked".
  1432. Deferring the flush costs nothing here: providers are committed rows, so
  1433. a pending change in the caller's session cannot be one this query wants.
  1434. """
  1435. # Build the query dynamically based on event field
  1436. query = select(NotificationProvider).where(
  1437. NotificationProvider.enabled.is_(True),
  1438. getattr(NotificationProvider, event_field).is_(True),
  1439. )
  1440. if printer_id is not None:
  1441. query = query.where(
  1442. (NotificationProvider.printer_id.is_(None)) | (NotificationProvider.printer_id == printer_id)
  1443. )
  1444. elif not fleet_wide:
  1445. query = query.where(NotificationProvider.printer_id.is_(None))
  1446. with db.no_autoflush:
  1447. result = await db.execute(query)
  1448. return list(result.scalars().all())
  1449. async def _log_notification(
  1450. self,
  1451. db: AsyncSession,
  1452. provider_id: int,
  1453. event_type: str,
  1454. title: str,
  1455. message: str,
  1456. success: bool,
  1457. error_message: str | None = None,
  1458. printer_id: int | None = None,
  1459. printer_name: str | None = None,
  1460. ):
  1461. """Create a log entry for a sent notification."""
  1462. try:
  1463. log = NotificationLog(
  1464. provider_id=provider_id,
  1465. event_type=event_type,
  1466. title=title,
  1467. message=message,
  1468. success=success,
  1469. error_message=redact_refused_headers(error_message),
  1470. printer_id=printer_id,
  1471. printer_name=printer_name,
  1472. )
  1473. db.add(log)
  1474. await db.commit()
  1475. except Exception as e:
  1476. logger.warning("Failed to log notification: %s", e)
  1477. # Don't fail the notification just because logging failed
  1478. async def _send_to_providers(
  1479. self,
  1480. providers: list[NotificationProvider],
  1481. title: str,
  1482. message: str,
  1483. db: AsyncSession,
  1484. event_type: str = "unknown",
  1485. printer_id: int | None = None,
  1486. printer_name: str | None = None,
  1487. image_data: bytes | None = None,
  1488. variables: dict | None = None,
  1489. ):
  1490. """Send notification to multiple providers and log the results.
  1491. All notifications are always sent immediately. If digest mode is enabled,
  1492. the notification is ALSO queued for the daily digest summary.
  1493. """
  1494. photo_cache: dict = {}
  1495. for provider in providers:
  1496. try:
  1497. # Always send notification immediately
  1498. success, error = await self._send_to_provider(
  1499. provider,
  1500. title,
  1501. message,
  1502. db,
  1503. image_data=image_data,
  1504. event_type=event_type,
  1505. variables=variables,
  1506. photo_cache=photo_cache,
  1507. printer_id=printer_id,
  1508. )
  1509. # Also queue for digest if enabled (digest is a summary, not a queue)
  1510. if provider.daily_digest_enabled and provider.daily_digest_time:
  1511. await self._queue_for_digest(
  1512. provider=provider,
  1513. event_type=event_type,
  1514. title=title,
  1515. message=message,
  1516. db=db,
  1517. printer_id=printer_id,
  1518. printer_name=printer_name,
  1519. )
  1520. await self._update_provider_status(db, provider.id, success, error if not success else None)
  1521. await self._log_notification(
  1522. db=db,
  1523. provider_id=provider.id,
  1524. event_type=event_type,
  1525. title=title,
  1526. message=message,
  1527. success=success,
  1528. error_message=error if not success else None,
  1529. printer_id=printer_id,
  1530. printer_name=printer_name,
  1531. )
  1532. if success:
  1533. logger.info("Sent notification via %s", provider.name)
  1534. else:
  1535. logger.warning("Failed to send notification via %s: %s", provider.name, error)
  1536. except Exception as e:
  1537. logger.exception("Error sending notification via %s", provider.name)
  1538. await self._update_provider_status(db, provider.id, False, str(e))
  1539. await self._log_notification(
  1540. db=db,
  1541. provider_id=provider.id,
  1542. event_type=event_type,
  1543. title=title,
  1544. message=message,
  1545. success=False,
  1546. error_message=str(e),
  1547. printer_id=printer_id,
  1548. printer_name=printer_name,
  1549. )
  1550. async def on_app_message(
  1551. self,
  1552. db: AsyncSession,
  1553. *,
  1554. sender: str,
  1555. title: str,
  1556. message: str,
  1557. url: str | None = None,
  1558. allowed: Callable[[NotificationProvider], bool] | None = None,
  1559. ) -> int:
  1560. """A message another application sends through Bambuddy.
  1561. Goes to every enabled channel with "Messages from connected apps" on,
  1562. through the same path as Bambuddy's own events: quiet hours, the daily
  1563. digest and the log (whose event type names the sender). The text is
  1564. the app's own; a link, when given, is appended so every channel type
  1565. carries it. Returns how many channels it was handed to. ``allowed``
  1566. narrows the channels to those the sender may reach.
  1567. """
  1568. providers = await self._get_providers_for_event(db, "on_app_message", fleet_wide=True)
  1569. if allowed is not None:
  1570. providers = [provider for provider in providers if allowed(provider)]
  1571. if not providers:
  1572. return 0
  1573. body = f"{message}\n{url}" if url else message
  1574. await self._send_to_providers(providers, title, body, db, event_type=f"app:{sender}"[:50])
  1575. return len(providers)
  1576. async def on_print_start(
  1577. self,
  1578. printer_id: int,
  1579. printer_name: str,
  1580. data: dict,
  1581. db: AsyncSession,
  1582. archive_data: dict | None = None,
  1583. ):
  1584. """Handle print start event - send notifications to relevant providers.
  1585. Args:
  1586. printer_id: The printer ID
  1587. printer_name: The printer name
  1588. data: MQTT event data with filename, subtask_name, remaining_time, raw_data
  1589. db: Database session
  1590. archive_data: Optional archive data with print_time_seconds from 3MF parsing
  1591. """
  1592. logger.info("on_print_start called for printer %s (%s)", printer_id, printer_name)
  1593. providers = await self._get_providers_for_event(db, "on_print_start", printer_id)
  1594. if not providers:
  1595. logger.info("No notification providers configured for print_start event on printer %s", printer_id)
  1596. return
  1597. # Use subtask_name (project name) if available, otherwise use filename
  1598. subtask_name = data.get("subtask_name")
  1599. if subtask_name:
  1600. # Replace underscores with spaces for readability
  1601. filename = subtask_name.replace("_", " ")
  1602. else:
  1603. filename = self._clean_filename(data.get("filename", "Unknown"))
  1604. # Priority for estimated_time:
  1605. # 1. Archive's print_time_seconds from 3MF parsing (most reliable)
  1606. # 2. MQTT remaining_time (may be 0 at print start)
  1607. # 3. raw_data mc_remaining_time
  1608. estimated_time = None
  1609. # Try archive data first (from 3MF parsing - most reliable)
  1610. if archive_data and archive_data.get("print_time_seconds"):
  1611. estimated_time = archive_data["print_time_seconds"]
  1612. logger.debug("Using print_time_seconds from archive: %s", estimated_time)
  1613. # Fall back to MQTT remaining_time
  1614. if estimated_time is None:
  1615. estimated_time = data.get("remaining_time")
  1616. if estimated_time:
  1617. logger.debug("Using remaining_time from MQTT: %s", estimated_time)
  1618. # Last resort: raw_data mc_remaining_time (in minutes, convert to seconds)
  1619. if estimated_time is None:
  1620. raw_time = data.get("raw_data", {}).get("mc_remaining_time")
  1621. if raw_time:
  1622. estimated_time = raw_time * 60
  1623. logger.debug("Using mc_remaining_time from raw_data: %s", estimated_time)
  1624. time_str = self._format_duration(estimated_time)
  1625. eta_str = await self._format_eta(estimated_time, db)
  1626. variables = {
  1627. "printer": printer_name,
  1628. "filename": filename,
  1629. "estimated_time": time_str,
  1630. "eta": eta_str,
  1631. }
  1632. # Extract image data for providers that support attachments (e.g. Pushover)
  1633. image_data = None
  1634. if archive_data:
  1635. image_data = archive_data.get("image_data")
  1636. logger.info("Found %s providers for print_start: %s", len(providers), [p.name for p in providers])
  1637. title, message = await self._build_message_from_template(db, "print_start", variables)
  1638. await self._send_to_providers(
  1639. providers,
  1640. title,
  1641. message,
  1642. db,
  1643. "print_start",
  1644. printer_id,
  1645. printer_name,
  1646. image_data=image_data,
  1647. variables=variables,
  1648. )
  1649. async def on_print_complete(
  1650. self,
  1651. printer_id: int,
  1652. printer_name: str,
  1653. status: str,
  1654. data: dict,
  1655. db: AsyncSession,
  1656. archive_data: dict | None = None,
  1657. ):
  1658. """Handle print complete event - send notifications to relevant providers."""
  1659. logger.info("on_print_complete called for printer %s (%s), status=%s", printer_id, printer_name, status)
  1660. # Determine event type based on status
  1661. if status == "completed":
  1662. event_field = "on_print_complete"
  1663. event_type = "print_complete"
  1664. elif status in ("failed",):
  1665. event_field = "on_print_failed"
  1666. event_type = "print_failed"
  1667. elif status in ("aborted", "stopped", "cancelled"):
  1668. event_field = "on_print_stopped"
  1669. event_type = "print_stopped"
  1670. else:
  1671. logger.warning("Unknown print status '%s', defaulting to on_print_complete", status)
  1672. event_field = "on_print_complete"
  1673. event_type = "print_complete"
  1674. providers = await self._get_providers_for_event(db, event_field, printer_id)
  1675. if not providers:
  1676. logger.info("No notification providers configured for %s event on printer %s", event_field, printer_id)
  1677. return
  1678. # Use subtask_name (project name) if available, otherwise use filename
  1679. subtask_name = data.get("subtask_name")
  1680. if subtask_name:
  1681. filename = subtask_name.replace("_", " ")
  1682. else:
  1683. filename = self._clean_filename(data.get("filename", "Unknown"))
  1684. variables = {
  1685. "printer": printer_name,
  1686. "filename": filename,
  1687. "duration": "Unknown",
  1688. "filament_grams": "Unknown",
  1689. "reason": "Unknown",
  1690. }
  1691. if archive_data:
  1692. # {{duration}} on completion / failure / stopped events is the *actual*
  1693. # elapsed time (#1198). Slicer-estimated print_time_seconds is only used
  1694. # as a last-resort fallback when timestamps weren't recorded.
  1695. duration_seconds = archive_data.get("actual_time_seconds") or archive_data.get("print_time_seconds")
  1696. if duration_seconds:
  1697. variables["duration"] = self._format_duration(duration_seconds)
  1698. if archive_data.get("actual_filament_grams"):
  1699. variables["filament_grams"] = f"{archive_data['actual_filament_grams']:.1f}"
  1700. if status == "failed" and archive_data.get("failure_reason"):
  1701. variables["reason"] = archive_data["failure_reason"]
  1702. if archive_data.get("finish_photo_url"):
  1703. variables["finish_photo_url"] = archive_data["finish_photo_url"]
  1704. # The archive (file) and run (this print) it was, for links and
  1705. # webhooks (archive redesign).
  1706. for key in ("archive_id", "run_id"):
  1707. if archive_data.get(key) is not None:
  1708. variables[key] = str(archive_data[key])
  1709. # Build per-slot breakdown string with AMS info when available
  1710. if archive_data.get("usage_results"):
  1711. parts = []
  1712. for u in archive_data["usage_results"]:
  1713. ams_id = u.get("ams_id", 0)
  1714. tray_id = u.get("tray_id", 0)
  1715. material = u.get("material", "Unknown") or "Unknown"
  1716. used = u.get("weight_used", 0)
  1717. if ams_id == EXTERNAL_AMS_ID or is_ams_ht_id(ams_id):
  1718. slot_label = "Ext"
  1719. else:
  1720. slot_label = f"{unit_label(ams_id)} T{tray_id + 1}"
  1721. parts.append(f"{slot_label} {material}: {used:.1f}g")
  1722. variables["filament_details"] = " | ".join(parts)
  1723. elif archive_data.get("filament_slots"):
  1724. parts = []
  1725. for slot in archive_data["filament_slots"]:
  1726. ftype = slot.get("type", "Unknown") or "Unknown"
  1727. used = slot.get("used_g", 0)
  1728. parts.append(f"{ftype}: {used:.1f}g")
  1729. variables["filament_details"] = " | ".join(parts)
  1730. # Add progress for partial prints
  1731. if archive_data.get("progress") is not None:
  1732. variables["progress"] = str(archive_data["progress"])
  1733. # Extract image data for providers that support attachments (e.g. Pushover)
  1734. image_data = None
  1735. if archive_data:
  1736. image_data = archive_data.get("image_data")
  1737. logger.info("Found %s providers for %s: %s", len(providers), event_field, [p.name for p in providers])
  1738. title, message = await self._build_message_from_template(db, event_type, variables)
  1739. await self._send_to_providers(
  1740. providers,
  1741. title,
  1742. message,
  1743. db,
  1744. event_type,
  1745. printer_id,
  1746. printer_name,
  1747. image_data=image_data,
  1748. variables=variables,
  1749. )
  1750. async def on_print_confirm_request(
  1751. self,
  1752. printer_id: int,
  1753. printer_name: str,
  1754. data: dict,
  1755. db: AsyncSession,
  1756. archive_data: dict | None = None,
  1757. good_url: str | None = None,
  1758. reject_url: str | None = None,
  1759. confirm_url: str | None = None,
  1760. archive_id: int | None = None,
  1761. run_id: int | None = None,
  1762. ):
  1763. """Ask for a post-print outcome verdict (#1898).
  1764. Fires only for completed prints whose queue item opted in — the
  1765. provider-level toggle exists to mute a channel, not to enable the
  1766. feature. good_url / reject_url are the one-tap capability links
  1767. (rendered as ntfy action buttons), confirm_url deep-links into the
  1768. archive's confirmation dialog in the web UI. archive_id lets a Telegram
  1769. provider in reaction mode (#3046) tie the sent message to the archive,
  1770. and run_id to the run whose token the links carry.
  1771. """
  1772. providers = await self._get_providers_for_event(db, "on_print_confirm_request", printer_id)
  1773. if not providers:
  1774. return
  1775. subtask_name = data.get("subtask_name")
  1776. if subtask_name:
  1777. filename = subtask_name.replace("_", " ")
  1778. else:
  1779. filename = self._clean_filename(data.get("filename", "Unknown"))
  1780. variables = {"printer": printer_name, "filename": filename}
  1781. if good_url:
  1782. variables["good_url"] = good_url
  1783. if reject_url:
  1784. variables["reject_url"] = reject_url
  1785. if confirm_url:
  1786. variables["confirm_url"] = confirm_url
  1787. if archive_id is not None:
  1788. variables["archive_id"] = archive_id
  1789. if run_id is not None:
  1790. variables["run_id"] = run_id
  1791. image_data = None
  1792. if archive_data:
  1793. if archive_data.get("finish_photo_url"):
  1794. variables["finish_photo_url"] = archive_data["finish_photo_url"]
  1795. image_data = archive_data.get("image_data")
  1796. title, message = await self._build_message_from_template(db, "print_confirm_request", variables)
  1797. await self._send_to_providers(
  1798. providers,
  1799. title,
  1800. message,
  1801. db,
  1802. "print_confirm_request",
  1803. printer_id,
  1804. printer_name,
  1805. image_data=image_data,
  1806. variables=variables,
  1807. )
  1808. async def on_print_progress(
  1809. self,
  1810. printer_id: int,
  1811. printer_name: str,
  1812. filename: str,
  1813. progress: int,
  1814. db: AsyncSession,
  1815. remaining_time: int | None = None,
  1816. image_data: bytes | None = None,
  1817. ):
  1818. """Handle print progress milestone (25%, 50%, 75%)."""
  1819. providers = await self._get_providers_for_event(db, "on_print_progress", printer_id)
  1820. if not providers:
  1821. return
  1822. eta_str = await self._format_eta(remaining_time, db)
  1823. variables = {
  1824. "printer": printer_name,
  1825. "filename": self._clean_filename(filename),
  1826. "progress": str(progress),
  1827. "remaining_time": self._format_duration(remaining_time) if remaining_time else "Unknown",
  1828. "eta": eta_str,
  1829. }
  1830. title, message = await self._build_message_from_template(db, "print_progress", variables)
  1831. await self._send_to_providers(
  1832. providers,
  1833. title,
  1834. message,
  1835. db,
  1836. "print_progress",
  1837. printer_id,
  1838. printer_name,
  1839. image_data=image_data,
  1840. variables=variables,
  1841. )
  1842. async def on_print_missing_spool_assignment(
  1843. self,
  1844. printer_id: int,
  1845. printer_name: str,
  1846. missing_slots: list[dict[str, str]],
  1847. db: AsyncSession,
  1848. ):
  1849. """Handle print-start event when required trays are missing spool assignments."""
  1850. if not missing_slots:
  1851. return
  1852. providers = await self._get_providers_for_event(db, "on_print_missing_spool_assignment", printer_id)
  1853. if not providers:
  1854. return
  1855. missing_slot_names = ", ".join(slot.get("slot", "Unknown") for slot in missing_slots)
  1856. detail_lines = []
  1857. for slot in missing_slots:
  1858. slot_name = slot.get("slot", "Unknown")
  1859. profile = slot.get("profile", "Unknown")
  1860. detail_lines.append(f"- {slot_name}: {profile}")
  1861. missing_profile_details = "\n".join(detail_lines)
  1862. variables = {
  1863. "printer": printer_name,
  1864. "missing_slots": missing_slot_names,
  1865. "missing_slot_details": missing_profile_details,
  1866. }
  1867. title, message = await self._build_message_from_template(db, "print_missing_spool_assignment", variables)
  1868. await self._send_to_providers(
  1869. providers,
  1870. title,
  1871. message,
  1872. db,
  1873. "print_missing_spool_assignment",
  1874. printer_id,
  1875. printer_name,
  1876. variables=variables,
  1877. )
  1878. async def on_billing_charge_failed(
  1879. self,
  1880. printer_id: int,
  1881. printer_name: str,
  1882. filename: str,
  1883. archive_id: int | None,
  1884. error: str,
  1885. db: AsyncSession,
  1886. ) -> None:
  1887. """Notify providers that a terminal print could not be charged."""
  1888. providers = await self._get_providers_for_event(db, "on_billing_charge_failed", printer_id)
  1889. if not providers:
  1890. return
  1891. variables = {
  1892. "printer": printer_name,
  1893. "filename": self._clean_filename(filename),
  1894. "archive_id": str(archive_id) if archive_id is not None else "Unknown",
  1895. "error": error,
  1896. }
  1897. title, message = await self._build_message_from_template(db, "billing_charge_failed", variables)
  1898. await self._send_to_providers(
  1899. providers,
  1900. title,
  1901. message,
  1902. db,
  1903. "billing_charge_failed",
  1904. printer_id,
  1905. printer_name,
  1906. variables=variables,
  1907. )
  1908. async def on_printer_offline(self, printer_id: int, printer_name: str, db: AsyncSession):
  1909. """Handle printer offline event."""
  1910. providers = await self._get_providers_for_event(db, "on_printer_offline", printer_id)
  1911. if not providers:
  1912. return
  1913. variables = {"printer": printer_name}
  1914. title, message = await self._build_message_from_template(db, "printer_offline", variables)
  1915. await self._send_to_providers(
  1916. providers, title, message, db, "printer_offline", printer_id, printer_name, variables=variables
  1917. )
  1918. async def on_printer_error(
  1919. self,
  1920. printer_id: int | None,
  1921. printer_name: str,
  1922. error_type: str,
  1923. db: AsyncSession,
  1924. error_detail: str | None = None,
  1925. image_data: bytes | None = None,
  1926. ):
  1927. """Handle printer error event (AMS issues, etc.)."""
  1928. providers = await self._get_providers_for_event(db, "on_printer_error", printer_id)
  1929. if not providers:
  1930. return
  1931. variables = {
  1932. "printer": printer_name,
  1933. "error_type": error_type,
  1934. "error_detail": error_detail or "No details available",
  1935. }
  1936. title, message = await self._build_message_from_template(db, "printer_error", variables)
  1937. await self._send_to_providers(
  1938. providers,
  1939. title,
  1940. message,
  1941. db,
  1942. "printer_error",
  1943. printer_id,
  1944. printer_name,
  1945. image_data=image_data,
  1946. variables=variables,
  1947. )
  1948. async def on_ai_failure_detection(
  1949. self,
  1950. printer_id: int,
  1951. printer_name: str,
  1952. task_name: str,
  1953. confidence: float | None,
  1954. action: str,
  1955. db: AsyncSession,
  1956. image_data: bytes | None = None,
  1957. print_quality: int | None = None,
  1958. ):
  1959. """Handle AI failure-detection events from the configured provider.
  1960. Split out of on_printer_error (#1794) so a user can subscribe to AI
  1961. alerts without also being paged for every HMS hardware code.
  1962. """
  1963. providers = await self._get_providers_for_event(db, "on_ai_failure_detection", printer_id)
  1964. if not providers:
  1965. return
  1966. variables = {
  1967. "printer": printer_name,
  1968. "task_name": task_name or "current job",
  1969. "confidence": f"{confidence:.2f}" if confidence is not None else "N/A",
  1970. "action": action,
  1971. "provider": "OctoEverywhere" if print_quality is not None else "Obico",
  1972. "print_quality": f"{print_quality}/10" if print_quality is not None else "N/A",
  1973. }
  1974. title, message = await self._build_message_from_template(db, "ai_failure_detection", variables)
  1975. await self._send_to_providers(
  1976. providers,
  1977. title,
  1978. message,
  1979. db,
  1980. "ai_failure_detection",
  1981. printer_id,
  1982. printer_name,
  1983. image_data=image_data,
  1984. variables=variables,
  1985. )
  1986. async def on_plate_not_empty(
  1987. self,
  1988. printer_id: int,
  1989. printer_name: str,
  1990. db: AsyncSession,
  1991. difference_percent: float | None = None,
  1992. image_data: bytes | None = None,
  1993. ):
  1994. """Handle plate not empty event - objects detected on build plate before print."""
  1995. providers = await self._get_providers_for_event(db, "on_plate_not_empty", printer_id)
  1996. if not providers:
  1997. return
  1998. variables = {
  1999. "printer": printer_name,
  2000. "difference_percent": f"{difference_percent:.1f}" if difference_percent else "N/A",
  2001. }
  2002. title, message = await self._build_message_from_template(db, "plate_not_empty", variables)
  2003. await self._send_to_providers(
  2004. providers,
  2005. title,
  2006. message,
  2007. db,
  2008. "plate_not_empty",
  2009. printer_id,
  2010. printer_name,
  2011. image_data=image_data,
  2012. variables=variables,
  2013. )
  2014. async def on_plate_clear_required(
  2015. self,
  2016. printer_id: int,
  2017. printer_name: str,
  2018. db: AsyncSession,
  2019. ):
  2020. """Handle plate-clear-required event — a print ended and the queue is gated (#2525).
  2021. Distinct from ``on_plate_not_empty``, which is the camera check *before* a
  2022. print starts. This one fires on the rising edge of the Bambuddy-side
  2023. awaiting-plate-clear flag, i.e. whenever a print reaches a terminal state
  2024. and the next queued job can't dispatch until someone confirms the bed is
  2025. free. Off by default on every provider: it lands at the same moment as the
  2026. print-complete notification, so opting in is a deliberate choice.
  2027. """
  2028. providers = await self._get_providers_for_event(db, "on_plate_clear_required", printer_id)
  2029. if not providers:
  2030. return
  2031. variables = {"printer": printer_name}
  2032. title, message = await self._build_message_from_template(db, "plate_clear_required", variables)
  2033. await self._send_to_providers(
  2034. providers,
  2035. title,
  2036. message,
  2037. db,
  2038. "plate_clear_required",
  2039. printer_id,
  2040. printer_name,
  2041. variables=variables,
  2042. )
  2043. async def on_filament_low(
  2044. self,
  2045. printer_id: int,
  2046. printer_name: str,
  2047. slot: str,
  2048. remaining_percent: int,
  2049. db: AsyncSession,
  2050. color: str | None = None,
  2051. ):
  2052. """Handle low filament event."""
  2053. providers = await self._get_providers_for_event(db, "on_filament_low", printer_id)
  2054. if not providers:
  2055. return
  2056. variables = {
  2057. "printer": printer_name,
  2058. "slot": slot,
  2059. "remaining_percent": str(remaining_percent),
  2060. "color": color or "",
  2061. }
  2062. title, message = await self._build_message_from_template(db, "filament_low", variables)
  2063. await self._send_to_providers(
  2064. providers, title, message, db, "filament_low", printer_id, printer_name, variables=variables
  2065. )
  2066. async def on_maintenance_due(
  2067. self,
  2068. printer_id: int,
  2069. printer_name: str,
  2070. maintenance_items: list[dict],
  2071. db: AsyncSession,
  2072. ):
  2073. """Handle maintenance due event - sends notification when maintenance is due or warning."""
  2074. if not maintenance_items:
  2075. return
  2076. providers = await self._get_providers_for_event(db, "on_maintenance_due", printer_id)
  2077. if not providers:
  2078. logger.info("No notification providers configured for maintenance_due event on printer %s", printer_id)
  2079. return
  2080. # Format maintenance items list
  2081. items_list = []
  2082. for item in maintenance_items:
  2083. status = "OVERDUE" if item.get("is_due") else "Soon"
  2084. items_list.append(f"- {item['name']} ({status})")
  2085. items_str = "\n".join(items_list)
  2086. variables = {
  2087. "printer": printer_name,
  2088. "items": items_str,
  2089. }
  2090. logger.info("Found %s providers for maintenance_due: %s", len(providers), [p.name for p in providers])
  2091. title, message = await self._build_message_from_template(db, "maintenance_due", variables)
  2092. await self._send_to_providers(
  2093. providers, title, message, db, "maintenance_due", printer_id, printer_name, variables=variables
  2094. )
  2095. async def on_ams_humidity_high(
  2096. self,
  2097. printer_id: int,
  2098. printer_name: str,
  2099. ams_label: str,
  2100. humidity: float,
  2101. threshold: float,
  2102. db: AsyncSession,
  2103. ):
  2104. """Handle AMS high humidity alarm event. Sent immediately, and also in the daily digest if enabled."""
  2105. providers = await self._get_providers_for_event(db, "on_ams_humidity_high", printer_id)
  2106. if not providers:
  2107. return
  2108. variables = {
  2109. "printer": printer_name,
  2110. "ams_label": ams_label,
  2111. "humidity": f"{humidity:.0f}",
  2112. "threshold": f"{threshold:.0f}",
  2113. }
  2114. title, message = await self._build_message_from_template(db, "ams_humidity_high", variables)
  2115. await self._send_to_providers(
  2116. providers,
  2117. title,
  2118. message,
  2119. db,
  2120. "ams_humidity_high",
  2121. printer_id,
  2122. printer_name,
  2123. variables=variables,
  2124. )
  2125. async def on_ams_temperature_high(
  2126. self,
  2127. printer_id: int,
  2128. printer_name: str,
  2129. ams_label: str,
  2130. temperature: float,
  2131. threshold: float,
  2132. db: AsyncSession,
  2133. ):
  2134. """Handle AMS high temperature alarm event. Sent immediately, and also in the daily digest if enabled."""
  2135. providers = await self._get_providers_for_event(db, "on_ams_temperature_high", printer_id)
  2136. if not providers:
  2137. return
  2138. variables = {
  2139. "printer": printer_name,
  2140. "ams_label": ams_label,
  2141. "temperature": f"{temperature:.1f}",
  2142. "threshold": f"{threshold:.1f}",
  2143. }
  2144. title, message = await self._build_message_from_template(db, "ams_temperature_high", variables)
  2145. await self._send_to_providers(
  2146. providers,
  2147. title,
  2148. message,
  2149. db,
  2150. "ams_temperature_high",
  2151. printer_id,
  2152. printer_name,
  2153. variables=variables,
  2154. )
  2155. async def on_ams_drying_suspended(
  2156. self,
  2157. printer_id: int,
  2158. printer_name: str,
  2159. ams_label: str,
  2160. humidity: float,
  2161. threshold: float,
  2162. cycles: int,
  2163. db: AsyncSession,
  2164. *,
  2165. never_heated: bool = False,
  2166. ):
  2167. """Handle automatic drying giving up on one AMS unit (#2770).
  2168. ``never_heated`` sends the message for a unit whose printer accepted
  2169. the drying commands but never heated (#3478). Same provider switch:
  2170. either way Bambuddy has stopped drying that unit.
  2171. Worth sending immediately (as every notification is; a daily digest only
  2172. repeats it): it reports that
  2173. Bambuddy has STOPPED doing something, and a report of inaction that
  2174. arrives with tomorrow's summary has already cost the user a day.
  2175. """
  2176. providers = await self._get_providers_for_event(db, "on_ams_drying_suspended", printer_id)
  2177. if not providers:
  2178. return
  2179. variables = {
  2180. "printer": printer_name,
  2181. "ams_label": ams_label,
  2182. "humidity": f"{humidity:.0f}",
  2183. "threshold": f"{threshold:.0f}",
  2184. "cycles": str(cycles),
  2185. }
  2186. template = "ams_drying_not_heating" if never_heated else "ams_drying_suspended"
  2187. title, message = await self._build_message_from_template(db, template, variables)
  2188. await self._send_to_providers(
  2189. providers,
  2190. title,
  2191. message,
  2192. db,
  2193. "ams_drying_suspended",
  2194. printer_id,
  2195. printer_name,
  2196. variables=variables,
  2197. )
  2198. async def on_ams_ht_humidity_high(
  2199. self,
  2200. printer_id: int,
  2201. printer_name: str,
  2202. ams_label: str,
  2203. humidity: float,
  2204. threshold: float,
  2205. db: AsyncSession,
  2206. ):
  2207. """Handle AMS-HT high humidity alarm event. Sent immediately, and also in the daily digest if enabled."""
  2208. providers = await self._get_providers_for_event(db, "on_ams_ht_humidity_high", printer_id)
  2209. if not providers:
  2210. return
  2211. variables = {
  2212. "printer": printer_name,
  2213. "ams_label": ams_label,
  2214. "humidity": f"{humidity:.0f}",
  2215. "threshold": f"{threshold:.0f}",
  2216. }
  2217. # Use the same template as regular AMS (can create separate templates later if needed)
  2218. title, message = await self._build_message_from_template(db, "ams_humidity_high", variables)
  2219. await self._send_to_providers(
  2220. providers,
  2221. title,
  2222. message,
  2223. db,
  2224. "ams_ht_humidity_high",
  2225. printer_id,
  2226. printer_name,
  2227. variables=variables,
  2228. )
  2229. async def on_ams_ht_temperature_high(
  2230. self,
  2231. printer_id: int,
  2232. printer_name: str,
  2233. ams_label: str,
  2234. temperature: float,
  2235. threshold: float,
  2236. db: AsyncSession,
  2237. ):
  2238. """Handle AMS-HT high temperature alarm event. Sent immediately, and also in the daily digest if enabled."""
  2239. providers = await self._get_providers_for_event(db, "on_ams_ht_temperature_high", printer_id)
  2240. if not providers:
  2241. return
  2242. variables = {
  2243. "printer": printer_name,
  2244. "ams_label": ams_label,
  2245. "temperature": f"{temperature:.1f}",
  2246. "threshold": f"{threshold:.1f}",
  2247. }
  2248. # Use the same template as regular AMS (can create separate templates later if needed)
  2249. title, message = await self._build_message_from_template(db, "ams_temperature_high", variables)
  2250. await self._send_to_providers(
  2251. providers,
  2252. title,
  2253. message,
  2254. db,
  2255. "ams_ht_temperature_high",
  2256. printer_id,
  2257. printer_name,
  2258. variables=variables,
  2259. )
  2260. async def on_bed_cooled(
  2261. self,
  2262. printer_id: int,
  2263. printer_name: str,
  2264. bed_temp: float,
  2265. threshold: float,
  2266. filename: str,
  2267. db: AsyncSession,
  2268. ):
  2269. """Handle bed cooled event - bed temperature dropped below threshold after print."""
  2270. providers = await self._get_providers_for_event(db, "on_bed_cooled", printer_id)
  2271. if not providers:
  2272. return
  2273. variables = {
  2274. "printer": printer_name,
  2275. "bed_temp": f"{bed_temp:.0f}",
  2276. "threshold": f"{threshold:.0f}",
  2277. "filename": self._clean_filename(filename) if filename else "Unknown",
  2278. }
  2279. title, message = await self._build_message_from_template(db, "bed_cooled", variables)
  2280. await self._send_to_providers(
  2281. providers, title, message, db, "bed_cooled", printer_id, printer_name, variables=variables
  2282. )
  2283. async def on_ha_sensor_alert(
  2284. self,
  2285. printer_id: int,
  2286. printer_name: str,
  2287. sensor_name: str,
  2288. state: str,
  2289. db: AsyncSession,
  2290. ):
  2291. """A Home Assistant sensor bound to a printer entered its alert state (#1148).
  2292. Worth sending immediately (as every notification is; a daily digest only
  2293. repeats it): the case this exists
  2294. for is an enclosure door left open, which is only worth telling someone
  2295. about while they can still act on it.
  2296. """
  2297. providers = await self._get_providers_for_event(db, "on_ha_sensor_alert", printer_id)
  2298. if not providers:
  2299. return
  2300. variables = {
  2301. "printer": printer_name,
  2302. "sensor": sensor_name,
  2303. "state": state,
  2304. }
  2305. title, message = await self._build_message_from_template(db, "ha_sensor_alert", variables)
  2306. await self._send_to_providers(
  2307. providers,
  2308. title,
  2309. message,
  2310. db,
  2311. "ha_sensor_alert",
  2312. printer_id,
  2313. printer_name,
  2314. variables=variables,
  2315. )
  2316. async def on_location_ha_sensor_alert(
  2317. self,
  2318. location_name: str,
  2319. sensor_name: str,
  2320. state: str,
  2321. db: AsyncSession,
  2322. ):
  2323. """A Home Assistant sensor bound to a storage location entered its alert state (#2824).
  2324. Worth sending immediately, for the same reason
  2325. as on_ha_sensor_alert above: this is the "drybox went stale" case, only
  2326. worth acting on while the humidity/temperature is still climbing.
  2327. """
  2328. # Own column, not on_ha_sensor_alert (#2824): that one can be scoped to
  2329. # a single printer via provider.printer_id, and a location alert has no
  2330. # printer to scope by, so sharing it would leak drybox alerts to a
  2331. # provider narrowed to one printer's sensors.
  2332. providers = await self._get_providers_for_event(db, "on_location_ha_sensor_alert", fleet_wide=True)
  2333. if not providers:
  2334. return
  2335. variables = {
  2336. "location": location_name,
  2337. "sensor": sensor_name,
  2338. "state": state,
  2339. }
  2340. title, message = await self._build_message_from_template(db, "location_ha_sensor_alert", variables)
  2341. await self._send_to_providers(
  2342. providers,
  2343. title,
  2344. message,
  2345. db,
  2346. "location_ha_sensor_alert",
  2347. variables=variables,
  2348. )
  2349. async def on_first_layer_complete(
  2350. self,
  2351. printer_id: int,
  2352. printer_name: str,
  2353. filename: str,
  2354. total_layers: int,
  2355. db: AsyncSession,
  2356. image_data: bytes | None = None,
  2357. ):
  2358. """Handle first layer complete event."""
  2359. providers = await self._get_providers_for_event(db, "on_first_layer_complete", printer_id)
  2360. if not providers:
  2361. return
  2362. variables = {
  2363. "printer": printer_name,
  2364. "filename": self._clean_filename(filename),
  2365. "total_layers": str(total_layers),
  2366. }
  2367. title, message = await self._build_message_from_template(db, "first_layer_complete", variables)
  2368. await self._send_to_providers(
  2369. providers,
  2370. title,
  2371. message,
  2372. db,
  2373. "first_layer_complete",
  2374. printer_id,
  2375. printer_name,
  2376. image_data=image_data,
  2377. variables=variables,
  2378. )
  2379. async def on_print_pause_upcoming(
  2380. self,
  2381. printer_id: int,
  2382. printer_name: str,
  2383. filename: str,
  2384. *,
  2385. minutes: int,
  2386. layer: int,
  2387. total_layers: int,
  2388. db: AsyncSession,
  2389. ):
  2390. """A pause the slicer put into the print is coming up (#2984)."""
  2391. providers = await self._get_providers_for_event(db, "on_print_pause_upcoming", printer_id)
  2392. if not providers:
  2393. return
  2394. variables = {
  2395. "printer": printer_name,
  2396. "filename": self._clean_filename(filename),
  2397. # At least a minute: a pause seconds away still reads as "1m".
  2398. "time_to_pause": self._format_duration(max(minutes, 1) * 60),
  2399. "eta": await self._format_eta(max(minutes * 60, 1), db),
  2400. "layer": str(layer),
  2401. "total_layers": str(total_layers),
  2402. }
  2403. title, message = await self._build_message_from_template(db, "print_pause_upcoming", variables)
  2404. await self._send_to_providers(
  2405. providers,
  2406. title,
  2407. message,
  2408. db,
  2409. "print_pause_upcoming",
  2410. printer_id,
  2411. printer_name,
  2412. variables=variables,
  2413. )
  2414. def clear_template_cache(self):
  2415. """Clear the template cache. Call this when templates are updated."""
  2416. self._template_cache.clear()
  2417. async def send_user_print_email(
  2418. self,
  2419. event_type: str,
  2420. created_by_id: int | None,
  2421. printer_name: str,
  2422. filename: str,
  2423. db: AsyncSession,
  2424. image_data: bytes | None = None,
  2425. finish_photo_url: str | None = None,
  2426. ) -> None:
  2427. """Send a print event email notification to the user who submitted the job.
  2428. Args:
  2429. event_type: 'user_print_start', 'user_print_complete', 'user_print_failed', or 'user_print_stopped'
  2430. created_by_id: User ID who submitted the print job (from archive)
  2431. printer_name: Name of the printer
  2432. filename: Raw filename or subtask name
  2433. db: Database session
  2434. image_data: Camera snapshot bytes, if one was captured for the event.
  2435. finish_photo_url: The same snapshot's public URL — only used so the
  2436. template can reference {finish_photo_url}, which is what opts
  2437. the email into inlining the photo (see send_user_print_notification).
  2438. """
  2439. if created_by_id is None:
  2440. logger.debug("[EMAIL] Skipping user print email (%s): no created_by_id", event_type)
  2441. return
  2442. try:
  2443. # Check if advanced auth is enabled - required for user email notifications
  2444. from backend.app.models.settings import Settings
  2445. result = await db.execute(select(Settings).where(Settings.key == "advanced_auth_enabled"))
  2446. setting = result.scalar_one_or_none()
  2447. if not setting or setting.value.lower() != "true":
  2448. logger.debug("[EMAIL] Skipping user print email (%s): advanced_auth not enabled", event_type)
  2449. return
  2450. # Check if user notifications are enabled (admin-controlled toggle)
  2451. notif_enabled_result = await db.execute(
  2452. select(Settings).where(Settings.key == "user_notifications_enabled")
  2453. )
  2454. notif_enabled_setting = notif_enabled_result.scalar_one_or_none()
  2455. if notif_enabled_setting and notif_enabled_setting.value.lower() == "false":
  2456. logger.debug("[EMAIL] Skipping user print email (%s): user_notifications_enabled is false", event_type)
  2457. return
  2458. # Check SMTP settings are configured - required for sending emails
  2459. from backend.app.services.email_service import get_smtp_settings, send_user_print_notification
  2460. smtp_settings = await get_smtp_settings(db)
  2461. if not smtp_settings:
  2462. logger.debug("[EMAIL] Skipping user print email (%s): SMTP settings not configured", event_type)
  2463. return
  2464. # Load user preferences
  2465. from backend.app.models.user import User
  2466. from backend.app.models.user_email_pref import UserEmailPreference
  2467. user_result = await db.execute(select(User).where(User.id == created_by_id))
  2468. user = user_result.scalar_one_or_none()
  2469. if user is None or not user.email:
  2470. logger.debug(
  2471. "[EMAIL] Skipping user print email (%s): user %s not found or has no email address",
  2472. event_type,
  2473. created_by_id,
  2474. )
  2475. return
  2476. # Load user's notification preferences
  2477. pref_result = await db.execute(
  2478. select(UserEmailPreference).where(UserEmailPreference.user_id == created_by_id)
  2479. )
  2480. pref = pref_result.scalar_one_or_none()
  2481. # Determine if this event type should be sent
  2482. should_send = False
  2483. if event_type == "user_print_start":
  2484. should_send = pref is None or pref.notify_print_start
  2485. elif event_type == "user_print_complete":
  2486. should_send = pref is None or pref.notify_print_complete
  2487. elif event_type == "user_print_failed":
  2488. should_send = pref is None or pref.notify_print_failed
  2489. elif event_type == "user_print_stopped":
  2490. should_send = pref is None or pref.notify_print_stopped
  2491. if not should_send:
  2492. logger.debug(
  2493. "[EMAIL] Skipping user print email (%s): user %s has notifications disabled for this event",
  2494. event_type,
  2495. created_by_id,
  2496. )
  2497. return
  2498. logger.info(
  2499. "[EMAIL] Sending user print email: event=%s, user=%s (%s), printer=%s, file=%s",
  2500. event_type,
  2501. user.username,
  2502. user.email,
  2503. printer_name,
  2504. filename,
  2505. )
  2506. # Build variables
  2507. variables = {
  2508. "printer": printer_name,
  2509. "filename": self._clean_filename(filename),
  2510. }
  2511. if finish_photo_url:
  2512. variables["finish_photo_url"] = finish_photo_url
  2513. # Send the email
  2514. await send_user_print_notification(
  2515. db=db,
  2516. event_type=event_type,
  2517. user_email=user.email,
  2518. username=user.username,
  2519. variables=variables,
  2520. image_data=image_data,
  2521. )
  2522. logger.info("[EMAIL] User print email sent: event=%s → %s", event_type, user.email)
  2523. except Exception as e:
  2524. logger.warning("Failed to send user print email notification: %s", e, exc_info=True)
  2525. # ==================== Queue Notifications ====================
  2526. async def on_queue_job_added(
  2527. self,
  2528. job_name: str,
  2529. target: str,
  2530. db: AsyncSession,
  2531. printer_id: int | None = None,
  2532. printer_name: str | None = None,
  2533. ):
  2534. """Handle queue job added event."""
  2535. providers = await self._get_providers_for_event(db, "on_queue_job_added", printer_id)
  2536. if not providers:
  2537. return
  2538. variables = {
  2539. "job_name": job_name,
  2540. "target": target, # e.g., "Printer1" or "Any X1C"
  2541. "printer": printer_name or target,
  2542. }
  2543. title, message = await self._build_message_from_template(db, "queue_job_added", variables)
  2544. await self._send_to_providers(
  2545. providers, title, message, db, "queue_job_added", printer_id, printer_name, variables=variables
  2546. )
  2547. async def on_queue_job_assigned(
  2548. self,
  2549. job_name: str,
  2550. printer_id: int,
  2551. printer_name: str,
  2552. target_model: str,
  2553. db: AsyncSession,
  2554. ):
  2555. """Handle model-based job assigned to printer event."""
  2556. providers = await self._get_providers_for_event(db, "on_queue_job_assigned", printer_id)
  2557. if not providers:
  2558. return
  2559. variables = {
  2560. "job_name": job_name,
  2561. "printer": printer_name,
  2562. "target_model": target_model,
  2563. }
  2564. title, message = await self._build_message_from_template(db, "queue_job_assigned", variables)
  2565. await self._send_to_providers(
  2566. providers, title, message, db, "queue_job_assigned", printer_id, printer_name, variables=variables
  2567. )
  2568. async def on_queue_job_started(
  2569. self,
  2570. job_name: str,
  2571. printer_id: int,
  2572. printer_name: str,
  2573. db: AsyncSession,
  2574. estimated_time: int | None = None,
  2575. ):
  2576. """Handle queue job started printing event."""
  2577. providers = await self._get_providers_for_event(db, "on_queue_job_started", printer_id)
  2578. if not providers:
  2579. return
  2580. eta_str = await self._format_eta(estimated_time, db)
  2581. variables = {
  2582. "job_name": job_name,
  2583. "printer": printer_name,
  2584. "estimated_time": self._format_duration(estimated_time),
  2585. "eta": eta_str,
  2586. }
  2587. title, message = await self._build_message_from_template(db, "queue_job_started", variables)
  2588. await self._send_to_providers(
  2589. providers, title, message, db, "queue_job_started", printer_id, printer_name, variables=variables
  2590. )
  2591. async def on_queue_job_waiting(
  2592. self,
  2593. job_name: str,
  2594. target_model: str,
  2595. waiting_reason: str,
  2596. db: AsyncSession,
  2597. printer_id: int | None = None,
  2598. ):
  2599. """Handle job waiting for filament event.
  2600. ``printer_id`` is the printer a pinned job waits on; None for a job for
  2601. "Any <model>" that no printer holds.
  2602. """
  2603. providers = await self._get_providers_for_event(db, "on_queue_job_waiting", printer_id)
  2604. if not providers:
  2605. return
  2606. variables = {
  2607. "job_name": job_name,
  2608. "target_model": target_model,
  2609. "waiting_reason": waiting_reason,
  2610. }
  2611. title, message = await self._build_message_from_template(db, "queue_job_waiting", variables)
  2612. await self._send_to_providers(providers, title, message, db, "queue_job_waiting", variables=variables)
  2613. async def on_queue_job_skipped(
  2614. self,
  2615. job_name: str,
  2616. printer_id: int,
  2617. printer_name: str,
  2618. reason: str,
  2619. db: AsyncSession,
  2620. ):
  2621. """Handle job skipped event (e.g., previous print failed)."""
  2622. providers = await self._get_providers_for_event(db, "on_queue_job_skipped", printer_id)
  2623. if not providers:
  2624. return
  2625. variables = {
  2626. "job_name": job_name,
  2627. "printer": printer_name,
  2628. "reason": reason,
  2629. }
  2630. title, message = await self._build_message_from_template(db, "queue_job_skipped", variables)
  2631. await self._send_to_providers(
  2632. providers, title, message, db, "queue_job_skipped", printer_id, printer_name, variables=variables
  2633. )
  2634. async def on_queue_job_failed(
  2635. self,
  2636. job_name: str,
  2637. printer_id: int | None,
  2638. printer_name: str | None,
  2639. reason: str,
  2640. db: AsyncSession,
  2641. ):
  2642. """Handle job failed to start event (upload error, etc.)."""
  2643. providers = await self._get_providers_for_event(db, "on_queue_job_failed", printer_id)
  2644. if not providers:
  2645. return
  2646. variables = {
  2647. "job_name": job_name,
  2648. "printer": printer_name or "Unknown",
  2649. "reason": reason,
  2650. }
  2651. title, message = await self._build_message_from_template(db, "queue_job_failed", variables)
  2652. await self._send_to_providers(
  2653. providers, title, message, db, "queue_job_failed", printer_id, printer_name, variables=variables
  2654. )
  2655. async def on_queue_completed(
  2656. self,
  2657. completed_count: int,
  2658. db: AsyncSession,
  2659. ):
  2660. """Handle all queue jobs completed event."""
  2661. providers = await self._get_providers_for_event(db, "on_queue_completed", fleet_wide=True)
  2662. if not providers:
  2663. return
  2664. variables = {
  2665. "completed_count": str(completed_count),
  2666. }
  2667. title, message = await self._build_message_from_template(db, "queue_completed", variables)
  2668. await self._send_to_providers(providers, title, message, db, "queue_completed", variables=variables)
  2669. # ==================== Inventory Stock Alerts ====================
  2670. async def on_stock_reorder_alert(
  2671. self,
  2672. material: str,
  2673. brand: str | None,
  2674. stock_g: float,
  2675. rate_g_day: float,
  2676. days_left: int,
  2677. db: AsyncSession,
  2678. *,
  2679. subtype: str | None = None,
  2680. color: str | None = None,
  2681. skip_break_subscribers: bool = False,
  2682. ):
  2683. """Fire when an inventory SKU reaches its reorder point.
  2684. ``subtype`` and ``color`` are what tell apart the messages for two colours of one
  2685. product, which the forecast (grouped by colour as well) reports separately.
  2686. A SKU in stock break has also reached its reorder point. ``skip_break_subscribers``
  2687. leaves out the providers that have the break alert on, so the producer can send
  2688. the reorder event for a break SKU without telling those providers twice.
  2689. """
  2690. providers = await self._get_providers_for_event(db, "on_stock_reorder_alert", fleet_wide=True)
  2691. if skip_break_subscribers:
  2692. providers = [p for p in providers if not p.on_stock_break_alert]
  2693. if not providers:
  2694. return
  2695. variables = {
  2696. "material": material,
  2697. "subtype": subtype or "",
  2698. "brand": brand or "",
  2699. "color": color or "",
  2700. "stock_g": f"{stock_g:.0f}",
  2701. "rate_g_day": f"{rate_g_day:.1f}",
  2702. "days_left": str(days_left),
  2703. }
  2704. title, message = await self._build_message_from_template(db, "stock_reorder_alert", variables)
  2705. await self._send_to_providers(providers, title, message, db, "stock_reorder_alert", variables=variables)
  2706. async def on_stock_break_alert(
  2707. self,
  2708. material: str,
  2709. brand: str | None,
  2710. stock_g: float,
  2711. rate_g_day: float,
  2712. days_left: int,
  2713. lead_time_days: int,
  2714. db: AsyncSession,
  2715. *,
  2716. subtype: str | None = None,
  2717. color: str | None = None,
  2718. ):
  2719. """Fire when a stock break is detected (stock runs out before lead time).
  2720. ``subtype`` and ``color`` as for :meth:`on_stock_reorder_alert`.
  2721. """
  2722. providers = await self._get_providers_for_event(db, "on_stock_break_alert", fleet_wide=True)
  2723. if not providers:
  2724. return
  2725. variables = {
  2726. "material": material,
  2727. "subtype": subtype or "",
  2728. "brand": brand or "",
  2729. "color": color or "",
  2730. "stock_g": f"{stock_g:.0f}",
  2731. "rate_g_day": f"{rate_g_day:.1f}",
  2732. "days_left": str(days_left),
  2733. "lead_time_days": str(lead_time_days),
  2734. }
  2735. title, message = await self._build_message_from_template(db, "stock_break_alert", variables)
  2736. await self._send_to_providers(providers, title, message, db, "stock_break_alert", variables=variables)
  2737. async def _queue_for_digest(
  2738. self,
  2739. provider: NotificationProvider,
  2740. event_type: str,
  2741. title: str,
  2742. message: str,
  2743. db: AsyncSession,
  2744. printer_id: int | None = None,
  2745. printer_name: str | None = None,
  2746. ):
  2747. """Queue a notification for later delivery in the daily digest."""
  2748. try:
  2749. queue_entry = NotificationDigestQueue(
  2750. provider_id=provider.id,
  2751. event_type=event_type,
  2752. title=title,
  2753. message=message,
  2754. printer_id=printer_id,
  2755. printer_name=printer_name,
  2756. )
  2757. db.add(queue_entry)
  2758. await db.commit()
  2759. logger.info("Queued notification for digest: %s for provider %s", event_type, provider.name)
  2760. except Exception as e:
  2761. logger.warning("Failed to queue notification for digest: %s", e)
  2762. async def send_digest(self, provider_id: int):
  2763. """Send all queued notifications as a single digest for a provider."""
  2764. from backend.app.core.database import async_session
  2765. async with async_session() as db:
  2766. # Get the provider
  2767. result = await db.execute(select(NotificationProvider).where(NotificationProvider.id == provider_id))
  2768. provider = result.scalar_one_or_none()
  2769. if not provider or not provider.enabled:
  2770. return
  2771. # Get all queued notifications for this provider
  2772. result = await db.execute(
  2773. select(NotificationDigestQueue)
  2774. .where(NotificationDigestQueue.provider_id == provider_id)
  2775. .order_by(NotificationDigestQueue.created_at)
  2776. )
  2777. queue_entries = list(result.scalars().all())
  2778. # Events kept after a failed digest get the next one, not every
  2779. # one after it: a provider whose digest always fails (revoked
  2780. # credentials, a busy day too long for Pushover) would otherwise
  2781. # grow its queue without end. Before FO-C4 a failed day was
  2782. # simply deleted.
  2783. cutoff = datetime.utcnow() - timedelta(hours=DIGEST_RETRY_HOURS)
  2784. expired = [e for e in queue_entries if e.created_at is not None and e.created_at < cutoff]
  2785. for entry in expired:
  2786. await db.delete(entry)
  2787. queue_entries = [e for e in queue_entries if e not in expired]
  2788. if not queue_entries:
  2789. await db.commit()
  2790. logger.debug("No queued notifications for provider %s", provider.name)
  2791. return
  2792. # Build digest message
  2793. title = f"Daily Digest - {len(queue_entries)} Events"
  2794. # Group by event type
  2795. events_by_type: dict[str, list] = {}
  2796. for entry in queue_entries:
  2797. if entry.event_type not in events_by_type:
  2798. events_by_type[entry.event_type] = []
  2799. events_by_type[entry.event_type].append(entry)
  2800. # Format the digest body
  2801. body_parts = []
  2802. for event_type, entries in events_by_type.items():
  2803. event_label = event_type.replace("_", " ").title()
  2804. body_parts.append(f"== {event_label} ({len(entries)}) ==")
  2805. for entry in entries:
  2806. # From an earlier day (late yesterday, or kept from a
  2807. # failed digest): say which day.
  2808. if entry.created_at.date() != datetime.utcnow().date():
  2809. time_str = entry.created_at.strftime("%m-%d %H:%M")
  2810. else:
  2811. time_str = entry.created_at.strftime("%H:%M")
  2812. printer_info = f"[{entry.printer_name}] " if entry.printer_name else ""
  2813. body_parts.append(f" {time_str} {printer_info}{entry.title}")
  2814. body_parts.append("")
  2815. body = "\n".join(body_parts)
  2816. # Send the digest. At its own time even inside quiet hours: skipped
  2817. # there, it was logged as sent and its events deleted (FO-C4).
  2818. success, error = await self._send_to_provider(provider, title, body, db, bypass_quiet_hours=True)
  2819. # Log the digest
  2820. await self._log_notification(
  2821. db=db,
  2822. provider_id=provider.id,
  2823. event_type="daily_digest",
  2824. title=title,
  2825. message=body,
  2826. success=success,
  2827. error_message=error if not success else None,
  2828. )
  2829. # Clear the queue once it went out; a failed send keeps the events
  2830. # for the next digest instead of losing them (FO-C4).
  2831. if success:
  2832. for entry in queue_entries:
  2833. await db.delete(entry)
  2834. await db.commit()
  2835. if success:
  2836. logger.info("Sent daily digest with %s events to %s", len(queue_entries), provider.name)
  2837. else:
  2838. logger.warning("Failed to send daily digest to %s: %s", provider.name, error)
  2839. async def check_and_send_digests(self):
  2840. """Check all providers and send digests if it's their scheduled time."""
  2841. from backend.app.core.database import async_session
  2842. current_time = datetime.now().strftime("%H:%M")
  2843. # Avoid duplicate checks within the same minute
  2844. if current_time == self._last_digest_check:
  2845. return
  2846. self._last_digest_check = current_time
  2847. async with async_session() as db:
  2848. # Find all providers with digest enabled at this time
  2849. result = await db.execute(
  2850. select(NotificationProvider).where(
  2851. NotificationProvider.enabled.is_(True),
  2852. NotificationProvider.daily_digest_enabled.is_(True),
  2853. NotificationProvider.daily_digest_time == current_time,
  2854. )
  2855. )
  2856. providers = result.scalars().all()
  2857. for provider in providers:
  2858. try:
  2859. await self.send_digest(provider.id)
  2860. except Exception as e:
  2861. logger.error("Error sending digest for provider %s: %s", provider.id, e)
  2862. def start_digest_scheduler(self):
  2863. """Start the background scheduler for daily digest notifications."""
  2864. if self._digest_scheduler_task is None:
  2865. self._digest_scheduler_task = asyncio.create_task(self._digest_scheduler_loop())
  2866. logger.info("Notification digest scheduler started")
  2867. def stop_digest_scheduler(self):
  2868. """Stop the background scheduler for daily digests."""
  2869. if self._digest_scheduler_task:
  2870. self._digest_scheduler_task.cancel()
  2871. self._digest_scheduler_task = None
  2872. logger.info("Notification digest scheduler stopped")
  2873. async def _digest_scheduler_loop(self):
  2874. """Background loop that checks for scheduled digests every minute."""
  2875. while True:
  2876. try:
  2877. await self.check_and_send_digests()
  2878. except Exception as e:
  2879. logger.error("Error in digest scheduler: %s", e)
  2880. # Wait until the next minute
  2881. await asyncio.sleep(60)
  2882. # Global instance
  2883. notification_service = NotificationService()