Skip to content

Commit 42664b0

Browse files
committed
Merge docs about Notifier
1 parent d7ffe5c commit 42664b0

File tree

4 files changed

+152
-188
lines changed

4 files changed

+152
-188
lines changed

notifier.rst

Lines changed: 152 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5150
SMS 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

175213
Chat Channel
176214
~~~~~~~~~~~~
@@ -273,6 +311,39 @@ Chatters are configured using the ``chatter_transports`` setting:
273311
;
274312
};
275313
314+
The :class:`Symfony\\Component\\Notifier\\ChatterInterface` class allows
315+
you to send messages to chat services::
316+
317+
// src/Controller/CheckoutController.php
318+
namespace App\Controller;
319+
320+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
321+
use Symfony\Component\Notifier\ChatterInterface;
322+
use Symfony\Component\Notifier\Message\ChatMessage;
323+
use Symfony\Component\Routing\Annotation\Route;
324+
325+
class CheckoutController extends AbstractController
326+
{
327+
/**
328+
* @Route("/checkout/thankyou")
329+
*/
330+
public function thankyou(ChatterInterface $chatter)
331+
{
332+
$message = (new ChatMessage('You got a new invoice for 15 EUR.'))
333+
// if not set explicitly, the message is send to the
334+
// default transport (the first one configured)
335+
->transport('slack');
336+
337+
$sentMessage = $chatter->send($message);
338+
339+
// ...
340+
}
341+
}
342+
343+
The ``send()`` method returns a variable of type
344+
:class:`Symfony\\Component\\Notifier\\Message\\SentMessage` which provides
345+
information such as the message ID and the original message contents.
346+
276347
.. _notifier-email-channel:
277348

278349
Email Channel
@@ -748,18 +819,88 @@ all configured texter and chatter transports only in the ``dev`` (and/or
748819
chatter_transports:
749820
slack: 'null://null'
750821
822+
.. index::
823+
single: Notifier; Events
824+
825+
Using Events
826+
------------
827+
828+
.. versionadded:: 5.4
829+
830+
The ``MessageEvent``, ``FailedMessageEvent`` and ``SentMessageEvent`` were
831+
introduced in Symfony 5.4.
832+
833+
The :class:`Symfony\\Component\\Notifier\\Transport`` class of the Notifier component
834+
allows you to optionally hook into the lifecycle via events.
835+
836+
The ``MessageEvent::class`` Event
837+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
838+
839+
**Typical Purposes**: Doing something before the message is send (like logging
840+
which message is going to be send, or displaying something about the event
841+
to be executed.
842+
843+
Just before send the message, the event class ``MessageEvent`` is
844+
dispatched. Listeners receive a
845+
:class:`Symfony\\Component\\Notifier\\Event\\MessageEvent` event::
846+
847+
use Symfony\Component\Notifier\Event\MessageEvent;
848+
849+
$dispatcher->addListener(MessageEvent::class, function (MessageEvent $event) {
850+
// gets the message instance
851+
$message = $event->getMessage();
852+
853+
// log something
854+
$this->logger(sprintf('Message with subject: %s will be send to %s, $message->getSubject(), $message->getRecipientId()'));
855+
});
856+
857+
The ``FailedMessageEvent`` Event
858+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
859+
860+
**Typical Purposes**: Doing something before the exception is thrown (Retry to send the message or log additional information).
861+
862+
Whenever an exception is thrown while sending the message, the event class ``FailedMessageEvent`` is
863+
dispatched. A listener can do anything useful before the exception is thrown.
864+
865+
Listeners receive a
866+
:class:`Symfony\\Component\\Notifier\\Event\\FailedMessageEvent` event::
867+
868+
use Symfony\Component\Notifier\Event\FailedMessageEvent;
869+
870+
$dispatcher->addListener(FailedMessageEvent::class, function (FailedMessageEvent $event) {
871+
// gets the message instance
872+
$message = $event->getMessage();
873+
874+
// gets the error instance
875+
$error = $event->getError();
876+
877+
// log something
878+
$this->logger(sprintf('The message with subject: %s has not been sent successfully. The error is: %s, $message->getSubject(), $error->getMessage()'));
879+
});
880+
881+
The ``SentMessageEvent`` Event
882+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
883+
884+
**Typical Purposes**: To perform some action when the message is successfully sent (like retrieve the id returned
885+
when the message is sent).
886+
887+
After the message has been successfully sent, the event class ``SentMessageEvent`` is
888+
dispatched. Listeners receive a
889+
:class:`Symfony\\Component\\Notifier\\Event\\SentMessageEvent` event::
890+
891+
use Symfony\Component\Notifier\Event\SentMessageEvent;
892+
893+
$dispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) {
894+
// gets the message instance
895+
$message = $event->getOriginalMessage();
896+
897+
// log something
898+
$this->logger(sprintf('The message has been successfully sent and have id: %s, $message->getMessageId()'));
899+
});
900+
751901
.. TODO
752902
.. - Using the message bus for asynchronous notification
753903
.. - Describe notifier monolog handler
754904
.. - Describe notification_on_failed_messages integration
755905
756-
Learn more
757-
----------
758-
759-
.. toctree::
760-
:maxdepth: 1
761-
:glob:
762-
763-
notifier/*
764-
765906
.. _`RFC 3986`: https://www.ietf.org/rfc/rfc3986.txt

notifier/chatters.rst

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,6 @@
44
How to send Chat Messages
55
=========================
66

7-
.. versionadded:: 5.0
8-
9-
The Notifier component was introduced in Symfony 5.0.
10-
11-
The :class:`Symfony\\Component\\Notifier\\ChatterInterface` class allows
12-
you to send messages to chat services like Slack or Telegram::
13-
14-
// src/Controller/CheckoutController.php
15-
namespace App\Controller;
16-
17-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
18-
use Symfony\Component\Notifier\ChatterInterface;
19-
use Symfony\Component\Notifier\Message\ChatMessage;
20-
use Symfony\Component\Routing\Annotation\Route;
21-
22-
class CheckoutController extends AbstractController
23-
{
24-
/**
25-
* @Route("/checkout/thankyou")
26-
*/
27-
public function thankyou(ChatterInterface $chatter)
28-
{
29-
$message = (new ChatMessage('You got a new invoice for 15 EUR.'))
30-
// if not set explicitly, the message is send to the
31-
// default transport (the first one configured)
32-
->transport('slack');
33-
34-
$sentMessage = $chatter->send($message);
35-
36-
// ...
37-
}
38-
}
39-
40-
The ``send()`` method returns a variable of type
41-
:class:`Symfony\\Component\\Notifier\\Message\\SentMessage` which provides
42-
information such as the message ID and the original message contents.
43-
44-
.. versionadded:: 5.2
45-
46-
The ``SentMessage`` class was introduced in Symfony 5.2.
47-
48-
.. seealso::
49-
50-
Read :ref:`the main Notifier guide <notifier-chatter-dsn>` to see how
51-
to configure the different transports.
52-
537
Adding Interactions to a Slack Message
548
--------------------------------------
559

notifier/events.rst

Lines changed: 0 additions & 79 deletions
This file was deleted.

notifier/texters.rst

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)