mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
transport - enforcing Webex max message length. (#15789)
* Add support for Webex max message length. Webex limits messages to just over 7 KB. Some alerts might be longer than this. So truncate the message with ellipses. While here switch to using the new API URL. * Use spaces. Formatting got messed up on the transfer from test to dev. * Do the truncation after the message is massaged. This may yield a few more allowed characters. --------- Co-authored-by: Joe Clarke <jclarke@cisco.com>
This commit is contained in:
@@ -19,23 +19,33 @@ use LibreNMS\Util\Http;
|
|||||||
class Ciscospark extends Transport
|
class Ciscospark extends Transport
|
||||||
{
|
{
|
||||||
protected string $name = 'Cisco Webex Teams';
|
protected string $name = 'Cisco Webex Teams';
|
||||||
|
// This is the total length minus 4 bytes for ellipses.
|
||||||
|
private static int $MAX_MSG_SIZE = 7435;
|
||||||
|
|
||||||
public function deliverAlert(array $alert_data): bool
|
public function deliverAlert(array $alert_data): bool
|
||||||
{
|
{
|
||||||
$room_id = $this->config['room-id'];
|
$room_id = $this->config['room-id'];
|
||||||
$token = $this->config['api-token'];
|
$token = $this->config['api-token'];
|
||||||
$url = 'https://api.ciscospark.com/v1/messages';
|
$url = 'https://webexapis.com/v1/messages';
|
||||||
$data = [
|
$data = [
|
||||||
'roomId' => $room_id,
|
'roomId' => $room_id,
|
||||||
];
|
];
|
||||||
|
|
||||||
if ($this->config['use-markdown'] === 'on') {
|
if ($this->config['use-markdown'] === 'on') {
|
||||||
// Remove blank lines as they create weird markdown behaviors.
|
// Remove blank lines as they create weird markdown behaviors.
|
||||||
$data['markdown'] = preg_replace('/^\s+/m', '', $alert_data['msg']);
|
$msg = preg_replace('/^\s+/m', '', $alert_data['msg']);
|
||||||
|
$mtype = 'markdown';
|
||||||
} else {
|
} else {
|
||||||
$data['text'] = strip_tags($alert_data['msg']);
|
$msg = strip_tags($alert_data['msg']);
|
||||||
|
$mtype = 'text';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (strlen($msg) > Ciscospark::$MAX_MSG_SIZE) {
|
||||||
|
$msg = substr($msg, 0, Ciscospark::$MAX_MSG_SIZE) . '...';
|
||||||
|
}
|
||||||
|
|
||||||
|
$data[$mtype] = $msg;
|
||||||
|
|
||||||
$res = Http::client()
|
$res = Http::client()
|
||||||
->withToken($token)
|
->withToken($token)
|
||||||
->post($url, $data);
|
->post($url, $data);
|
||||||
|
Reference in New Issue
Block a user