Skip to content

Instantly share code, notes, and snippets.

@mlutfy
Created December 6, 2016 14:18
Show Gist options
  • Save mlutfy/872aea83872a527ca1524a09c85200f2 to your computer and use it in GitHub Desktop.
Save mlutfy/872aea83872a527ca1524a09c85200f2 to your computer and use it in GitHub Desktop.
/*
Description:
CiviCRM contact search
Configuration:
see ../config/civicrm.js (shared with civicrm-timetrack.js).
https://gist.github.com/mlutfy/b452f628bd79103a98283c48a2a39770
Commands:
!pi [...]
!po
*/
var _ = require('underscore');
var settings = {};
_.defaults(settings, require('./../config/civicrm.js'));
var crmAPI = require('civicrm')(settings.civicrm);
module.exports = function(robot) {
/**
* Repond to "search" command.
*/
robot.respond(/search\s+(.*)/, function(msg) {
var query = msg.match[1];
msg.reply('Searching...');
var params = {
display_name: query,
return: 'contact_sub_type,contact_type,contact_id,email'
}
crmAPI.call('Contact', 'get', params,
function(data) {
if (data.is_error) {
msg.reply(":fire: ERROR " + data.error_message);
return;
}
data.values.forEach(function(element, index, array) {
var ctype = (element.contact_sub_type ? element.contact_sub_type.join('+') : element.contact_type);
var icon = (element.contact_type == 'Organization' ? ':department_store:' : ':bust_in_silhouette:');
var output = icon + ' ' + ctype + ' #' + element.contact_id + ': ' + element.display_name + ' ' + (element.email ? element.email + ' ' : '');
// Fetch phone explicitely, since sometimes not getting the extension correctly.
crmAPI.call('Phone', 'get', { contact_id: element.contact_id }, function(data2) {
if (data2.is_error) {
msg.reply(":fire: ERROR " + data2.error_message);
return;
}
data2.values.forEach(function(el2, i2, a2) {
output += ' ' + el2.phone + (el2.phone_ext ? 'ext: ' + el2.phone_ext : '');
});
msg.reply(output);
});
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment