CS Auto-Reply #5042

This commit is contained in:
Pererinha 2015-04-03 14:53:28 -03:00
parent 7626abf299
commit 44a1b97377
6 changed files with 82 additions and 10 deletions

View File

@ -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");

View File

@ -0,0 +1 @@
ALTER TABLE support_message CHANGE COLUMN `type` `type` enum('sms','note','auto-reply') NOT NULL DEFAULT 'sms';

View File

@ -1,5 +1,5 @@
<?php
class Crunchbutton_Message_Incoming_Customer extends Cana_model {
const ACTION_STATUS = 'status';
@ -10,7 +10,7 @@ class Crunchbutton_Message_Incoming_Customer extends Cana_model {
$parsed = $this->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;
}

View File

@ -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) {

View File

@ -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){

View File

@ -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 {