Custom Server Notification for Slack Not Working
Hi Kennerth,
Thanks for the reply but it didn't answer to my question.
I folowed this kb article: How To Create Custom Server Notifications - cPanel Knowledge Base - cPanel Documentation to make the custom notification.
I have Slack setup and generated a token.
My Schema Module is working fine and is saving the Slack data needed to send the POST:
Then my Provider code:
The problem is that no message is sent to slack. The email notification is received but nothing in Slack. Also I tail the cpanel error log and there are no errors. I share here my code if someone want to help. Any help would be much appreciated. Thanks, Bruno Morais
# cpanel - Cpanel/iContact/Provider/Schema/Slack.pm
#
# This code was developed by Bruno Morais bruno.morais@smartevolve.com
# The code has no warranty use it as you want.
package Cpanel::iContact::Provider::Schema::Slack;
use strict;
use warnings;
use Cpanel::LoadModule;
sub get_settings {
return {
'SLACKTOKEN' => {
'shadow' => 1, # Flag for saving to /etc/wwwacct.conf.shadow or /etc/wwwacct.conf
'type' => 'text', # This maps to an HTML input tag's "type" attribute.
'checkval' => sub {
Cpanel::LoadModule::load_perl_module('Cpanel::StringFunc::Trim');
# Not really much to do here, since I'm not actually aware of a good validation regex for these tokens
my $value = shift();
return Cpanel::StringFunc::Trim::ws_trim($value);
},
'label' => 'Slack Token',
'help' => 'The Slack Token to use in slack integration. For more information about how to obtain this token, read Slacks Create and regenerate API tokens documentation.',
},
'SLACKCHANNEL' => {
'shadow' => 1,
'type' => 'text',
'checkval' => sub {
Cpanel::LoadModule::load_perl_module('Cpanel::StringFunc::Trim');
my $value = shift();
$value = Cpanel::StringFunc::Trim::ws_trim($value);
# Ideally we would have used Data::Validate::URI, however loading it
# was 30-40% of the execution time of the Basic Setup page so a basic
# regex was used instead.
return $value;
},
'label' => 'Slack Channel',
'help' => sprintf( 'Your Slack channel to send notifications'),
}
};
}
sub get_config {
return {
'default_level' => 'All', # Maps to what you'd see in the WHM >> Contact Manager UI as the default notification level. In /var/cpanel/clevels.conf, this would be translated to '3'.
'display_name' => 'Slack', # Maps to what your provider shows up as in the WHM >> Contact Manager UI.
'icon_name' => 'Slack', # Maps to what your icon is named. Currently irrelevant, as it does nothing to help your icon load, and falls back to 'display_name' above if not set.
'icon' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4gEGDCoaFECqcwAAAwZJREFUOMs9k0toXGUYhp/vP/85M9PcmnbSGadpMLVQLygJooKoVCpeqSB046aIim5chiKCLrpQwaUEXbgTY1Rwb6GCorSaFlvTamPTtEkznUzbZCZzSWbOOf//uUh08W4feG9y78fvHn312TfeC41x1qChGEJjsEawIgCkSZPNxS/o3vqR2FtS5/DOS5C4SdvcbLx/q155aCQ/guARBESQbQGo7SPoPUjmzilyRilECaNhTEniD613qbtSnmO0MIpLPV4U5z2qghqhJ7QMZiP6CwfJtwcppUuEqjivxF1Nrfee8u0lNpMuguA95DOWYi7DXb1ZCrks+Sgkmz5Iq3GAzs15Yg3Bg6pifepothrUajd5av997M2G7MlFDPflaK2vo90NsrkBNNiF9j2AcgpUwQMKtlAYZKiQZ99AynOlPAGO0Aac+OgTzvx2jiRNObD/bo5PTFDaNU432oO2K6CAKnZktEiUiViN63TdJv2ZHL+e+Z1vv/uet998nfHxMS7PzTG4sw+NxpDsXmguo96ABwMep0q1XWe+XsUDw/uGCW3A1PQ0M2dneObwIXb05tDMAKb/fnQ7A+8Vo+rx3lHtrPNPbQVNlGKxyDfTX/LSiy8w9fU0R15+hT8vzGIFgqEnkaAP9Yp6j9mqzNFJulxZX6GebFCtVJn6aopHH3mY4xMTrK6usbBwDQFk6HHI7EbV41se67xDvEFEWGisUG7XsK0NFpdu8MPJEwAce+0YTxw+RFfBtQxxuUTnjznaVz3W6X8Aw3Jrlau1Cs/fM87kZ5OUq1WcEUrRDpKZv6mdvURycZ7OhSrr1x0JDuu9w3lBxJCo51KzwmPBGL2tLoOz14h/Psed85dJl1dwldv4OEZsABGg2xYQSxAZTMdx+uRPPP35aQqz13FrDbTRQuMYNQICGgUABL6HWOtY55zEf5Vp/TJP+3yZZnOTi9ndDAQ5tq4EBLA1PVAUgJAeNlgTKQyU3tFG/IEEJhUjdIEjYS9vZXcSyf8IUEUAxaMogpfFZOnTfwFJOoth8j5XJgAAAABJRU5ErkJggg==', # A long Data URI (ex. "data:image/png;base64,$my_image_base64"). Thankfully these only display at 16x16px, so you can scale down the image first.
};
}
1;
Then my Provider code:
# cpanel - Cpanel/iContact/Provider/Schema/Slack.pm
#
# This code was developed by Bruno Morais bruno.morais@smartevolve.com
# The code has no warranty use it as you want.
package Cpanel::iContact::Provider::Slack;
use strict;
use warnings;
use parent 'Cpanel::iContact::Provider';
use Try::Tiny;
use Cpanel::Hostname ();
use Cpanel::LoadModule ();
use Cpanel::Exception ();
use Cpanel::Rand::Get ();
use Cpanel::Validate::EmailRFC ();
use Cpanel::Autodie::More::Lite ();
our $POSTURL_CLASS = 'Cpanel::Posturl';
sub send {
my ($self) = @_;
my $args_hr = $self->{'args'};
my $contact_hr = $self->{'contact'};
my @errs;
Cpanel::LoadModule::load_perl_module($POSTURL_CLASS);
my $token = $contact_hr->{'SLACKTOKEN'};
my $channel = $contact_hr->{'SLACKCHANNEL'};
my $message = $args_hr->{'text_body'};
my $username = Cpanel::Hostname::gethostname();
my $url = 'https://slack.com/api/chat.postMessage';
my $obj = $POSTURL_CLASS->new();
try {
my $response = $obj->post(
$url,
{
token => $token,
channel => $channel,
text => $message,
username => $username,
as_user => 'false',
}
);
if ( !$response->{success} ) {
die Cpanel::Exception::create( 'ConnectionFailed', 'The system could not send data via [asis,HTTP POST] to the [asis,URL] "[_1]" due to an error: [_2]', [ $url, $response->{content} || $response->{reason} ] );
}
}
catch {
push @errs, $_;
};
if (@errs) {
die Cpanel::Exception::create( 'Collection', [ exceptions => \@errs ] );
}
return 1;
}
1;
The problem is that no message is sent to slack. The email notification is received but nothing in Slack. Also I tail the cpanel error log and there are no errors. I share here my code if someone want to help. Any help would be much appreciated. Thanks, Bruno Morais
-
Hi, At a guess, you need to redo how your post handler works. Slack appears to require an Authorization header with the token (based upon Using OAuth 2.0). I also suggest liberal use of print statements, and Data::Dumper statements, during your development and testing stages. Remove both prior to production use though. I often print simple phrases like sub send{ print print "Entered Cpanel::iContact::Provider::send function\n"; ...
Those simple statements, used frequently in your code, can help pinpoint where the errors and failures are occurring. Data::Dumper can be used to output data structures. Both will help you see what is going on.0 -
Hi Kenneth, I'm using a Legalacy Token and not OAuth2. For instance ot send a message to channel I can make a curl call in console: curl -X POST https://slack.com/api/chat.postMessage --data "token=xoxp-xxxxxxxxxx-xxxxxxxxxxx-xxxxxxxxxxx-xxxxxxxxxxxxx&channel=#cpanel&text=Test&as_user=false&username=cpanel01.smartevolve.com&pretty=1"
This curl request is working. This is regular POST with some query string data. I cant find the Cpanel::Posturl class reference to read about it. Just tried to figure out with other notification modules. Comparing with another modules, I believe it is correct. Do you see any error in my POST call? Thanks for the help. Regards, Bruno Morais0 -
Hi Bruno, You may find the following example helpful if you are able to utilize an incoming WebHook URL: Let us know if the example and instructions help. Additionally, I encourage you to vote and add feedback to the following feature request if you'd like to see support for Slack notifications integrated directly in the product: slack notifications Thank you. 0 -
Hi Michael, That helped a lot. A different approach with webhook in Slack side but working good. Thanks for the help. Regards, Bruno Morais 0 -
Hi Bruno, I'm glad to see that helped. I've marked this thread as solved. Thanks! 0
Please sign in to leave a comment.
Comments
5 comments