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/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; + } + } } diff --git a/src/RocketChatWebHook.php b/src/RocketChatWebHook.php new file mode 100644 index 0000000..9164a2d --- /dev/null +++ b/src/RocketChatWebHook.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; + } + + /** + * 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.'" + } + ] + }'; +/* + "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; + } +} +