CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviSMS »
  • Inbound SMS processing
Pages: [1]

Author Topic: Inbound SMS processing  (Read 2549 times)

jcm55

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 14
Inbound SMS processing
November 10, 2012, 08:47:04 am
As part of a new fundraising effort, I'd like to set up some "pledge via SMS" functionality via CiviCRM. 

At our theatre performances, we'd ask patrons to send an SMS to our inbound SMS number with a message like "pledge 25" to indicate they'd like to donate $25.  If they show us their phone with the sent SMS on the way out, they'd get a small gift related to the show they just saw.  Later on, our staff would call them to get contact info and payment details for the pledge/donation.

Is this something that CiviSMS can handle -- or could handle with a small dev effort?  I don't want to reinvent the wheel if CiviSMS can do this or is close.

If not, I was planning to code this up using Twilio, which can do a GET/POST to a callback URL for each inbound SMS.  I'd have a bit of PHP at the callback URL to process the inbound SMS and do the following:

1) If the mobile number is associated with an existing contact, add that contact to a group like "Pledgers" and add a note to the contact record with the SMS content.
2) If the mobile number isn't associated with any contact, add a new contact record, and do the same re: group and note.

BTW, is it possible to have a contact record with no details other than a mobile number?

Later staff can go through all the contact records in the Pledgers group, contact them, process donations, and merge contact records if we discover that person already had a contact record, but without their mobile number.


Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Inbound SMS processing
November 11, 2012, 10:15:45 am

jim:

might want to contact michael mcandrew on irc (UK time) on this, since he's working on similar things and extending CiviSMS

I think most of the below can be down with CiviSMS and a few hooks (and if not, we should ensure it can do so)

1. I think an inbound SMS gets assigned to an activity and the contents of the SMS put in the (description or subject or result?)

2. I think it does item 2 today (w/o the group/note part but on an activity)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

jcm55

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 14
Re: Inbound SMS processing
November 15, 2012, 09:14:53 pm
Okay, I've got this completed now.  Two parts:

1) As of 4.2 at least, CiviSMS does not create new contact records for inbound SMS from unknown mobile numbers.  I've added support for that, along with improved support for SMS provider Twilio.  Opened CRM-11299 for this with patch attached.  Thanks to bpmccain for initial Twilio support patch he provided in CRM-11207.

2) I created an extension that implements hook_civicrm_post() to look for new "SMS Received" activities.  If the SMS body starts with the word "Pledge", then we add the contact to a group named "SMS Pledge" and send a "thanks for pledging" auto-response SMS.

If I have time (or collaborators) I'd like to flesh this out with an admin interface where you can create multiple rules/actions/auto-responses for inbound SMS messages.

Here's my hook for reference:

Code: [Select]
function inboundsms_civicrm_post( $op, $objectName, $id, &$params ) {
if (($objectName == 'Activity') && ($op == 'create') &&
(! strcmp($params->subject, 'SMS Received'))) {
// inbound SMS "rules"
$messageActions = Array ( 1 => Array (
'pattern' => '/^[Pp]led*ge/',
'group' => 'SMS Pledge',
'response' => "Thanks for your pledge to Bay Area Children's Theatre!  A staff member will contact you soon to complete your donation." ) );
// loop over pattern/action array
foreach ($messageActions as $key => $actions) {
if (preg_match($actions['pattern'], $params->details)) {
// check for existing group
$groupQueryParams = Array ( 'title' => $actions['group'] );
$messageActionGroup = CRM_Contact_BAO_Group::retrieve($groupQueryParams);
if (! $messageActionGroup) {
// create group if it doesn't exist
$groupQueryParams['description'] = $actions['group'];
$groupQueryParams['is_active'] = TRUE;
$messageActionGroup = CRM_Contact_BAO_Group::create($groupQueryParams);
}
$groupID = $messageActionGroup->id;
// add contact to group
$contacts = Array ( 1 => $params->source_contact_id );
$result = CRM_Contact_BAO_GroupContact::addContactsToGroup($contacts, $groupID);
// send SMS reply
$provider = CRM_SMS_Provider::singleton($_REQUEST);
$header = Array( 'To' => $params->phone_number,
'Contact' => $params->source_contact_id );
$recipients = Array();
$provider->send($recipients, $header, $actions['response']);
// return after first match
return;
}
}
}
}

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Inbound SMS processing
November 16, 2012, 01:00:33 am
hey there,

you can see what I have been getting up to extending sms here: https://github.com/michaelmcandrew/ff/tree/master/extensions/org.thirdsectordesign.chainsms

like you i have implemented hook_civicrm_post and that seems like a good solution.  i also made an sms Processor class
https://github.com/michaelmcandrew/ff/blob/master/extensions/org.thirdsectordesign.chainsms/CRM/Chainsms/Processor.php

which atm is just built to send back sms based on templates depending on your answers, but we could extend that to make api calls.

Quote
If I have time (or collaborators) I'd like to flesh this out with an admin interface where you can create multiple rules/actions/auto-responses for inbound SMS messages.
we could expand from just sms to other entities

 i am wondering if we are then beginning to create a workflow component (one step at a time)??

That would be cool and I have a feeling that there are a couple of modules that we could piggy back on for that.

Quote
CiviSMS does not create new contact records for inbound SMS from unknown mobile numbers

how do you get around the fact that you don't know these contact's names / email addresses?
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

jcm55

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 14
Re: Inbound SMS processing
November 16, 2012, 06:51:40 am
Quote from: Michael McAndrew on November 16, 2012, 01:00:33 am
how do you get around the fact that you don't know these contact's names / email addresses?

I make up a fake email address by appending "@mobile.sms" to the phone number, and leave the name empty.  In our use case, these are inbound "pledge by SMS" messages, and someone on our staff will call them back to fill out the rest of the contact details, or merge with an existing contact where we didn't have the mobile number on file.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviSMS »
  • Inbound SMS processing

This forum was archived on 2017-11-26.