Skip to content

Instantly share code, notes, and snippets.

@zimaben
Last active December 30, 2021 11:40
Show Gist options
  • Save zimaben/bbb34a12bc8a396166944fd3f68dbae4 to your computer and use it in GitHub Desktop.
Save zimaben/bbb34a12bc8a396166944fd3f68dbae4 to your computer and use it in GitHub Desktop.
<?php
namespace justinexample\integrations;
Class SlackIntegration {
#this is here to carry information from the WP environment to Slack, you can add args as needed for your application
private static $allowed_args = array(
'message',
);
public $err = false;
public function __construct( $name, $args = array() ){
$this->name = $name ? $this->checkName($name) : false;
$this->args = $this->checkArgs($args);
}
private function createProp($prop, $value){
$this->{$prop} = $value;
}
public function checkArgs( $args ){
if(empty($args)) return false;
foreach($args as $key=>$value){
if(in_array($key, self::$allowed_args)){
$this->createProp($key, $value);
}
}
return $args;
}
public function checkName( $name ){
$info = $this->getBotInfo( $name );
if(is_array($info)){
foreach($info as $k=>$v){
$this->createProp($k,$v);
}
return $name;
} else {
$this->err = $info;
return false;
}
}
public function getWebhook(){
return isset($this->webhook) ? $this->webhook : false;
}
public function getId(){
return isset($this->id) ? $this->id : false;
}
public function getChannel(){
return isset($this->channel) ? $this->channel : false;
}
public function postMessage( $message = null ){
$this->err = false;
if(!$message) $message = (isset($this->message) && $this->message) ? $this->message : false;
if($message){
if($this->webhook){
$body = array('text'=>$message);
#if(isset($this->channel)) $body['channel'] = $this->channel;
if( !\wp_remote_post($this->webhook,
array(
'headers'=> array('content-type'=>'application/json'),
'body'=> json_encode($body)
)
)
){
$this->err = 'Problem with Post Request';
}
} else { $this->err = 'Webhook not Set'; }
} else { $this->err = 'Message not Set'; }
}
#requires Carbon Themes, but only for a quick UI to get the info into the database
#see https://gist.github.com/zimaben/291fa687287cfffe12cc96db899aec0b for this integration
#you can add theme options manually, it will be without admin page and UI unless you build it yourself
private function getBotInfo( $string ){
$return_array = array();
$bots = \carbon_get_theme_option( 'slack_bots');
if(is_array($bots) && !empty($bots)){
foreach($bots as $bot){
$name = $bot['slack_bot_name'];
if( trim(strtolower($name)) === trim(strtolower($string)) ){
$channel = $bot['slack_bot_channel'];
$id = $bot['slack_bot_customer_id'];
$webhook = $bot['slack_bot_webhook'];
$return_array['name'] = $name;
$return_array['id'] = $id;
$return_array['channel'] = $channel;
$return_array['webhook'] = $webhook;
}
}
return (empty($return_array)) ? 'Could not find that Integration' : $return_array;
} else {
return 'No bots set up';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment