Difference between revisions of "Notifications"

From WHMCS Documentation

 
(17 intermediate revisions by 4 users not shown)
Line 1: Line 1:
==Overview==
+
WHMCS integrates with popular team communication apps, including Slack and HipChat, to allow WHMCS to notify you in real time as events occur.
  
[[File:Notifications Home.png]]
+
You can access this feature at '''Configuration (<i class="fa fa-wrench" aria-hidden="true"></i>) > System Settings > Notifications''' or, prior to WHMCS 8.0, '''Setup > Notifications'''.
  
WHMCS integrates with popular team communication apps including Slack and HipChat to allow you to be notified in real-time as events occur within WHMCS.
+
[[File:Notifications Home.png|900px]]
  
Using conditions, you can setup WHMCS to notify you in your Slack channels and HipChat rooms about the events that are important to you.  Here are some examples.
+
== Example Use ==
  
* Notify my '''Support''' channel/room when a new ticket is opened in the '''Support department''' with a '''High priority'''
+
Using conditions, you can set up WHMCS to notify you in your Slack channels and HipChat rooms about the events that are important to you. For example:
* Notify my '''Enterprise Support''' channel/room when a '''new ticket''' is opened or replied to by a customer belonging to the '''Enterprise Customers client group'''
+
 
* Notify my '''Emergency''' channel/room when a new ticket is opened '''containing the words''' "Emergency" in the subject
+
* Notify my '''Support''' channel or room when a new ticket opens in the '''Support department''' with a '''High priority'''.
* Notify my '''Sales''' channel/room when a '''new order''' is placed with a '''value over $25'''
+
* Notify my '''Enterprise Support''' channel or room when a '''new ticket''' opens or a customer in the '''Enterprise Customers client group''' replies.
* Notify my  '''Billing''' channel/room when '''an invoice is paid''' by a customer belonging to the '''High Value client group'''
+
* Notify my '''Emergency''' channel or room when a new ticket opens '''containing the words''' "Emergency" in the subject.
* Notify my '''Customer Service''' channel/room when an '''order is placed''' for the product '''Business Hosting'''
+
* Notify my '''Sales''' channel or room when someone places a '''new order''' with a '''value over $25'''.
 +
* Notify my  '''Billing''' channel or room when '''an invoice is paid''' by a customer belonging to the '''High Value client group'''.
 +
* Notify my '''Customer Service''' channel or room when an '''order is placed''' for the product '''Business Hosting'''.
  
 
==Configuring Notification Providers==
 
==Configuring Notification Providers==
  
At the time of writing, WHMCS supports the following notification providers out of the box. Select one to learn more and for information on how to configure it.
+
WHMCS supports the following notification providers:
  
 
* [[Configuring Notifications with HipChat]]
 
* [[Configuring Notifications with HipChat]]
 
* [[Configuring Notifications with Slack]]
 
* [[Configuring Notifications with Slack]]
 +
* [[Configuring Notifications via Email]]
  
 
<div class="docs-alert-info">You can add additional notification providers by creating a custom notification module.  You can learn more about that here: https://developers.whmcs.com/notification-providers/</div>
 
<div class="docs-alert-info">You can add additional notification providers by creating a custom notification module.  You can learn more about that here: https://developers.whmcs.com/notification-providers/</div>
Line 25: Line 28:
 
==Creating a Notification Rule==
 
==Creating a Notification Rule==
  
Notifications are based around rules.
+
Notifications use rules. Each rule defines when to trigger a notification through a combination of events and conditions.
 
 
Each rule defines when a notification should be triggered through a combination of events and conditions.
 
  
To create a new rule, follow the steps below.
+
To create a new rule, follow the steps below:
  
 
1. Click the '''Create New Notification Rule''' button.<br>
 
1. Click the '''Create New Notification Rule''' button.<br>
2. Enter a name/description for your rule that will allow you to easily identify it again later.<br>
+
2. Enter a name and description for your rule, to allow you to easily identify it again later.<br>
 
3. Choose an Event Category from the available selector buttons.
 
3. Choose an Event Category from the available selector buttons.
  
 
[[File:Notifications Events Selector.png]]
 
[[File:Notifications Events Selector.png]]
  
