parseBody($params['body']); $action = $parsed['verb']; $this->message = $parsed['message']; $this->support = Support::o(intval($parsed['support'])); $this->admin = $params['admin']; $this->body = $params['body']; $this->from = $params['from']; $this->media = $params['media']; $response = []; /* $session = Session_Twilio::o($action['session']); $session->twilio_id = $params['sid']; $session->phone = $params['from']; */ if ($this->support->id_support) { switch ($action) { case self::ACTION_CLOSE: $response = ['msg' => $this->close(), 'stop' => true]; break; case self::ACTION_REPLY: $response = ['msg' => $this->reply(), 'stop' => true]; break; case self::ACTION_DETAILS: $response = ['msg' => $this->status(), 'stop' => true]; break; case self::ACTION_HELP: $response = ['msg' => $this->help($order), 'stop' => false]; break; } } elseif ($action == self::ACTION_HELP) { $response = ['msg' => $this->help(), 'stop' => false]; } $this->response = (object)$response; } public function close() { $this->support->addSystemMessage($this->admin->name . ' closed the message from text message.' ); $this->support->status = Crunchbutton_Support::STATUS_CLOSED; $this->support->save(); $this->log( [ 'action' => 'closing support', 'id_support' => $this->support->id_support, 'phone' => $this->from, 'message' => $this->body] ); self::notifyReps($this->admin->firstName() . ' closed #' . $this->support->id_support, $support); } public function reply() { $this->support->addAdminMessage( [ 'phone' => $this->from, 'body' => $this->message, 'media' => $this->media ] ); $this->log( [ 'action' => 'saving the answer', 'id_support' => $this->support->id_support, 'phone' => $this->from, 'message' => $this->body] ); Crunchbutton_Message_Sms::send([ 'to' => $this->support->phone, 'message' => $this->admin->firstName() . ': '.$this->message, 'reason' => Crunchbutton_Message_Sms::REASON_SUPPORT ]); self::notifyReps($this->admin->firstName() . ' replied to #' . $this->support->id_support . ': ' . $this->message, $support); } public function details() { $response = 'From: '.$this->support->phone; if ($this->support->id_user) { $response .= "\nUser: ".$this->support->user()->name; } if ($this->support->id_order) { $response .= "\nOrder: #".$this->support->id_order; $date = new DateTime($this->support->order()->date, new DateTimeZone('America/Los_Angeles')); $date->setTimeZone(new DateTimeZone($this->support->order()->restaurant()->timezone)); $response .= "\nOrdered @ ".$date->format('n/j g:iA T'); $response .= "\nRestaurant: ".$this->support->order()->restaurant()->name; $response .= "\nDriver: ".$this->support->order()->status()->last()['driver']['name']; $response .= "\nStatus: ".$this->support->order()->status()->last()['status']; $date = new DateTime($this->support->order()->status()->last()['date'], new DateTimeZone('America/Los_Angeles')); $date->setTimeZone(new DateTimeZone($this->support->order()->restaurant()->timezone)); $response .= "\nUpdated @ ".$date->format('n/j g:iA T'); } return $response; } public static function notifyReps($message, $support = null, $media = null) { $to = []; $adminsg[] = Crunchbutton_Support::getUsers(); if ($support && $support->id_order && $support->order()->id_order) { $adminsg[] = $support->order()->restaurant()->adminReceiveSupportSMS(); } foreach ($adminsg as $admins) { if ($admins) { foreach ($admins as $admin) { $to[] = ['num' => $admin->txt, 'tz' => $admin->timezone]; } } } Crunchbutton_Message_Sms::send([ 'to' => $to, 'message' => $message, 'media' => $media, 'reason' => Crunchbutton_Message_Sms::REASON_SUPPORT ]); } public function help($order = null) { $response = "Support command usage: @".($order ? $order->id_order : 'order')." command|message\n". "Commands: \n". " close - close the ticket\n". " info - get info on the ticket\n". "Ex:\n". " @".($order ? $order->id_order : '123')." close\n". " @".($order ? $order->id_order : '123')." Hello there!"; $this->log( [ 'action' => 'help requested', 'invalidOrder' => $invalidOrder ] ); return $response; } public function parseBody($body) { $body = strtolower($body); $verbs = [ self::ACTION_CLOSE => [ 'close' ], self::ACTION_OPEN => [ 'open' ], self::ACTION_DETAILS => [ 'info', 'details' ], self::ACTION_HELP => [ 'help', 'h', 'info', 'commands', '\?', 'support'], self::ACTION_REPLY => [ '.*' ] ]; foreach ($verbs[self::ACTION_HELP] as $k => $verb) { $help .= ($help ? '$|^' : '').'\/?'.$verb; } if (preg_match('/^'.$help.'$/',$body)) { return ['verb' => self::ACTION_HELP, 'order' => null]; } foreach ($verbs as $verb => $verbList) { foreach ($verbList as $v) { if (preg_match('/^(\@|\#)([0-9]+) \/?('.$v.')$/', $body, $matches)) { return ['verb' => $verb, 'support' => $matches[2], 'message' => $matches[3]]; } } } return false; } public function log($content) { Log::debug( array_merge ( $content, [ 'type' => 'support-sms' ] ) ); } }