From 44a1b973779b59c9747c8b9db9730926f6e58646 Mon Sep 17 00:00:00 2001 From: Pererinha Date: Fri, 3 Apr 2015 14:53:28 -0300 Subject: [PATCH] CS Auto-Reply #5042 --- db/migrate/000304_config.sql | 1 + db/migrate/000305_support_message.sql | 1 + .../Message/Incoming/Customer.php | 20 +++---- include/library/Crunchbutton/Message/Sms.php | 1 + include/library/Crunchbutton/Support.php | 57 +++++++++++++++++++ .../library/Crunchbutton/Support/Message.php | 12 ++++ 6 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 db/migrate/000304_config.sql create mode 100644 db/migrate/000305_support_message.sql diff --git a/db/migrate/000304_config.sql b/db/migrate/000304_config.sql new file mode 100644 index 000000000..9a6f895fb --- /dev/null +++ b/db/migrate/000304_config.sql @@ -0,0 +1 @@ +INSERT INTO `config` (`id_site`, `key`, `value`) VALUES (NULL,'auto-reply-text',"Hey, thanks for texting, we'll be with ya in a second! -Crunchbutton"); \ No newline at end of file diff --git a/db/migrate/000305_support_message.sql b/db/migrate/000305_support_message.sql new file mode 100644 index 000000000..e7f64f6ce --- /dev/null +++ b/db/migrate/000305_support_message.sql @@ -0,0 +1 @@ +ALTER TABLE support_message CHANGE COLUMN `type` `type` enum('sms','note','auto-reply') NOT NULL DEFAULT 'sms'; \ No newline at end of file diff --git a/include/library/Crunchbutton/Message/Incoming/Customer.php b/include/library/Crunchbutton/Message/Incoming/Customer.php index b01620241..70dc71369 100644 --- a/include/library/Crunchbutton/Message/Incoming/Customer.php +++ b/include/library/Crunchbutton/Message/Incoming/Customer.php @@ -1,5 +1,5 @@ parseBody($params['body']); $action = $parsed['verb']; - + $this->order = Order::q('select * from `order` where phone="'.$params['from'].'" order by date desc limit 1')->get(0); $this->support = Support::q(' select support.* from support @@ -40,7 +40,7 @@ class Crunchbutton_Message_Incoming_Customer extends Cana_model { $this->response = (object)$response; } - + public function status() { if ($this->support->id_order) { $response .= "\nOrder: #".$this->support->id_order; @@ -54,14 +54,14 @@ class Crunchbutton_Message_Incoming_Customer extends Cana_model { $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 function reply($params) { if (!$this->support->id_support || ($this->support->id_support && $this->support->id_order && $this->support->id_order != $this->order->id_order)) { @@ -99,7 +99,7 @@ class Crunchbutton_Message_Incoming_Customer extends Cana_model { $message .= ' DRIVER '.$params['admin']->name; } elseif ($this->order->id_order) { $message .= ' #'.$this->order->id_order.' '.$this->order->name; - + if ($created) { // send a message before support $types = $this->order->restaurant()->notification_types(); @@ -107,9 +107,9 @@ class Crunchbutton_Message_Incoming_Customer extends Cana_model { if (count($types) > 0) { $notifications = ' / RN: ' . join('/', $types); } - + $date = '%DATETIMETZ:'.$this->order->date()->format('Y-m-d H:i:s').'%'; - + $community = $this->order->restaurant()->communityNames(); if ($community) { $community = ' (' . $community . ')'; @@ -134,7 +134,7 @@ class Crunchbutton_Message_Incoming_Customer extends Cana_model { } public function help() { - $response = + $response = "Support usage: command|message\n". "Commands: \n". " status - show status of last order\n". @@ -155,7 +155,7 @@ class Crunchbutton_Message_Incoming_Customer extends Cana_model { self::ACTION_HELP => [ 'help', 'h', 'info', 'commands', '\?', 'support'], self::ACTION_REPLY => [ '.*' ] ]; - + foreach ($verbs[self::ACTION_HELP] as $k => $verb) { $help .= ($help ? '$|^' : '').'\/?'.$verb; } diff --git a/include/library/Crunchbutton/Message/Sms.php b/include/library/Crunchbutton/Message/Sms.php index b8c57c810..5483989bc 100644 --- a/include/library/Crunchbutton/Message/Sms.php +++ b/include/library/Crunchbutton/Message/Sms.php @@ -19,6 +19,7 @@ class Crunchbutton_Message_Sms extends Crunchbutton_Message { const REASON_GIFT_CARD = 'gift card'; const REASON_GIFT_CARD_REDEEMED = 'gift card redeemed'; const REASON_SETTLEMENT_FAIL = 'settlement fail'; + const REASON_AUTO_REPLY = 'auto reply'; public static function number($t = null) { if ($t) { diff --git a/include/library/Crunchbutton/Support.php b/include/library/Crunchbutton/Support.php index 3d5b4701b..e280c2e88 100644 --- a/include/library/Crunchbutton/Support.php +++ b/include/library/Crunchbutton/Support.php @@ -8,6 +8,8 @@ class Crunchbutton_Support extends Cana_Table_Trackchange { const TYPE_TICKET = 'TICKET'; const TYPE_COCKPIT_CHAT = 'COCKPIT_CHAT'; + const CONFIG_AUTO_REPLY_KEY = 'auto-reply-text'; + const STATUS_OPEN = 'open'; const STATUS_CLOSED = 'closed'; @@ -349,7 +351,62 @@ class Crunchbutton_Support extends Cana_Table_Trackchange { $messageParams[ 'phone' ] = $params[ 'phone' ]; $messageParams[ 'body' ] = $params[ 'body' ]; $messageParams[ 'media' ] = $params[ 'media' ]; + $this->addMessage( $messageParams ); + + // CS Auto-Reply #5042 + $support = $this; + Cana::timeout( function() use( $support ) { + $support->autoReply(); + } ); + } + + public function autoReply(){ + $body = $this->autoReplyMessage(); + if( $this->shoudAutoReply() && $body ){ + $messageParams[ 'type' ] = Crunchbutton_Support_Message::TYPE_AUTO_REPLY; + $messageParams[ 'from' ] = Crunchbutton_Support_Message::TYPE_FROM_SYSTEM; + $messageParams[ 'visibility' ] = Crunchbutton_Support_Message::TYPE_VISIBILITY_EXTERNAL; + $messageParams[ 'phone' ] = $params[ 'phone' ]; + $messageParams[ 'body' ] = $body; + $message = $this->addMessage( $messageParams ); + if( $message->id_support_message ){ + $message->notify(); + } + } + } + + public function autoReplyMessage(){ + $message = Crunchbutton_Config::q( 'SELECT * FROM config WHERE `key` = "' . Crunchbutton_Support::CONFIG_AUTO_REPLY_KEY . '" ORDER BY RAND() LIMIT 1' ); + if( $message->value ){ + return $message->value; + } + return false; + } + + public function lastAutoReplyByPhone( $phone ){ + $support_message = Crunchbutton_Support_Message::q( 'SELECT sm.* FROM support s + INNER JOIN support_message sm ON sm.id_support = s.id_support + WHERE s.phone = "' . $phone . '" + AND sm.type = "' . Crunchbutton_Support_Message::TYPE_AUTO_REPLY . '" + ORDER BY id_support_message DESC LIMIT 1' )->get( 0 ); + if( $support_message->id_support_message ){ + return $support_message; + } + return false; + } + + public function shoudAutoReply(){ + $last = $this->lastAutoReplyByPhone( $this->phone ); + if( !$last ){ + return true; + } else { + $now = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) ); + if( Crunchbutton_Util::intervalMoreThan24Hours( $now->diff( $last->date() ) ) ){ + return true; + } + } + return false; } public function addAdminReply($body, $guid = null){ diff --git a/include/library/Crunchbutton/Support/Message.php b/include/library/Crunchbutton/Support/Message.php index 90a28fdcc..c7369feff 100644 --- a/include/library/Crunchbutton/Support/Message.php +++ b/include/library/Crunchbutton/Support/Message.php @@ -4,6 +4,7 @@ class Crunchbutton_Support_Message extends Cana_Table { const TYPE_SMS = 'sms'; const TYPE_NOTE = 'note'; + const TYPE_AUTO_REPLY = 'auto-reply'; const TYPE_FROM_CLIENT = 'client'; const TYPE_FROM_REP = 'rep'; const TYPE_FROM_SYSTEM = 'system'; @@ -108,6 +109,17 @@ class Crunchbutton_Support_Message extends Cana_Table { $support = $this->support(); $phone = $support->phone; if (!$phone) return; + + // Auto reply messages + if( $this->type == Crunchbutton_Support_Message::TYPE_AUTO_REPLY && $this->body ){ + Crunchbutton_Message_Sms::send([ + 'to' => $phone, + 'message' => $this->body, + 'reason' => Crunchbutton_Message_Sms::REASON_AUTO_REPLY + ]); + return true; + } + if( $this->admin()->id_admin ){ $rep_name = $this->admin()->firstName(); } else {