_request = $request; // $this->_chatConfig = $_chatConfig; parent::__construct($context); } public function getMessage() { try { $topic = $this->_request->getParams()["topic"]; $streamName = $this->_chatConfig->getStreamName(); $client = new Client(); $headers = [ 'Authorization' => $this->_chatConfig->getZulipAuthToken() ]; $url = $this->_chatConfig->getHostDomain().'api/v1/messages?anchor=newest&num_before=100&num_after=0&narrow=[{"operator":"stream","operand":"'.$streamName.'"},{"operator":"topic","operand":"'.$topic.'"}]&apply_markdown=false'; $request = new GuzzRequest('GET', $url, $headers); $promise = $client->sendAsync($request)->then(function ($response) { echo $response->getBody(); exit(); }); $promise->wait(); } catch (\Exception $e) { return json_encode(["code" => 500, "message" => "Error".$e]); } } public function getMessageUnRead() { try { $topic = $this->_request->getParams()["topic"]; $streamName = $this->_chatConfig->getStreamName(); $client = new Client(); $headers = [ 'Authorization' => $this->_chatConfig->getZulipAuthToken() ]; $url = $this->_chatConfig->getHostDomain().'api/v1/messages?anchor=first_unread&num_before=0&num_after=99&narrow=[{"operator":"stream","operand":"'.$streamName.'"},{"operator":"topic","operand":"'.$topic.'"}]&apply_markdown=false'; $request = new GuzzRequest('GET', $url, $headers); $promise = $client->sendAsync($request)->then(function ($response) { echo $response->getBody(); exit(); }); $promise->wait(); } catch (\Exception $e) { return json_encode(["code" => 500, "message" => "Error".$e]); } } public function registerQueue() { try { $topic = $this->_request->getParams()["topic"]; $streamName = $this->_chatConfig->getStreamName(); $client = new Client(); $headers = [ 'Authorization' => $this->_chatConfig->getZulipAuthToken() ]; $options = [ 'multipart' => [ [ 'name' => 'event_types', 'contents' => '["message"]', ], [ 'name' => 'narrow', 'contents' => '[["stream", "'.$streamName.'"], ["topic", "'.$topic.'"]]', ] ] ]; $request = new GuzzRequest('POST', $this->_chatConfig->getHostDomain().'api/v1/register', $headers); $promise = $client->sendAsync($request, $options)->then(function ($response) { echo $response->getBody(); exit(); }); $promise->wait(); } catch (\Exception $e) { return json_encode(["code" => 500, "message" => "Error".$e]); } } public function listenMessage() { try { $params = $this->_request->getParams(); $queueId = $params["queue_id"]; $lastEventId = $params["last_event_id"]; $client = new Client(); $headers = [ 'Authorization' => $this->_chatConfig->getZulipAuthToken() ]; $url = $this->_chatConfig->getHostDomain().'api/v1/events?queue_id='.$queueId.'&last_event_id='.$lastEventId; $request = new GuzzRequest('GET', $url, $headers); $promise = $client->sendAsync($request)->then(function ($response) { echo $response->getBody(); exit(); }); $promise->wait(); } catch (\Exception $e) { return json_encode(["code" => 500, "message" => "Error".$e]); } } public function sendMessage() { try { $params = $this->_request->getParams(); $content = $params["content"]; $topic = $params["topic"]; $streamName = $this->_chatConfig->getStreamName(); $client = new Client(); $headers = [ 'Authorization' => $this->_chatConfig->getZulipAuthToken() ]; $url = $this->_chatConfig->getHostDomain().'api/v1/messages'; $options = [ 'multipart' => [ [ 'name' => 'to', 'contents' => $streamName, ], [ 'name' => 'type', 'contents' => 'stream', ], [ 'name' => 'topic', 'contents' => $topic, ], [ 'name' => 'content', 'contents' => $content, ] ] ]; $request = new GuzzRequest('POST', $url, $headers); $promise = $client->sendAsync($request, $options)->then(function ($response) { return json_encode(["code" => 200, "message" => "Success"]); }); $promise->wait(); } catch (\Exception $e) { return json_encode(["code" => 500, "message" => "Error".$e]); } } public function markTopicAsRead() { try { $params = $this->_request->getParams(); $topic = $params["topic"]; $streamID = $this->_chatConfig->getStreamID(); $client = new Client(); $headers = [ 'Authorization' => $this->_chatConfig->getZulipAuthToken() ]; $url = $this->_chatConfig->getHostDomain().'api/v1/mark_topic_as_read'; $options = [ 'multipart' => [ [ 'name' => 'stream_id', 'contents' => $streamID, ], [ 'name' => 'topic_name', 'contents' => $topic, ] ] ]; $request = new GuzzRequest('POST', $url, $headers); $promise = $client->sendAsync($request, $options)->then(function ($response) { return json_encode(["code" => 200, "message" => "Success"]); }); $promise->wait(); } catch (\Exception $e) { return json_encode(["code" => 500, "message" => "Error".$e]); } } /** * Api for make offer * @param string[] $data * @return string */ public function makeAnOffer($data) { try { $topic = $data["customer-email"]; // $streamName = $this->_chatConfig->getStreamMakeOffer(); $streamName = $this->_chatConfig->getStreamName(); $content = "Product: [".$data["product-sku"]."](".$data["product-url"].") \nSell Price: **".$data["product-sell-price"]."** \nCustomer Offer Price: **".$data["customer-offer"]."** \nCustomer: ".$data["customer-name"]." \nEmail: ".$data["customer-email"]." \nPhone: ".$data["customer-mobile-phone"]." \nCustomer shipping destination: ".$data["customer-country"]." \nMessage: ".$data["customer-message"]; $client = new Client(); $headers = [ 'Authorization' => $this->_chatConfig->getZulipAuthToken() ]; $url = $this->_chatConfig->getHostDomain().'api/v1/messages'; $options = [ 'multipart' => [ [ 'name' => 'to', 'contents' => $streamName, ], [ 'name' => 'type', 'contents' => 'stream', ], [ 'name' => 'topic', 'contents' => $topic, ], [ 'name' => 'content', 'contents' => $content, ] ] ]; $request = new GuzzRequest('POST', $url, $headers); $promise = $client->sendAsync($request, $options)->then(function ($response) { return true; }); $promise->wait(); return $request->getBody()->getContents(); } catch (\Exception $e) { return false; } } }