4. Choose one or more events that should trigger the notification within the selected event category. Use Ctrl+click to select multiple ones.<br>
+
4. Choose one or more events that should trigger the notification within the selected event category. Use Ctrl+Click to select multiple ones.<br>
5. Configure any conditions you want to use to restrict when the notification is triggered. The conditions available to you will vary based on the selected event category.
+
5. Configure any conditions you want to use to restrict when the system triggers the notification. The conditions available to you will vary depending on the selected event category.
  
 
[[File:Notifications Conditions.png]]
 
[[File:Notifications Conditions.png]]
  
By default all conditions default to unrestricted. To apply a condition to a rule, simply select or provide a value for a given condition. In the case of a free text based condition such as ''Subject'', you are given the option of choosing from an Exact Match or Containing Match. In a monetary value condition such as ''Order Total'', you have the option of specifying a Greater Than or Less Than match.
+
By default, all conditions default to unrestricted. To apply a condition to a rule, select or provide a value for a given condition. In the case of a free text-based condition such as ''Subject'', you can choose from an Exact Match or a Containing Match. In a monetary value condition such as ''Order Total'', you have the option of specifying a Greater Than or Less Than match.
  
 
6. Configure the desired Notification Settings:
 
6. Configure the desired Notification Settings:
Line 49: Line 50:
  
 
i. Begin by choosing the desired notification provider for this rule.  Here we have chosen HipChat.<br>
 
i. Begin by choosing the desired notification provider for this rule.  Here we have chosen HipChat.<br>
ii. The other options here will depend upon the notification provider selected, but in the case of both HipChat and Slack, you will be asked to choose a Room/Channel for the notification to be posted to and be given the ability to override the default message with a custom one of your choosing.<br>
+
ii. The other options here will depend upon the notification provider that you selected, but in the case of both HipChat and Slack, you must choose a Room or Channel to post the notification. You will also be able to override the default message with a custom one of your choosing.<br>
iii. In the case of HipChat, you also have the ability to select whether the notification should trigger an alert for the user. When selected, this can include playing a sound, a mobile notification, and more based on an individual users notification settings.
+
iii. In the case of HipChat, you also have the ability to select whether the notification should trigger an alert for the user. When you select this, this can include playing a sound, a mobile notification, and more, depending on an individual user's notification settings.
  
 
==Modifying a Notification Rule==
 
==Modifying a Notification Rule==
  
[[File:Notifications Manage.png]]
+
To edit a notification, locate the notification rule you wish to edit and click the pencil icon. This will open the rule configuration dialog box, where you can make your desired changes.
 +
 
 +
For more information on the configuration options that are available, see the Creating a Notification Rule guide.
 +
 
 +
==Duplicating a Notification Rule==
 +
 
 +
To duplicate a notification, locate the notification rule you wish to copy and click the copy icon (two files). This will open the rule configuration dialog where you can customise the rule and then click the Duplicate button to complete the process.
 +
 
 +
For more information on the configuration options that are available, see the Creating a Notification Rule guide.
 +
 
 +
==Enabling/Disabling a Notification Rule==
 +
 
 +
An on-or-off slider in the Notification Rules table indicates the current status of each notification rule. To toggle the status of a notification, click the slide toggle for the desired notification rule.
 +
 
 +
==Deleting a Notification Rule==
 +
 
 +
To delete a notification, locate the notification rule you wish to edit and click the trash can icon.
 +
 
 +
The system will ask you to confirm that you want to delete the rule before removing it.
  
To edit a notification, locate the notification rule you wish to edit and click the pencil icon.
+
The system can't restore a rule after it deletes it. If you may want to reuse the rule in future, we recommend disabling the rule instead of deleting it.
  
This will open the rule configuration dialog where you can make your desired changes.
+
==Configuring Email Notifications==
  
For more information on the configuration options that are available, please refer to the Creating a Notification Rule guide.
+
Email notifications you to receive notifications to your email address when events occur within WHMCS.
  
==Duplicating a Notification Rule==
+
To configure this:
 +
 
 +
# Go to '''Configuration (<i class="fa fa-wrench" aria-hidden="true"></i>) > System Settings > [[Notifications]]''' or, prior to WHMCS 8.0, '''Setup > Notifications'''.
 +
# Click '''Configure''' for '''Email'''.<br/>[[File:Email-notification-configuration.JPG]]
 +
# Configure the Email module settings:
 +
#* '''Sender Name''' — The name you wish for emails from this module to be sent as, typically something unique that stands out to prevent these notifications from being missed.
 +
#* '''Sender Email''' — The email address the notification should come from.
 +
