@@ -42,7 +42,6 @@ The notifier component supports the following channels:
42
42
API's tokens.
43
43
44
44
.. _notifier-sms-channel :
45
- .. _notifier-texter-dsn :
46
45
47
46
SMS Channel
48
47
~~~~~~~~~~~
@@ -169,8 +168,47 @@ configure the ``texter_transports``:
169
168
;
170
169
};
171
170
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
+ #[Route('/login/success')]
186
+ public function loginSuccess(TexterInterface $texter)
187
+ {
188
+ $sms = new SmsMessage(
189
+ // the phone number to send the SMS message to
190
+ '+1411111111',
191
+ // the message
192
+ 'A new login was detected!',
193
+ // optionally, you can override default "from" defined in transports
194
+ '+1422222222',
195
+ );
196
+
197
+ $sentMessage = $texter->send($sms);
198
+
199
+ // ...
200
+ }
201
+ }
202
+
203
+ .. versionadded :: 6.2
204
+
205
+ The 3rd argument of ``SmsMessage `` (``$from ``) was introduced in Symfony 6.2.
206
+
207
+ The ``send() `` method returns a variable of type
208
+ :class: `Symfony\\ Component\\ Notifier\\ Message\\ SentMessage ` which provides
209
+ information such as the message ID and the original message contents.
210
+
172
211
.. _notifier-chat-channel :
173
- .. _notifier-chatter-dsn :
174
212
175
213
Chat Channel
176
214
~~~~~~~~~~~~
@@ -186,29 +224,29 @@ The chat channel is used to send chat messages to users by using
186
224
:class: `Symfony\\ Component\\ Notifier\\ Chatter ` classes. Symfony provides
187
225
integration with these chat services:
188
226
189
- ============== ==================================== == =============================================================================
190
- Service Package DSN
191
- ============== ==================================== == =============================================================================
192
- AmazonSns ``symfony/amazon-sns-notifier `` ``sns://ACCESS_KEY:SECRET_KEY@default?region=REGION ``
193
- Chatwork ``symfony/chatwork-notifier `` ``chatwork://API_TOKEN@default?room_id=ID ``
194
- Discord ``symfony/discord-notifier `` ``discord://TOKEN@default?webhook_id=ID ``
195
- FakeChat ``symfony/fake-chat-notifier `` ``fakechat+email://default?to=TO&from=FROM `` or ``fakechat+logger://default ``
196
- Firebase ``symfony/firebase-notifier `` ``firebase://USERNAME:PASSWORD@default ``
197
- Gitter ``symfony/gitter-notifier `` ``gitter://TOKEN@default?room_id=ROOM_ID ``
198
- GoogleChat ``symfony/google-chat-notifier `` ``googlechat://ACCESS_KEY:ACCESS_TOKEN@default/SPACE?thread_key=THREAD_KEY ``
199
- LINE Notify ``symfony/line-notify-notifier `` ``linenotify://TOKEN@default ``
200
- LinkedIn ``symfony/linked-in-notifier `` ``linkedin://TOKEN:USER_ID@default ``
201
- Mastodon ``symfony/mastodon-notifier `` ``mastodon://ACCESS_TOKEN@HOST ``
202
- Mattermost ``symfony/mattermost-notifier `` ``mattermost://ACCESS_TOKEN@HOST/PATH?channel=CHANNEL ``
203
- Mercure ``symfony/mercure-notifier `` ``mercure://HUB_ID?topic=TOPIC ``
204
- MicrosoftTeams ``symfony/microsoft-teams-notifier `` ``microsoftteams://default/PATH ``
205
- RocketChat ``symfony/rocket-chat-notifier `` ``rocketchat://TOKEN@ENDPOINT?channel=CHANNEL ``
206
- Slack ``symfony/slack-notifier `` ``slack://TOKEN@default?channel=CHANNEL ``
207
- Telegram ``symfony/telegram-notifier `` ``telegram://TOKEN@default?channel=CHAT_ID ``
208
- Twitter ``symfony/twitter-notifier `` ``twitter://API_KEY:API_SECRET:ACCESS_TOKEN:ACCESS_SECRET@default ``
209
- Zendesk ``symfony/zendesk-notifier `` ``zendesk://EMAIL:TOKEN@SUBDOMAIN ``
210
- Zulip ``symfony/zulip-notifier `` ``zulip://EMAIL:TOKEN@HOST?channel=CHANNEL ``
211
- ============== ==================================== == =============================================================================
227
+ ====================================== ==================================== =============================================================================
228
+ Service Package DSN
229
+ ====================================== ==================================== =============================================================================
230
+ AmazonSns ``symfony/amazon-sns-notifier `` ``sns://ACCESS_KEY:SECRET_KEY@default?region=REGION ``
231
+ Chatwork ``symfony/chatwork-notifier `` ``chatwork://API_TOKEN@default?room_id=ID ``
232
+ :doc: ` Discord < notifier/discord >` ``symfony/discord-notifier `` ``discord://TOKEN@default?webhook_id=ID ``
233
+ FakeChat ``symfony/fake-chat-notifier `` ``fakechat+email://default?to=TO&from=FROM `` or ``fakechat+logger://default ``
234
+ Firebase ``symfony/firebase-notifier `` ``firebase://USERNAME:PASSWORD@default ``
235
+ Gitter ``symfony/gitter-notifier `` ``gitter://TOKEN@default?room_id=ROOM_ID ``
236
+ GoogleChat ``symfony/google-chat-notifier `` ``googlechat://ACCESS_KEY:ACCESS_TOKEN@default/SPACE?thread_key=THREAD_KEY ``
237
+ LINE Notify ``symfony/line-notify-notifier `` ``linenotify://TOKEN@default ``
238
+ LinkedIn ``symfony/linked-in-notifier `` ``linkedin://TOKEN:USER_ID@default ``
239
+ Mastodon ``symfony/mastodon-notifier `` ``mastodon://ACCESS_TOKEN@HOST ``
240
+ Mattermost ``symfony/mattermost-notifier `` ``mattermost://ACCESS_TOKEN@HOST/PATH?channel=CHANNEL ``
241
+ Mercure ``symfony/mercure-notifier `` ``mercure://HUB_ID?topic=TOPIC ``
242
+ :doc: ` MicrosoftTeams < notifier/teams >` ``symfony/microsoft-teams-notifier `` ``microsoftteams://default/PATH ``
243
+ RocketChat ``symfony/rocket-chat-notifier `` ``rocketchat://TOKEN@ENDPOINT?channel=CHANNEL ``
244
+ :doc: ` Slack < notifier/slack >` ``symfony/slack-notifier `` ``slack://TOKEN@default?channel=CHANNEL ``
245
+ :doc: ` Telegram < notifier/telegram >` ``symfony/telegram-notifier `` ``telegram://TOKEN@default?channel=CHAT_ID ``
246
+ Twitter ``symfony/twitter-notifier `` ``twitter://API_KEY:API_SECRET:ACCESS_TOKEN:ACCESS_SECRET@default ``
247
+ Zendesk ``symfony/zendesk-notifier `` ``zendesk://EMAIL:TOKEN@SUBDOMAIN ``
248
+ Zulip ``symfony/zulip-notifier `` ``zulip://EMAIL:TOKEN@HOST?channel=CHANNEL ``
249
+ ====================================== ==================================== =============================================================================
212
250
213
251
.. versionadded :: 6.2
214
252
@@ -267,6 +305,41 @@ Chatters are configured using the ``chatter_transports`` setting:
267
305
;
268
306
};
269
307
308
+ .. _sending-chat-messages :
309
+
310
+ The :class: `Symfony\\ Component\\ Notifier\\ ChatterInterface ` class allows
311
+ you to send messages to chat services::
312
+
313
+ // src/Controller/CheckoutController.php
314
+ namespace App\Controller;
315
+
316
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
317
+ use Symfony\Component\Notifier\ChatterInterface;
318
+ use Symfony\Component\Notifier\Message\ChatMessage;
319
+ use Symfony\Component\Routing\Annotation\Route;
320
+
321
+ class CheckoutController extends AbstractController
322
+ {
323
+ /**
324
+ * @Route("/checkout/thankyou")
325
+ */
326
+ public function thankyou(ChatterInterface $chatter)
327
+ {
328
+ $message = (new ChatMessage('You got a new invoice for 15 EUR.'))
329
+ // if not set explicitly, the message is send to the
330
+ // default transport (the first one configured)
331
+ ->transport('slack');
332
+
333
+ $sentMessage = $chatter->send($message);
334
+
335
+ // ...
336
+ }
337
+ }
338
+
339
+ The ``send() `` method returns a variable of type
340
+ :class: `Symfony\\ Component\\ Notifier\\ Message\\ SentMessage ` which provides
341
+ information such as the message ID and the original message contents.
342
+
270
343
.. _notifier-email-channel :
271
344
272
345
Email Channel
@@ -816,18 +889,87 @@ all configured texter and chatter transports only in the ``dev`` (and/or
816
889
chatter_transports :
817
890
slack : ' null://null'
818
891
892
+ .. _notifier-events :
893
+
894
+ .. index ::
895
+ single: Notifier; Events
896
+
897
+ Using Events
898
+ ------------
899
+
900
+ The :class: `Symfony\\ Component\\ Notifier\\ Transport` ` class of the Notifier component
901
+ allows you to optionally hook into the lifecycle via events.
902
+
903
+ The ``MessageEvent::class `` Event
904
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
905
+
906
+ **Typical Purposes **: Doing something before the message is send (like logging
907
+ which message is going to be send, or displaying something about the event
908
+ to be executed.
909
+
910
+ Just before send the message, the event class ``MessageEvent `` is
911
+ dispatched. Listeners receive a
912
+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ MessageEvent ` event::
913
+
914
+ use Symfony\Component\Notifier\Event\MessageEvent;
915
+
916
+ $dispatcher->addListener(MessageEvent::class, function (MessageEvent $event) {
917
+ // gets the message instance
918
+ $message = $event->getMessage();
919
+
920
+ // log something
921
+ $this->logger(sprintf('Message with subject: %s will be send to %s, $message->getSubject(), $message->getRecipientId()'));
922
+ });
923
+
924
+ The ``FailedMessageEvent `` Event
925
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
926
+
927
+ **Typical Purposes **: Doing something before the exception is thrown
928
+ (Retry to send the message or log additional information).
929
+
930
+ Whenever an exception is thrown while sending the message, the event class
931
+ ``FailedMessageEvent `` is dispatched. A listener can do anything useful before
932
+ the exception is thrown.
933
+
934
+ Listeners receive a
935
+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ FailedMessageEvent ` event::
936
+
937
+ use Symfony\Component\Notifier\Event\FailedMessageEvent;
938
+
939
+ $dispatcher->addListener(FailedMessageEvent::class, function (FailedMessageEvent $event) {
940
+ // gets the message instance
941
+ $message = $event->getMessage();
942
+
943
+ // gets the error instance
944
+ $error = $event->getError();
945
+
946
+ // log something
947
+ $this->logger(sprintf('The message with subject: %s has not been sent successfully. The error is: %s, $message->getSubject(), $error->getMessage()'));
948
+ });
949
+
950
+ The ``SentMessageEvent `` Event
951
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
952
+
953
+ **Typical Purposes **: To perform some action when the message is successfully
954
+ sent (like retrieve the id returned when the message is sent).
955
+
956
+ After the message has been successfully sent, the event class ``SentMessageEvent ``
957
+ is dispatched. Listeners receive a
958
+ :class: `Symfony\\ Component\\ Notifier\\ Event\\ SentMessageEvent ` event::
959
+
960
+ use Symfony\Component\Notifier\Event\SentMessageEvent;
961
+
962
+ $dispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) {
963
+ // gets the message instance
964
+ $message = $event->getOriginalMessage();
965
+
966
+ // log something
967
+ $this->logger(sprintf('The message has been successfully sent and have id: %s, $message->getMessageId()'));
968
+ });
969
+
819
970
.. TODO
820
971
.. - Using the message bus for asynchronous notification
821
972
.. - Describe notifier monolog handler
822
973
.. - Describe notification_on_failed_messages integration
823
974
824
- Learn more
825
- ----------
826
-
827
- .. toctree ::
828
- :maxdepth: 1
829
- :glob:
830
-
831
- notifier/*
832
-
833
975
.. _`RFC 3986` : https://www.ietf.org/rfc/rfc3986.txt
0 commit comments