|Version 3 (modified by 5 years ago) ( diff ),|
Extension Point : INotificationDistributor
The INotificationDistributor sends notification events over some distribution channel, like email.
Trac provides an extendible and flexible notification system, that historically has sent notifications by email. Notifications could however also be distributed by XMPP, SMS or any other messaging protocol.
transports() method lists the names of the supported transports (protocols / distribution channels). The
distribute() method distributes a message to one or more recipients. It receives the following parameters:
transport: The name of the transports that should be used. One of those returned by
recipients: A list of
(sid, authenticated, address, format)tuples.
sid: The Trac session ID of the recipient (or
1if the Trac session ID is authenticated,
address: A protocol specific address to send the notification to. If it is
Nonethe implementation might still be able to send the message to the recipient using the
authenticatedinformation somehow. (E.g. the
EmailDistributorfalls back to IEmailAddressResolver implementations.)
format: MIME type of the message format to be used.
trac.notification.api.NotificationEventinstance describing the event about which the recipients should be notified.
event is supposed to be formatted into a message in the given
format using a INotificationFormatter.
The following example distributes notifications by SMS. (The
sms library does not exist. Several commercial SMS services provide real APIs.)
from itertools import groupby from operator import itemgetter from trac.core import * from trac.notification.api import INotificationDistributor, \ INotificationFormatter import sms class SmsNotificationDistributor(Component): implements(INotificationDistributor) formatters = ExtensionPoint(INotificationFormatter) # INotificationDistributor methods def transports(self): return ['sms'] def distribute(self, transport, recipients, event): if transport != 'sms': return # Group by message format: for format, group in groupby(recipients, itemgetter(3)): formatter = None for f in self.formatters: for style in f.styles(transport, event.realm): if style == format: formatter = f if formatter: message = formatter.format(transport, format, event) for sid, authenticated, address, format in group: if address: sms.send(message, address)