@@ -46,7 +46,6 @@ The notifier component supports the following channels:
4646 API's tokens.
4747
4848.. _notifier-sms-channel :
49- .. _notifier-texter-dsn :
5049
5150SMS Channel
5251~~~~~~~~~~~
@@ -169,8 +168,47 @@ configure the ``texter_transports``:
169168 ;
170169 };
171170
171+ .. _sending-sms :
172+
173+ The:class: `Symfony\\ Component\\ Notifier\\ TexterInterface ` class allows you to
174+ send SMS messages::
175+
176+ // src/Controller/SecurityController.php
177+ namespace App\Controller;
178+
179+ use Symfony\Component\Notifier\Message\SmsMessage;
180+ use Symfony\Component\Notifier\TexterInterface;
181+ use Symfony\Component\Routing\Annotation\Route;
182+
183+ class SecurityController
184+ {
185+ /**
186+ * @Route("/login/success")
187+ */
188+ public function loginSuccess(TexterInterface $texter)
189+ {
190+ $sms = new SmsMessage(
191+ // the phone number to send the SMS message to
192+ '+1411111111',
193+ // the message
194+ 'A new login was detected!'
195+ );
196+
197+ $sentMessage = $texter->send($sms);
198+
199+ // ...
200+ }
201+ }
202+
203+ The ``send() `` method returns a variable of type
204+ :class: `Symfony\\ Component\\ Notifier\\ Message\\ SentMessage ` which provides
205+ information such as the message ID and the original message contents.
206+
207+ ..versionadded ::5.2
208+
209+ The ``SentMessage `` class was introduced in Symfony 5.2.
210+
172211.. _notifier-chat-channel :
173- .. _notifier-chatter-dsn :
174212
175213Chat Channel
176214~~~~~~~~~~~~
@@ -186,24 +224,24 @@ The chat channel is used to send chat messages to users by using
186224:class: `Symfony\\ Component\\ Notifier\\ Chatter ` classes. Symfony provides
187225integration with these chat services:
188226
189- ============== ==================================== =============================================================================
190- Service Package DSN
191- ============== ==================================== =============================================================================
192- AmazonSns ``symfony/amazon-sns-notifier `` ``sns://ACCESS_KEY:SECRET_KEY@default?region=REGION ``
193- Discord ``symfony/discord-notifier `` ``discord://TOKEN@default?webhook_id=ID ``
194- FakeChat ``symfony/fake-chat-notifier `` ``fakechat+email://default?to=TO&from=FROM `` or ``fakechat+logger://default ``
195- Firebase ``symfony/firebase-notifier `` ``firebase://USERNAME:PASSWORD@default ``
196- Gitter ``symfony/gitter-notifier `` ``gitter://TOKEN@default?room_id=ROOM_ID ``
197- GoogleChat ``symfony/google-chat-notifier `` ``googlechat://ACCESS_KEY:ACCESS_TOKEN@default/SPACE?thread_key=THREAD_KEY ``
198- LinkedIn ``symfony/linked-in-notifier `` ``linkedin://TOKEN:USER_ID@default ``
199- Mattermost ``symfony/mattermost-notifier `` ``mattermost://ACCESS_TOKEN@HOST/PATH?channel=CHANNEL ``
200- Mercure ``symfony/mercure-notifier `` ``mercure://HUB_ID?topic=TOPIC ``
201- MicrosoftTeams ``symfony/microsoft-teams-notifier `` ``microsoftteams://default/PATH ``
202- RocketChat ``symfony/rocket-chat-notifier `` ``rocketchat://TOKEN@ENDPOINT?channel=CHANNEL ``
203- Slack ``symfony/slack-notifier `` ``slack://TOKEN@default?channel=CHANNEL ``
204- Telegram ``symfony/telegram-notifier `` ``telegram://TOKEN@default?channel=CHAT_ID ``
205- Zulip ``symfony/zulip-notifier `` ``zulip://EMAIL:TOKEN@HOST?channel=CHANNEL ``
206- ============== ==================================== =============================================================================
227+ ====================================== ==================================== =============================================================================
228+ Service Package DSN
229+ ====================================== ==================================== =============================================================================
230+ AmazonSns ``symfony/amazon-sns-notifier `` ``sns://ACCESS_KEY:SECRET_KEY@default?region=REGION ``
231+ :doc: ` Discord < notifier/discord >` ``symfony/discord-notifier `` ``discord://TOKEN@default?webhook_id=ID ``
232+ FakeChat ``symfony/fake-chat-notifier `` ``fakechat+email://default?to=TO&from=FROM `` or ``fakechat+logger://default ``
233+ Firebase ``symfony/firebase-notifier `` ``firebase://USERNAME:PASSWORD@default ``
234+ Gitter ``symfony/gitter-notifier `` ``gitter://TOKEN@default?room_id=ROOM_ID ``
235+ GoogleChat ``symfony/google-chat-notifier `` ``googlechat://ACCESS_KEY:ACCESS_TOKEN@default/SPACE?thread_key=THREAD_KEY ``
236+ LinkedIn ``symfony/linked-in-notifier `` ``linkedin://TOKEN:USER_ID@default ``
237+ Mattermost ``symfony/mattermost-notifier `` ``mattermost://ACCESS_TOKEN@HOST/PATH?channel=CHANNEL ``
238+ Mercure ``symfony/mercure-notifier `` ``mercure://HUB_ID?topic=TOPIC ``
239+ :doc: ` MicrosoftTeams < notifier/teams >` ``symfony/microsoft-teams-notifier `` ``microsoftteams://default/PATH ``
240+ RocketChat ``symfony/rocket-chat-notifier `` ``rocketchat://TOKEN@ENDPOINT?channel=CHANNEL ``
241+ :doc: ` Slack < notifier/slack >` ``symfony/slack-notifier `` ``slack://TOKEN@default?channel=CHANNEL ``
242+ :doc: ` Telegram < notifier/telegram >` ``symfony/telegram-notifier `` ``telegram://TOKEN@default?channel=CHAT_ID ``
243+ Zulip ``symfony/zulip-notifier `` ``zulip://EMAIL:TOKEN@HOST?channel=CHANNEL ``
244+ ====================================== ==================================== =============================================================================
207245
208246..versionadded ::5.1
209247
@@ -273,6 +311,41 @@ Chatters are configured using the ``chatter_transports`` setting:
273311 ;
274312 };
275313
314+ .. _sending-chat-messages :
315+
316+ The:class: `Symfony\\ Component\\ Notifier\\ ChatterInterface ` class allows
317+ you to send messages to chat services::
318+
319+ // src/Controller/CheckoutController.php
320+ namespace App\Controller;
321+
322+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
323+ use Symfony\Component\Notifier\ChatterInterface;
324+ use Symfony\Component\Notifier\Message\ChatMessage;
325+ use Symfony\Component\Routing\Annotation\Route;
326+
327+ class CheckoutController extends AbstractController
328+ {
329+ /**
330+ * @Route("/checkout/thankyou")
331+ */
332+ public function thankyou(ChatterInterface $chatter)
333+ {
334+ $message = (new ChatMessage('You got a new invoice for 15 EUR.'))
335+ // if not set explicitly, the message is send to the
336+ // default transport (the first one configured)
337+ ->transport('slack');
338+
339+ $sentMessage = $chatter->send($message);
340+
341+ // ...
342+ }
343+ }
344+
345+ The ``send() `` method returns a variable of type
346+ :class: `Symfony\\ Component\\ Notifier\\ Message\\ SentMessage ` which provides
347+ information such as the message ID and the original message contents.
348+
276349.. _notifier-email-channel :
277350
278351Email Channel
@@ -750,18 +823,90 @@ all configured texter and chatter transports only in the ``dev`` (and/or
750823chatter_transports :
751824slack :' null://null'
752825
826+ .. _notifier-events :
827+
828+ ..index ::
829+ single: Notifier; Events
830+
831+ Using Events
832+ ------------
833+
834+ ..versionadded ::5.4
835+
836+ The ``MessageEvent ``, ``FailedMessageEvent `` and ``SentMessageEvent `` were
837+ introduced in Symfony 5.4.
838+
839+ The:class: `Symfony\\ Component\\ Notifier\\ Transport` ` class of the Notifier component
840+ allows you to optionally hook into the lifecycle via events.
841+
842+ The ``MessageEvent::class `` Event
843+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
844+
845+ **Typical Purposes **: Doing something before the message is send (like logging
846+ which message is going to be send, or displaying something about the event
847+ to be executed.
848+
849+ Just before send the message, the event class ``MessageEvent `` is
850+ dispatched. Listeners receive a
851+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ MessageEvent ` event::
852+
853+ use Symfony\Component\Notifier\Event\MessageEvent;
854+
855+ $dispatcher->addListener(MessageEvent::class, function (MessageEvent $event) {
856+ // gets the message instance
857+ $message = $event->getMessage();
858+
859+ // log something
860+ $this->logger(sprintf('Message with subject: %s will be send to %s, $message->getSubject(), $message->getRecipientId()'));
861+ });
862+
863+ The ``FailedMessageEvent `` Event
864+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
865+
866+ **Typical Purposes **: Doing something before the exception is thrown (Retry to send the message or log additional information).
867+
868+ Whenever an exception is thrown while sending the message, the event class ``FailedMessageEvent `` is
869+ dispatched. A listener can do anything useful before the exception is thrown.
870+
871+ Listeners receive a
872+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ FailedMessageEvent ` event::
873+
874+ use Symfony\Component\Notifier\Event\FailedMessageEvent;
875+
876+ $dispatcher->addListener(FailedMessageEvent::class, function (FailedMessageEvent $event) {
877+ // gets the message instance
878+ $message = $event->getMessage();
879+
880+ // gets the error instance
881+ $error = $event->getError();
882+
883+ // log something
884+ $this->logger(sprintf('The message with subject: %s has not been sent successfully. The error is: %s, $message->getSubject(), $error->getMessage()'));
885+ });
886+
887+ The ``SentMessageEvent `` Event
888+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
889+
890+ **Typical Purposes **: To perform some action when the message is successfully sent (like retrieve the id returned
891+ when the message is sent).
892+
893+ After the message has been successfully sent, the event class ``SentMessageEvent `` is
894+ dispatched. Listeners receive a
895+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ SentMessageEvent ` event::
896+
897+ use Symfony\Component\Notifier\Event\SentMessageEvent;
898+
899+ $dispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) {
900+ // gets the message instance
901+ $message = $event->getOriginalMessage();
902+
903+ // log something
904+ $this->logger(sprintf('The message has been successfully sent and have id: %s, $message->getMessageId()'));
905+ });
906+
753907.. TODO
754908.. - Using the message bus for asynchronous notification
755909.. - Describe notifier monolog handler
756910.. - Describe notification_on_failed_messages integration
757911
758- Learn more
759- ----------
760-
761- ..toctree ::
762- :maxdepth: 1
763- :glob:
764-
765- notifier/*
766-
767912 .. _`RFC 3986` :https://www.ietf.org/rfc/rfc3986.txt