Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20238 - Create hook for inbound SMS #10347

Merged
merged 3 commits into from
May 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions CRM/SMS/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

class CRM_SMS_Message {

/**
* @var String
* What address is this SMS message coming from.
*/
public $from = '';


/**
* @var String
* What address is this SMS message going to.
*/
public $to = '';

/**
* @var Integer
* Contact ID that is matched to the From address
*/
public $fromContactID = NULL;

/**
* @var Integer
* Contact ID that is matched to the To address
*/
public $toContactID = NULL;

/**
* @var String
* Body content of the message
*/
public $body = '';

/**
* @var Integer
* Trackable ID in the system to match to
*/
public $trackID = NULL;

}
55 changes: 34 additions & 21 deletions CRM/SMS/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,30 @@ public function retrieve($name, $type, $abort = TRUE, $default = NULL, $location
* @throws CRM_Core_Exception
*/
public function processInbound($from, $body, $to = NULL, $trackID = NULL) {
$formatFrom = $this->formatPhone($this->stripPhone($from), $like, "like");
$escapedFrom = CRM_Utils_Type::escape($formatFrom, 'String');
$fromContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE "%' . $escapedFrom . '"');
$message = new CRM_SMS_Message();
$message->from = $from;
$message->to = $to;
$message->body = $body;
$message->trackID = $trackID;
// call hook_civicrm_inboundSMS
CRM_Utils_Hook::inboundSMS($message);

if (!$message->fromContactID) {
// find sender by phone number if $fromContactID not set by hook
$formatFrom = '%' . $this->formatPhone($this->stripPhone($message->from), $like, "like");
$message->fromContactID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE %1", array(
1 => array($formatFrom, 'String')));
}

if (!$fromContactID) {
if (!$message->fromContactID) {
// unknown mobile sender -- create new contact
// use fake @mobile.sms email address for new contact since civi
// requires email or name for all contacts
$locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
$phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
$phoneloc = array_search('Home', $locationTypes);
$phonetype = array_search('Mobile', $phoneTypes);
$stripFrom = $this->stripPhone($from);
$stripFrom = $this->stripPhone($message->from);
$contactparams = array(
'contact_type' => 'Individual',
'email' => array(
Expand All @@ -233,38 +244,40 @@ public function processInbound($from, $body, $to = NULL, $trackID = NULL) {
),
);
$fromContact = CRM_Contact_BAO_Contact::create($contactparams, FALSE, TRUE, FALSE);
$fromContactID = $fromContact->id;
$message->fromContactID = $fromContact->id;
}

if ($to) {
$to = CRM_Utils_Type::escape($to, 'String');
$toContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE "%' . $to . '"');
}
else {
$toContactID = $fromContactID;
if (!$message->toContactID) {
// find recipient if $toContactID not set by hook
if ($message->to) {
$message->toContactID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE %1", array(
1 => array('%' . $message->to, 'String')));
}
else {
$message->toContactID = $message->fromContactID;
}
}

if ($fromContactID) {
if ($message->fromContactID) {
$actStatusIDs = array_flip(CRM_Core_OptionGroup::values('activity_status'));
$activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound SMS');

// note: lets not pass status here, assuming status will be updated by callback
$activityParams = array(
'source_contact_id' => $toContactID,
'target_contact_id' => $fromContactID,
'source_contact_id' => $message->toContactID,
'target_contact_id' => $message->fromContactID,
'activity_type_id' => $activityTypeID,
'activity_date_time' => date('YmdHis'),
'status_id' => $actStatusIDs['Completed'],
'details' => $body,
'phone_number' => $from,
'details' => $message->body,
'phone_number' => $message->from,
);
if ($trackID) {
$trackID = CRM_Utils_Type::escape($trackID, 'String');
$activityParams['result'] = $trackID;
if ($message->trackID) {
$activityParams['result'] = CRM_Utils_Type::escape($message->trackID, 'String');
}

$result = CRM_Activity_BAO_Activity::create($activityParams);
CRM_Core_Error::debug_log_message("Inbound SMS recorded for cid={$fromContactID}.");
CRM_Core_Error::debug_log_message("Inbound SMS recorded for cid={$message->fromContactID}.");
return $result;
}
}
Expand Down
11 changes: 11 additions & 0 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2299,4 +2299,15 @@ public static function geocoderFormat($geoProvider, &$values, $xml) {
);
}

/**
* This hook is called before an inbound SMS is processed.
*
* @param CRM_SMS_Message Object $message
* An SMS message recieved
* @return mixed
*/
public static function inboundSMS(&$message) {
return self::singleton()->invoke(array('message'), $message, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_inboundSMS');
}

}
21 changes: 21 additions & 0 deletions tests/phpunit/CRM/SMS/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ public function testProcessInboundNoTo() {
$this->assertEquals($contact['id'], $activity['target_contact_id'][0]);
}

/**
* CRM-20238 Add test of ProcessInbound function where no To number is passed into the function but the toContactId gets set in a hook
*/
public function testProcessInboundSetToContactIDUsingHook() {
$provider = new testSMSProvider();
$this->hookClass->setHook('civicrm_inboundSMS', array($this, 'smsHookTest'));
$result = $provider->processInbound('+61412345678', 'This is a test message', NULL, '12345');
$this->assertEquals('This is a test message', $result->details);
$this->assertEquals('+61412345678', $result->phone_number);
$this->assertEquals('12345', $result->result);
$contact = $this->callAPISuccess('contact', 'getsingle', array('phone' => '+61487654321'));
$activity = $this->callAPISuccess('activity', 'getsingle', array('id' => $result->id, 'return' => array('source_contact_id', 'target_contact_id', 'assignee_contact_id')));
$this->assertEquals($contact['id'], $activity['source_contact_id']);
}


public function smsHookTest(&$message) {
$testSourceContact = $this->individualCreate(array('phone' => array(1 => array('phone' => '+61487654321'))));
$message->toContactID = $testSourceContact;
}

}

/**
Expand Down