# Click '''Save Changes'''.
 +
 
 +
<div class="docs-alert-info">
 +
This module is not encoded. You can use it as a sample in developing custom notification provider modules or extend it further.
 +
</div>
 +
 
 +
==Hooks==
 +
 
 +
Using hooks, it is possible to implement more complex conditional logic than is possible with the conditions available via the Admin Area.
 +
 
 +
WHMCS includes a "[https://developers.whmcs.com/hooks-reference/everything-else/#notificationpresend NotificationPreSend]" hook point, which executes prior to the system sending a notification. This allows you to apply additional conditional criteria and manipulate the notification message. You can also use this hook point to abort the sending of a Notification that uses your logic.
 +
 
 +
<source lang="php">
 +
 
 +
<?php
 +
 
 +
add_hook('NotificationPreSend', 1, function($vars) {
 +
 
 +
    $eventType = $vars['eventType']; // e.g. "Ticket", "Invoice", "Order", "Service" or "Domain"
 +
    $eventName = $vars['eventName'];
 +
    $rule = $vars['rule'];
 +
    $hookParameters = $vars['hookParameters'];
 +
    $notification = $vars['notification'];
 +
 
 +
    // Perform additional conditional logic and throw the AbortNotification
 +
    // exception to prevent the notification from sending.
 +
    if ($eventType == 'Invoice'
 +
        && $eventName == 'created'
 +
        && (isset($hookParameters['invoiceid'])
 +
        && $hookParameters['invoiceid'] > 1000)
 +
    ) {
 +
        throw new \WHMCS\Notification\Exception\AbortNotification();
 +
    }
  
[[File:Notifications Manage.png]]
+
    // If allowing the notification to continue, you can manipulate the
 +
    // notification using the \WHMCS\Notification\Notification object.
 +
    $notification->setTitle('Override notification title');
 +
    $notification->setMessage('Override notification message body');
  
To duplicate a notification, locate the notification rule you wish to copy and click the copy icon (indicated by two files).
+
});
  
This will open the rule configuration dialog where you can customise the rule and then click the Duplicate button to complete the process.
+
</source>
  
For more information on the configuration options that are available, please refer to the Creating a Notification Rule guide.
+
By implementing the above hook, any notification for an invoice with an ID above 1000 will abort. To abort a notification, the system returns a `\WHMCS\Notification\Exception\AbortNotification();` exception.
  
