Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
88 lines (74 sloc) 2.94 KB
# Can output total amount in your sales pipeline, as specified in a report from SugarCRM
# pipeline me - Gives the total amount in sales pipeline
module.exports = (robot) ->
robot.respond /pipeline( me)?/i, (msg) ->
url = process.env.HUBOT_SUGARCRM_URL
username = process.env.HUBOT_SUGARCRM_USERNAME
password = process.env.HUBOT_SUGARCRM_PASSWORD
reportID = process.env.HUBOT_SUGARCRM_REPORT_ID
reportField = process.env.HUBOT_SUGARCRM_REPORT_FIELD
unless url
msg.send "SugarCRM URL isn't set."
msg.send "Please set the HUBOT_SUGARCRM_URL environment variable without prefixed HTTP or trailing slash"
unless username
msg.send "SugarCRM username isn't set."
msg.send "Please set the HUBOT_SUGARCRM_USERNAME environment variable"
unless password
msg.send "SugarCRM password isn't set."
msg.send "Please set the HUBOT_SUGARCRM_PASSWORD environment variable"
unless reportID
msg.send "SugarCRM report ID is not set."
msg.send "Please set the HUBOT_SUGARCRM_REPORT_ID to the report ID of your pipeline report"
unless reportField
msg.send "SugarCRM report field is not set."
msg.send "Please set the HUBOT_SUGARCRM_REPORT_FIELD to the field of the report that should be totaled"
sugarCRMLogin msg, url, username, password, (session) ->
data = {
session: session,
ids: [reportID]
sugarCRMCall msg, url, 'get_report_entries', data, (err, res, body) ->
json = JSON.parse(body)
entries = json.entry_list[0]
fields = json.field_list[0]
fieldID = -1
pipelineTotal = 0
for i in [0...fields.length]
fieldID = i if fields[i].name == reportField
if fieldID == -1
msg.send "Could not find " + reportField + " in the report fields."
msg.send "Please double check HUBOT_SUGARCRM_REPORT_FIELD."
for entry in entries
amount = (entry.name_value_list.filter (field) -> == fieldID)[0]
pipelineTotal += parseInt(amount.value.replace(',', ''))
msg.send "Total: $" + pipelineTotal
sugarCRMLogin = (msg, url, user_name, password, callback) ->
crypto = require('crypto')
hashedPassword = crypto.createHash('md5').update(password).digest("hex")
data = {
user_auth: {
user_name: user_name,
password: hashedPassword
sugarCRMCall msg, url, 'login', data, (err, res, body) ->
sessionID = JSON.parse(body).id
sugarCRMCall = (msg, url, method, data, callback) ->
msg.http('https://' + url + '/service/v4/rest.php')
.header('Content-Length', 0)
method: method,
input_type: 'JSON',
response_type: 'JSON',
rest_data: JSON.stringify data
.post() (err, res, body) ->
callback(err, res, body)
Jump to Line
Something went wrong with that request. Please try again.