-
Notifications
You must be signed in to change notification settings - Fork 3
/
RocketChat.php
82 lines (74 loc) · 2.51 KB
/
RocketChat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace WHMCS\Module\Notification\RocketChat;
require_once(dirname(__FILE__).'/vendor/autoload.php');
use GuzzleHttp\Client as Guzzle;
use WHMCS\Config\Setting;
use WHMCS\Exception;
use WHMCS\Module\Notification\DescriptionTrait;
use WHMCS\Module\Contracts\NotificationModuleInterface;
use WHMCS\Notification\Contracts\NotificationInterface;
class RocketChat implements NotificationModuleInterface
{
use DescriptionTrait;
public function __construct()
{
$this->setDisplayName('RocketChat')
->setLogoFileName('logo.svg');
}
public function settings()
{
return [
'baseURL' => [
'FriendlyName' => 'RocketChat Base URL',
'Type' => 'text',
'Description' => 'The base URL for your RocketChat instance (ie: https://chat.example.com)',
'Placeholder' => "",
],
];
}
public function testConnection($settings)
{
return true;
}
public function notificationSettings()
{
return [
'notificationToken' => [
'FriendlyName' => 'Incoming WebHook Tokens',
'Type' => 'text',
'Description' => 'Choose the notification webhook tokens (comma delimit for more than one)',
'Required' => true,
],
];
}
public function getDynamicField($fieldName, $settings)
{
return [];
}
public function sendNotification(NotificationInterface $notification, $moduleSettings, $notificationSettings)
{
$to = explode(',', $notificationSettings['notificationToken']);
$to = array_filter(array_unique($to));
if (!$to) {
throw new Exception('No Notification tokens Found');
}
$postData=[
'text' => sprintf("[%s](%s) \n %s", $notification->getTitle(), $notification->getUrl(), $notification->getMessage()),
];
foreach ($notification->getAttributes() as $attribute) {
$postData['attachments'][] = [
'title' => $attribute->getLabel(),
'text' => $attribute->getValue(),
'title_link' => $attribute->getUrl(),
];
}
foreach ($to as $k => $notificationToken) {
$notificationURL=sprintf("%s/hooks/%s", $moduleSettings['baseURL'], $notificationToken);
$client = new Guzzle();
$response = $client->request('POST', $notificationURL, ['json' => $postData]);
if (array_key_exists('error', $response)) {
throw new Exception($response['error']);
}
}
}
}