==Enabling/Disabling a Notification Rule==
+
If the ID is below 1000, the system will continue to send the notification, but will override the title and message. To override values, use the "set" functions in $notification. For more information, see [https://docs.whmcs.com/classes/7.6/WHMCS/Notification/Contracts/NotificationInterface.html Internal Class Documentation].
  
[[File:Notifications Manage.png]]
+
==API==
  
The current status of each notification rule is indicated within the Notification Rules table by an on/off slider.
+
WHMCS makes a TriggerNotificationEvent API function available, which allows you to leverage the Notifications system within WHMCS to send notifications on demand.
  
To toggle the status of a notification, simply click the slide toggle for the desired notification rule.
+
Below is an example of using this API method to trigger a notification whenever an admin user logs in to the Admin Area. You can achieve this using a combination of the "AdminLogin" hook and the TriggerNotificationEvent API call:
  
==Deleting a Notification Rule==
+
<source lang="php">
  
[[File:Notifications Manage.png]]
+
<?php
 +
add_hook('AdminLogin', 1, function($vars) {
  
To delete a notification, locate the notification rule you wish to edit and click the trash can icon.
+
    $command = 'TriggerNotificationEvent';
 +
    $postData = array(
 +
        'notification_identifier' => 'adminarea.staff.login', // A unique identifier used when referencing the notification rule.
 +
        'title' => 'A Staff Member Just Logged In',
 +
        'message' => $vars['username'] . ' just logged in to the WHMCS Admin Area.',
 +
        'url' => 'https://whmcs.example.test/admin/',
 +
        'status' => 'Success',
 +
        'statusStyle' => 'info',
 +
    );
 +
    $adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later
 +
    $results = localAPI($command, $postData, $adminUsername);
 +
    print_r($results);
 +
});
  
You will be asked to confirm you are sure you want to delete the rule before it is removed.
+
</source>
  
Please note that a rule cannot be restored after it has been deleted so if you may want to re-use the rule in future, we recommend disabling the rule instead of deleting it.
+
After you create your API call, return to the WHMCS Admin Area and define a rule for this event.
  
==Hooks==
+
# Click "Create New Notification Rule".
 +
# Type a name for your Notification Rule.
 +
# Select "API" and "Custom API Trigger".
 +
# Under "Trigger Identifier", input the notification identifier you specified in the API call. In our example, this is "adminarea.staff.login".
 +
# Select "Exact Match" in the select element above.
 +
# Finally, select and configure the notification method. For example, in the screenshot below, we are configuring an Email notification to login-notifications@example.net.
  
Using hooks, it is possible to implement more complex conditional logic than is possible with the conditions available via the UI.
+
[[File:NotificationsAPISetup.png]]
  
Documentation coming soon.
+
After you set this up, whenever you trigger this API call, the system will deliver a custom notification via your chosen Notification provider.
  
 
==Adding a Custom Notification Provider==
 
==Adding a Custom Notification Provider==
  
 
You can add additional notification providers that notification rules can use by creating a custom notification module.  You can learn more about that here: https://developers.whmcs.com/notification-providers/
 
You can add additional notification providers that notification rules can use by creating a custom notification module.  You can learn more about that here: https://developers.whmcs.com/notification-providers/

Latest revision as of 18:30, 16 June 2022

WHMCS integrates with popular team communication apps, including Slack and HipChat, to allow WHMCS to notify you in real time as events occur.

You can access this feature at Configuration () > System Settings > Notifications or, prior to WHMCS 8.0, Setup > Notifications.

Notifications Home.png

Example Use

Using conditions, you can set up WHMCS to notify you in your Slack channels and HipChat rooms about the events that are important to you. For example:

  • Notify my Support channel or room when a new ticket opens in the Support department with a High priority.
  • Notify my Enterprise Support channel or room when a new ticket opens or a customer in the Enterprise Customers client group replies.
  • Notify my Emergency channel or room when a new ticket opens containing the words "Emergency" in the subject.
  • Notify my Sales channel or room when someone places a new order with a value over $25.
  • Notify my Billing channel or room when an invoice is paid by a customer belonging to the High Value client group.
  • Notify my Customer Service channel or room when an order is placed for the product Business Hosting.

Configuring Notification Providers

WHMCS supports the following notification providers:

You can add additional notification providers by creating a custom notification module. You can learn more about that here: https://developers.whmcs.com/notification-providers/

Creating a Notification Rule

Notifications use rules. Each rule defines when to trigger a notification through a combination of events and conditions.

To create a new rule, follow the steps below:

1. Click the Create New Notification Rule button.
2. Enter a name and description for your rule, to allow you to easily identify it again later.
3. Choose an Event Category from the available selector buttons.

Notifications Events Selector.png

4. Choose one or more events that should trigger the notification within the selected event category. Use Ctrl+Click to select multiple ones.
5. Configure any conditions you want to use to restrict when the system triggers the notification. The conditions available to you will vary depending on the selected event category.

Notifications Conditions.png

By default, all conditions default to unrestricted. To apply a condition to a rule, select or provide a value for a given condition. In the case of a free text-based condition such as Subject, you can choose from an Exact Match or a Containing Match. In a monetary value condition such as Order Total, you have the option of specifying a Greater Than or Less Than match.

6. Configure the desired Notification Settings:

Notifications Notification Settings.png

i. Begin by choosing the desired notification provider for this rule. Here we have chosen HipChat.
ii. The other options here will depend upon the notification provider that you selected, but in the case of both HipChat and Slack, you must choose a Room or Channel to post the notification. You will also be able to override the default message with a custom one of your choosing.
iii. In the case of HipChat, you also have the ability to select whether the notification should trigger an alert for the user. When you select this, this can include playing a sound, a mobile notification, and more, depending on an individual user's notification settings.

Modifying a Notification Rule

To edit a notification, locate the notification rule you wish to edit and click the pencil icon. This will open the rule configuration dialog box, where you can make your desired changes.

For more information on the configuration options that are available, see the Creating a Notification Rule guide.

Duplicating a Notification Rule

To duplicate a notification, locate the notification rule you wish to copy and click the copy icon (two files). This will open the rule configuration dialog where you can customise the rule and then click the Duplicate button to complete the process.

For more information on the configuration options that are available, see the Creating a Notification Rule guide.

Enabling/Disabling a Notification Rule

An on-or-off slider in the Notification Rules table indicates the current status of each notification rule. To toggle the status of a notification, click the slide toggle for the desired notification rule.

Deleting a Notification Rule

To delete a notification, locate the notification rule you wish to edit and click the trash can icon.

The system will ask you to confirm that you want to delete the rule before removing it.

The system can't restore a rule after it deletes it. If you may want to reuse the rule in future, we recommend disabling the rule instead of deleting it.

Configuring Email Notifications

Email notifications you to receive notifications to your email address when events occur within WHMCS.

To configure this:

  1. Go to Configuration () > System Settings > Notifications or, prior to WHMCS 8.0, Setup > Notifications.
  2. Click Configure for Email.
    Email-notification-configuration.JPG
  3. Configure the Email module settings:
    • Sender Name — The name you wish for emails from this module to be sent as, typically something unique that stands out to prevent these notifications from being missed.
    • Sender Email — The email address the notification should come from.
  4. Click Save Changes.

This module is not encoded. You can use it as a sample in developing custom notification provider modules or extend it further.

Hooks

Using hooks, it is possible to implement more complex conditional logic than is possible with the conditions available via the Admin Area.

WHMCS includes a "NotificationPreSend" hook point, which executes prior to the system sending a notification. This allows you to apply additional conditional criteria and manipulate the notification message. You can also use this hook point to abort the sending of a Notification that uses your logic.

<?php

add_hook('NotificationPreSend', 1, function($vars) {

    $eventType = $vars['eventType']; // e.g. "Ticket", "Invoice", "Order", "Service" or "Domain"
    $eventName = $vars['eventName'];
    $rule = $vars['rule'];
    $hookParameters = $vars['hookParameters'];
    $notification = $vars['notification'];

    // Perform additional conditional logic and throw the AbortNotification
    // exception to prevent the notification from sending.
    if ($eventType == 'Invoice'
        && $eventName == 'created'
        && (isset($hookParameters['invoiceid'])
        && $hookParameters['invoiceid'] > 1000)
    ) {
        throw new \WHMCS\Notification\Exception\AbortNotification();
    }

    // If allowing the notification to continue, you can manipulate the
    // notification using the \WHMCS\Notification\Notification object.
    $notification->setTitle('Override notification title');
    $notification->setMessage('Override notification message body');

});

By implementing the above hook, any notification for an invoice with an ID above 1000 will abort. To abort a notification, the system returns a `\WHMCS\Notification\Exception\AbortNotification();` exception.

If the ID is below 1000, the system will continue to send the notification, but will override the title and message. To override values, use the "set" functions in $notification. For more information, see Internal Class Documentation.

API

WHMCS makes a TriggerNotificationEvent API function available, which allows you to leverage the Notifications system within WHMCS to send notifications on demand.

Below is an example of using this API method to trigger a notification whenever an admin user logs in to the Admin Area. You can achieve this using a combination of the "AdminLogin" hook and the TriggerNotificationEvent API call:

<?php
add_hook('AdminLogin', 1, function($vars) {

    $command = 'TriggerNotificationEvent';
    $postData = array(
        'notification_identifier' => 'adminarea.staff.login', // A unique identifier used when referencing the notification rule.
        'title' => 'A Staff Member Just Logged In',
        'message' => $vars['username'] . ' just logged in to the WHMCS Admin Area.',
        'url' => 'https://whmcs.example.test/admin/',
        'status' => 'Success',
        'statusStyle' => 'info',
    );
    $adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later
    $results = localAPI($command, $postData, $adminUsername);
    print_r($results);
});

After you create your API call, return to the WHMCS Admin Area and define a rule for this event.

  1. Click "Create New Notification Rule".
  2. Type a name for your Notification Rule.
  3. Select "API" and "Custom API Trigger".
  4. Under "Trigger Identifier", input the notification identifier you specified in the API call. In our example, this is "adminarea.staff.login".
  5. Select "Exact Match" in the select element above.
  6. Finally, select and configure the notification method. For example, in the screenshot below, we are configuring an Email notification to login-notifications@example.net.

NotificationsAPISetup.png

After you set this up, whenever you trigger this API call, the system will deliver a custom notification via your chosen Notification provider.

Adding a Custom Notification Provider

You can add additional notification providers that notification rules can use by creating a custom notification module. You can learn more about that here: https://developers.whmcs.com/notification-providers/