From 5230100320a6d9fc1ee697902654bf703ca6109a Mon Sep 17 00:00:00 2001 From: Oleg Korshunov Date: Mon, 29 Apr 2019 22:46:20 +0300 Subject: [PATCH 1/4] # Add RocketChatWebhook.php This need for wokr as webhook. * __construct - get post data as array $postData from json and $text as array from text message * function sendmessage() - send message to current channel # README.md + example usage WebHook Signed-off-by: Oleg Korshunov --- README.md | 8 +++++ src/RocketChatWebhuk.php | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/RocketChatWebhuk.php diff --git a/README.md b/README.md index e8364eb..4c7e798 100644 --- a/README.md +++ b/README.md @@ -68,3 +68,11 @@ $channel->postMessage('Hello world'); ``` ## Credits This REST client uses the excellent [Httpful](http://phphttpclient.com/) PHP library by [Nate Good](https://github.com/nategood) ([github repo is here](https://github.com/nategood/httpful)). + +## WebHook +``` +$WebHook= new \RocketChat\WebHook(); + +if ($WebHook->postData['user_name']!="rocket.cat") + $WebHook->sendmessage("Echo: ".$WebHook->postData['text']); +``` diff --git a/src/RocketChatWebhuk.php b/src/RocketChatWebhuk.php new file mode 100644 index 0000000..4759c41 --- /dev/null +++ b/src/RocketChatWebhuk.php @@ -0,0 +1,68 @@ + 'token', + 'bot' => false, + 'channel_id' => 'EtP4MMXZ8WHWEMw6e', + 'channel_name' => 'chanal', + 'message_id' => 'Wp2s2nDLpTGfp2eQp', + 'timestamp' => '2019-04-29T18:13:41.310Z', + 'user_id' => '13123123', + 'user_name' => 'user', + 'text' => 'edit text', + 'isEdited' => true, + ) + */ + + function __construct(){ + $this->postData=json_decode(file_get_contents('php://input'), true); + if (isset($this->postData['text'])) + $this->text = explode ("\n",$this->postData['text']); + } + public function getChannelId() + { + return $this->postData['channel_id']; + } + + public function escapeJsonString($value) { # list from www.json.org: (\b backspace, \f formfeed) + $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); + $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); + $result = str_replace($escapers, $replacements, $value); + return $result; +} + public function sendmessage($text){ + // Send message and EXIT script; + $text_valid=$this->escapeJsonString($text); + + $data = '{ + "text": "'.$text_valid.'"}'; +/* "attachments": [ + { + "title": "'.$text.'" + } + ] + }'; +/* + "title_link": "https://rocket.chat", + "text": "Rocket.Chat, the best open source chat", + "image_url": "https://rocket.chat/images/mockup.png", + "color": "#764FA5" + } + ] +}'; +*/ + header('Content-Type: application/json'); + echo $data; + exit; + } +} + From 4920f0481a7865d7c593b0ba7f29e777f109b9c4 Mon Sep 17 00:00:00 2001 From: Oleg Korshunov Date: Wed, 1 May 2019 18:14:19 +0300 Subject: [PATCH 2/4] # RocketChatChannel.php + clearHistory - Clean history of the channel. # RocketChatClient.php + getMessage - Get message by msgId. Need access for user to room or chat. Signed-off-by: Oleg Korshunov --- src/RocketChatChannel.php | 16 ++++++++++++++++ src/RocketChatClient.php | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/RocketChatChannel.php b/src/RocketChatChannel.php index b649098..798a4f8 100644 --- a/src/RocketChatChannel.php +++ b/src/RocketChatChannel.php @@ -200,6 +200,22 @@ public function removeOwner( $user ) { return false; } } + + /** + * Clean history of the channel. + */ + public function clearHistory($BEFORE_DAY=1){ + $response = Request::post( $this->api . 'rooms.cleanHistory' ) + ->body(array('roomId' => $this->id, "latest"=> date('Y-m-d',strtotime( $BEFORE_DAY.' days' ))."T".date('H:i:s').".304Z", "oldest"=> "2000-01-01T00:00:00.001Z")) + ->send(); + + if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { + return true; + } else { + echo( $response->body->error . "\n" ); + return false; + } + } } diff --git a/src/RocketChatClient.php b/src/RocketChatClient.php index f619ff0..ca1583b 100644 --- a/src/RocketChatClient.php +++ b/src/RocketChatClient.php @@ -95,4 +95,17 @@ public function list_channels() { } } + /** + * Get message by msgId. Need access for user to room or chat. + */ + public function getMessage($msgId) { + $response = Request::get( $this->api . 'chat.getMessage?msgId='.$msgId )->send(); + + if( $response->code == 200 && isset($response->body->success) && $response->body->success == true ) { + return $response->body->message; + } else { + echo( $response->body->error . "\n" ); + return false; + } + } } From e716ecf64d65ed20ae107f97a1c3efa98092c7ce Mon Sep 17 00:00:00 2001 From: Oleg Korshunov Date: Wed, 1 May 2019 18:30:40 +0300 Subject: [PATCH 3/4] * Formated code --- src/RocketChatWebhuk.php | 42 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/RocketChatWebhuk.php b/src/RocketChatWebhuk.php index 4759c41..9164a2d 100644 --- a/src/RocketChatWebhuk.php +++ b/src/RocketChatWebhuk.php @@ -2,12 +2,9 @@ namespace RocketChat; -#use Httpful\Request; -#use RocketChat\Client; - class WebHook extends Client { - public $text; - public $postData; + public $text; // Text message as array + public $postData; // Full message as array /* array ( 'token' => 'token', @@ -22,29 +19,32 @@ class WebHook extends Client { 'isEdited' => true, ) */ - + function __construct(){ $this->postData=json_decode(file_get_contents('php://input'), true); if (isset($this->postData['text'])) $this->text = explode ("\n",$this->postData['text']); } + public function getChannelId() { return $this->postData['channel_id']; } - + public function escapeJsonString($value) { # list from www.json.org: (\b backspace, \f formfeed) - $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); - $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); - $result = str_replace($escapers, $replacements, $value); - return $result; -} - public function sendmessage($text){ - // Send message and EXIT script; - $text_valid=$this->escapeJsonString($text); + $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); + $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); + $result = str_replace($escapers, $replacements, $value); + return $result; + } - $data = '{ - "text": "'.$text_valid.'"}'; + /** + * Send message via WebHook + */ + public function sendmessage($text){ + // Send message and EXIT script; + $text_valid=$this->escapeJsonString($text); + $data = '{"text": "'.$text_valid.'"}'; /* "attachments": [ { "title": "'.$text.'" @@ -60,9 +60,9 @@ public function sendmessage($text){ ] }'; */ - header('Content-Type: application/json'); - echo $data; - exit; - } + header('Content-Type: application/json'); + echo $data; + exit; + } } From 03f2009116e08f2af4fd2948c5380f6a861fa111 Mon Sep 17 00:00:00 2001 From: Oleg Korshunov Date: Wed, 1 May 2019 18:36:19 +0300 Subject: [PATCH 4/4] Rename file RocketChatWebhuk.php to RocketChatWebHook.php --- src/{RocketChatWebhuk.php => RocketChatWebHook.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{RocketChatWebhuk.php => RocketChatWebHook.php} (100%) diff --git a/src/RocketChatWebhuk.php b/src/RocketChatWebHook.php similarity index 100% rename from src/RocketChatWebhuk.php rename to src/RocketChatWebHook.php