Skip to content

Commit

Permalink
Add Slack Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainweb committed Jul 13, 2015
1 parent 39fd4db commit c1c3e54
Show file tree
Hide file tree
Showing 10 changed files with 697 additions and 0 deletions.
339 changes: 339 additions & 0 deletions docroot/sites/all/modules/contrib/slack/LICENSE.txt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @file
* Slack integration module admin functions.
*/

/**
* Slack module admin form.
*/
function slack_configure_form($form, &$form_state) {
$form['slack_webhook_url'] = array(
'#type' => 'textfield',
'#title' => t('Webhook URL'),
'#description' => t('Enter your Webhook URL from an Incoming WebHooks integration. It looks like https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ'),
'#default_value' => slack_get_default_webhook_url(),
'#required' => TRUE,
);
$form['slack_channel'] = array(
'#type' => 'textfield',
'#title' => t('Default channel'),
'#description' => t('Enter your channel name with # symbol, for example #general (or @username for a private message or a private group name).'),
'#default_value' => variable_get('slack_channel'),
);
$form['slack_username'] = array(
'#type' => 'textfield',
'#title' => t('Default username'),
'#description' => t('What would you like to name your Slack bot?'),
'#default_value' => variable_get('slack_username'),
);
return system_settings_form($form);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @file
* Slack module page functions.
*/

/**
* Slack test message form.
*/
function slack_send_test_message_form($form, &$form_state) {
$form['slack_test_channel'] = array(
'#type' => 'textfield',
'#title' => t('Channel'),
'#default_value' => variable_get('slack_channel'),
);
$form['slack_test_username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('slack_username'),
);
$form['slack_test_message'] = array(
'#type' => 'textarea',
'#title' => t('Message'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send message'),
);
return $form;
}

/**
* Submit handler for slack test message form.
*/
function slack_send_test_message_form_submit($form, &$form_state) {
$channel = $form_state['values']['slack_test_channel'];
$username = $form_state['values']['slack_test_username'];
$message = $form_state['values']['slack_test_message'];
$result = slack_send_message($message, $channel, $username);
if (!$result) {
drupal_set_message(t("Message wasn't sent. Please, check slack module configuration."));
}
elseif (!isset($result->error) && $result->code == SLACK_CODE_OK) {
drupal_set_message(t('Message was successfully sent.'));
}
else {
drupal_set_message(t("Message wasn't sent."), 'error');
}
}
112 changes: 112 additions & 0 deletions docroot/sites/all/modules/contrib/slack/includes/slack.api.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/**
* @file
* Slack integration module API functions.
*/

/**
* Send message to the Slack.
*
* @param string $message
* The message sent to the channel
* @param string $channel
* The channel in the Slack service to send messages
* @param string $username
* The bot name displayed in the channel
*
* @return bool|object
* Slack response.
*/
function slack_send_message($message, $channel = '', $username = '') {
$webhook_url = slack_get_default_webhook_url();
if (!$webhook_url) {
return FALSE;
}
$message_options = array();
if ($channel) {
$message_options['channel'] = $channel;
}
else {
$message_options['channel'] = slack_get_default_channel();
}
if ($username) {
$message_options['username'] = $username;
}
else {
$message_options['username'] = slack_get_default_username();
}
$result = _slack_send_message($webhook_url, $message, $message_options);
return $result;
}

/**
* Send message to the Slack with more options.
*
* @param string $team_name
* Your team name in the Slack
* @param string $team_token
* The token from "Incoming WebHooks" integration in the Slack
* @param string $message
* The message sent to the channel
* @param array $message_options
* An associative array, it can contain:
* - channel: The channel in the Slack service to send messages
* - username: The bot name displayed in the channel
*
* @return object
* Can contain:
* success fail fail
* - data: ok No hooks Invalid channel specified
* - status message: OK Not found Server Error
* - code: 200 404 500
* - error: - Not found Server Error
*/
function _slack_send_message($webhook_url, $message, $message_options = array()) {
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded',
);
$message_options['text'] = $message;
$sending_data = 'payload=' . drupal_json_encode($message_options);
$options = array(
'method' => 'POST',
'data' => $sending_data,
'headers' => $headers,
);
$sending_url = $webhook_url;
$result = drupal_http_request($sending_url, $options);
return $result;
}

/**
* Get default Webhook URL.
*
* @return string
* Get default Webhook URL.
*/
function slack_get_default_webhook_url() {
$channel = variable_get('slack_webhook_url');
return $channel;
}

/**
* Get default team channel.
*
* @return string
* Get default team channel
*/
function slack_get_default_channel() {
$channel = variable_get('slack_channel');
return $channel;
}

/**
* Get default Slack bot username.
*
* @return string
* Get default Slack bot username
*/
function slack_get_default_username() {
$username = variable_get('slack_username');
return $username;
}
11 changes: 11 additions & 0 deletions docroot/sites/all/modules/contrib/slack/slack.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = Slack
description = Integration with Slack.com service
core = 7.x

configure = admin/config/services/slack
; Information added by Drupal.org packaging script on 2014-12-15
version = "7.x-1.4"
core = "7.x"
project = "slack"
datestamp = "1418630881"

42 changes: 42 additions & 0 deletions docroot/sites/all/modules/contrib/slack/slack.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

define('SLACK_CODE_OK', 200);
define('SLACK_CODE_NOT_FOUND', 404);
define('SLACK_CODE_SERVER_ERROR', 500);

require_once 'includes/slack.api.inc';

/**
* Implements hook_menu().
*/
function slack_menu() {
$items = array();
$slack_module_url = 'admin/config/services/slack';
$items[$slack_module_url] = array(
'title' => 'Slack',
'description' => 'Configure slack module.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items[$slack_module_url . '/config'] = array(
'title' => 'Configuration',
'description' => 'Adjust slack settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('slack_configure_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/pages/slack.admin.inc',
);
$items[$slack_module_url . '/test'] = array(
'title' => 'Send a message',
'description' => 'Allows to send a test message to the Slack.',
'page callback' => 'drupal_get_form',
'page arguments' => array('slack_send_test_message_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/pages/slack.pages.inc',
);
return $items;
}
50 changes: 50 additions & 0 deletions docroot/sites/all/modules/contrib/slack/slack.rules.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* @file
* Slack integration module rules functions.
*/

/**
* Implements hook_rules_action_info().
*/
function slack_rules_action_info() {
$actions = array();
$actions['slack_send_message'] = array(
'base' => 'slack_rules_send_message_action',
'label' => t('Slack send message'),
'group' => t('Slack'),
'parameter' => array(
'message' => array(
'type' => 'text',
'label' => t('Sending message'),
),
'channel' => array(
'type' => 'text',
'label' => t('Channel'),
'description' => t("It will be using a channel from slack module settings, if you don't enter channel here."),
'optional' => TRUE,
),
'username' => array(
'type' => 'text',
'label' => t('Username'),
'description' => t("It will be using a username from slack module settings, if you don't enter username here."),
'optional' => TRUE,
),
),
);
return $actions;
}

/**
* Rules action for sending a message to the Slack.
*/
function slack_rules_send_message_action($message, $channel, $username) {
if (!$channel) {
$channel = slack_get_default_channel();
}
if (!$username) {
$username = slack_get_default_username();
}
slack_send_message($message, $channel, $username);
}
12 changes: 12 additions & 0 deletions docroot/sites/all/modules/features/dcb_rules/dcb_rules.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name = DCB Rules
description = Misc Rules configuration for DCB
core = 7.x
package = DCB Features
dependencies[] = entity
dependencies[] = rules
dependencies[] = slack
dependencies[] = ticket
features[features_api][] = api:2
features[rules_config][] = rules_registration_for_event_while_signup
features[rules_config][] = rules_send_slack_notification_on_session_submission
project path = sites/all/modules/features
5 changes: 5 additions & 0 deletions docroot/sites/all/modules/features/dcb_rules/dcb_rules.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
/**
* @file
* Drupal needs this blank file.
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* @file
* dcb_rules.rules_defaults.inc
*/

/**
* Implements hook_default_rules_configuration().
*/
function dcb_rules_default_rules_configuration() {
$items = array();
$items['rules_registration_for_event_while_signup'] = entity_import('rules_config', '{ "rules_registration_for_event_while_signup" : {
"LABEL" : "Registration for Event while signup",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "registration", "slack" ],
"REQUIRES" : [ "rules", "slack", "ticket" ],
"ON" : { "ticket_registration_insert" : [] },
"IF" : [
{ "data_is" : { "data" : [ "ticket-registration:bundle" ], "value" : "2" } }
],
"DO" : [
{ "slack_send_message" : { "message" : "Registration received by [ticket-registration:author:field-profile-first] [ticket-registration:author:field-profile-last] ([ticket-registration:author:mail]) for [ticket-registration:user:mail]" } }
]
}
}');
$items['rules_send_slack_notification_on_session_submission'] = entity_import('rules_config', '{ "rules_send_slack_notification_on_session_submission" : {
"LABEL" : "Send Slack Notification on Session submission",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"TAGS" : [ "session", "slack" ],
"REQUIRES" : [ "rules", "slack" ],
"ON" : {
"node_insert--session" : { "bundle" : "session" },
"node_update--session" : { "bundle" : "session" }
},
"DO" : [
{ "slack_send_message" : { "message" : "[node:author:field-profile-first] [node:author:field-profile-last] ([node:author:mail]) has just submitted a session on *[node:title]* under *[node:og-vocabulary]* track." } }
]
}
}');
return $items;
}

0 comments on commit c1c3e54

Please sign in to comment.