| 1 | == Extension Point : ''INotificationDistributor'' == |
| 2 | |
| 3 | ||'''Interface'''||''INotificationDistributor''||'''Since'''||[wiki:TracDev/ApiChanges/1.1.3#INotificationDistributor 1.1.3]|| |
| 4 | ||'''Module'''||''trac.notification''||'''Source'''||[source:trunk/trac/notification/api.py#/INotificationDistributor api.py]|| |
| 5 | |
| 6 | |
| 7 | The ''INotificationDistributor'' sends [TracNotification notification] events over some distribution channel, like email. |
| 8 | |
| 9 | == Purpose == |
| 10 | |
| 11 | 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. |
| 12 | |
| 13 | == Usage == |
| 14 | |
| 15 | Implementing the interface follows the standard guidelines found in [wiki:TracDev/ComponentArchitecture] and of course [wiki:TracDev/PluginDevelopment]. |
| 16 | |
| 17 | A simple `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: |
| 18 | * `transport`: The name of the transports that should be used. One of those returned by `transports()`. |
| 19 | * `recipients`: A list of `(sid, authenticated, address, format)` tuples. |
| 20 | * `sid`: The Trac session ID of the recipient (or `None`). |
| 21 | * `authenticated`: `1` if the Trac session ID is authenticated, `0` otherwise. |
| 22 | * `address`: A protocol specific address to send the notification to. If it is `None` the implementation might still be able to send the message to the recipient using the `sid` / `authenticated` information somehow. (E.g. the `EmailDistributor` falls back to [wiki:IEmailAddressResolver IEmailAddressResolver] implementations.) |
| 23 | * `format`: MIME type of the message format to be used. |
| 24 | * `event`: A `trac.notification.api.NotificationEvent` instance describing the event about which the recipients should be notified. |
| 25 | |
| 26 | The `event` is supposed to be formatted into a message in the given `format` using a [wiki:trac.notification.api.INotificationFormatter INotificationFormatter]. |
| 27 | |
| 28 | == Examples == |
| 29 | |
| 30 | The following example distributes notifications by SMS. (The `sms` library does not exist. Several commercial SMS services provide real APIs.) |
| 31 | |
| 32 | {{{#!python |
| 33 | from itertools import groupby |
| 34 | from operator import itemgetter |
| 35 | |
| 36 | from trac.core import * |
| 37 | from trac.notification.api import INotificationDistributor, \ |
| 38 | INotificationFormatter |
| 39 | |
| 40 | import sms |
| 41 | |
| 42 | class SmsNotificationDistributor(Component): |
| 43 | |
| 44 | implements(INotificationDistributor) |
| 45 | |
| 46 | formatters = ExtensionPoint(INotificationFormatter) |
| 47 | |
| 48 | # INotificationDistributor methods |
| 49 | |
| 50 | def transports(self): |
| 51 | return ['sms'] |
| 52 | |
| 53 | def distribute(self, transport, recipients, event): |
| 54 | if transport != 'sms': |
| 55 | return |
| 56 | |
| 57 | # Group by message format: |
| 58 | for format, group in groupby(recipients, itemgetter(3)): |
| 59 | formatter = None |
| 60 | for f in self.formatters: |
| 61 | for style in f.styles(transport, event.realm): |
| 62 | if style == format: |
| 63 | formatter = f |
| 64 | if formatter: |
| 65 | message = formatter.format(transport, format, event) |
| 66 | for sid, authenticated, address, format in group: |
| 67 | if address: |
| 68 | sms.send(message, address) |
| 69 | }}} |
| 70 | |
| 71 | == Available Implementations == |
| 72 | |
| 73 | * [source:trunk/trac/notification/mail.py#/EmailDistributor trac.notification.mail.EmailDistributor] |
| 74 | |
| 75 | == Additional Information and References == |
| 76 | |
| 77 | * [http://www.edgewall.org/docs/trac-trunk/epydoc/trac.notification.api.INotificationDistributor-class.html epydoc] |
| 78 | * [http://www.edgewall.org/docs/trac-trunk/html/api/trac_notification.html#trac.notification.api.INotificationDistributor API Reference] |
| 79 | * Related to the [wiki:trac.notification.api.INotificationFormatter INotificationFormatter] |
| 80 | |
| 81 | === History |
| 82 | * This interface originated in th:AnnouncerPlugin as `IAnnouncementDistributor`. |
| 83 | * [wiki:TracDev/ApiChanges/1.1.3#INotificationDistributor 1.1.3]: Integrated `INotificationDistributor` in Trac as part of [wiki:TracDev/Proposals/AdvancedNotification this proposal] (#3517) |
| 84 | * Added `format` in the `recipients` parameter to `distribute()`. |