8000 [Notifier] [RocketChat] Updated RocketChatOptions · symfony/symfony@8eeea16 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8eeea16

Browse files
Léo VINCENTfabpot
Léo VINCENT
authored andcommitted
[Notifier] [RocketChat] Updated RocketChatOptions
1 parent 3eacfda commit 8eeea16

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

src/Symfony/Component/Notifier/Bridge/RocketChat/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Add the ability to send multiple attachments in a message
8+
* Add the ability to send custom payload data to Rocket.Chat webhooks
9+
410
5.3
511
---
612

src/Symfony/Component/Notifier/Bridge/RocketChat/README.md

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,76 @@ ROCKETCHAT_DSN=rocketchat://ACCESS_TOKEN@default?channel=CHANNEL
1111
```
1212

1313
where:
14-
- `ACCESS_TOKEN` is your RocketChat access token
15-
- `CHANNEL` is your RocketChat channel
14+
- `ACCESS_TOKEN` is your RocketChat webhook token
15+
- `CHANNEL` is your RocketChat channel, it may be overridden in the payload
16+
17+
Example (be sure to escape the middle slash with %2F):
18+
19+
```
20+
# Webhook URL: https://rocketchathost/hooks/a847c392165c41f7bc5bbf273dd701f3/9343289d1c33464bb15ef132b5a7628d
21+
ROCKETCHAT_DSN=rocketchat://a847c392165c41f7bc5bbf273dd701f3%2F9343289d1c33464bb15ef132b5a7628d@rocketchathost?channel=channel
22+
```
23+
24+
Attachments and Payload
25+
-----------------------
26+
27+
When creating a `ChatMessage`, you can add payload and multiple attachments to
28+
`RocketChatOptions`. These enable you to customize the name or the avatar of the
29+
bot posting the message, and to add files to it.
30+
31+
The payload can contain any data you want; its data is processed by a
32+
Rocket.Chat Incoming Webhook Script which you can write to best suit your needs.
33+
For example, you can use this script to send the raw payload to Rocket.Chat:
34+
35+
```javascript
36+
class Script {
37+
process_incoming_request({ request }) {
38+
return {
39+
request.content
40+
};
41+
}
42+
}
43+
```
44+
45+
When using this script, the Payload must be indexed following Rocket.Chat
46+
Payload convention:
47+
48+
```php
49+
$payload = [
50+
'alias' => 'Bot Name',
51+
'emoji' => ':joy:', // Emoji used as avatar
52+
'avatar' => 'http://site.com/logo.png', // Overridden by emoji if provided
53+
'channel' => '#myChannel', // Overrides the DSN's channel setting
54+
];
55+
56+
$attachement1 = [
57+
'color' => '#ff0000',
58+
'title' => 'My title',
59+
'text' => 'My text',
60+
// ...
61+
];
62+
63+
$attachement2 = [
64+
'color' => '#ff0000',
65+
'title' => 'My title',
66+
'text' => 'My text',
67+
// ...
68+
];
69+
70+
// For backward compatibility reasons, both usages are valid
71+
$rocketChatOptions = new RocketChatOptions($attachement1, $payload);
72+
$rocketChatOptions = new RocketChatOptions([$attachement1, $attachement2], $payload);
73+
```
74+
75+
**Note:** the `text` and `attachments` keys of the payload will be overridden
76+
respectively by the ChatMessage's subject and the attachments provided in
77+
RocketChatOptions' constructor.
78+
79+
See Also
80+
--------
81+
82+
* [Rocket.Chat Webhook Integration](https://docs.rocket.chat/guides/administration/admin-panel/integrations)
83+
* [Rocket.Chat Message Payload](https://developer.rocket.chat/reference/api/rest-api/endpoints/core-endpoints/chat-endpoints/postmessage#payload)
1684

1785
Resources
1886
---------

src/Symfony/Component/Notifier/Bridge/RocketChat/RocketChatOptions.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,36 @@
1717
* @author Jeroen Spee <https://github.com/Jeroeny>
1818
*
1919
* @see https://rocket.chat/docs/administrator-guides/integrations/
20+
* @see https://developer.rocket.chat/reference/api/rest-api/endpoints/core-endpoints/chat-endpoints/postmessage
2021
*/
2122
final class RocketChatOptions implements MessageOptionsInterface
2223
{
2324
/** prefix with '@' for personal messages */
2425
private ?string $channel = null;
2526

26-
/** @var mixed[] */
27+
/** @var string[]|string[][] */
2728
private array $attachments;
2829

30+
/** @var string[] */
31+
private array $payload;
32+
2933
/**
30-
* @param string[] $attachments
34+
* @param string[]|string[][] $attachments
35+
* @param string[] $payload
3136
*/
32-
public function __construct(array $attachments = [])
37+
public function __construct(array $attachments = [], array $payload = [])
3338
{
3439
$this->attachments = $attachments;
40+
$this->payload = $payload;
3541
}
3642

3743
public function toArray(): array
3844
{
39-
return [
40-
'attachments' => [$this->attachments],
41-
];
45+
if ($this->attachments) {
46+
return $this->payload + ['attachments' => array_is_list($this->attachments) ? $this->attachments : [$this->attachments]];
47+
}
48+
49+
return $this->payload;
4250
}
4351

4452
public function getRecipientId(): ?string

0 commit comments

Comments
 (0)
0