Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 104 lines (90 sloc) 3.264 kb
e69f9e9 @tombell Update "s" scripts help comments
tombell authored
1 # Description:
2 # Can output total amount in your sales pipeline, as specified in a
3 # report from SugarCRM
4 #
5 # Dependencies:
6 # None
7 #
8 # Configuration:
9 # HUBOT_SUGARCRM_URL
10 # HUBOT_SUGARCRM_USERNAME
11 # HUBOT_SUGARCRM_PASSWORD
12 # HUBOT_SUGARCRM_REPORT_ID
13 # HUBOT_SUGARCRM_REPORT_FIELD
14 #
15 # Commands:
16 # hubot pipeline me - Gives the total amount in sales pipeline
17 #
18 # Author:
19 # skalnik
511fdd8 @skalnik Add SugarCRM script to fetch total from a report
skalnik authored
20
21 module.exports = (robot) ->
22 robot.respond /pipeline( me)?/i, (msg) ->
23 url = process.env.HUBOT_SUGARCRM_URL
24 username = process.env.HUBOT_SUGARCRM_USERNAME
25 password = process.env.HUBOT_SUGARCRM_PASSWORD
26 reportID = process.env.HUBOT_SUGARCRM_REPORT_ID
27 reportField = process.env.HUBOT_SUGARCRM_REPORT_FIELD
28
29 unless url
30 msg.send "SugarCRM URL isn't set."
31 msg.send "Please set the HUBOT_SUGARCRM_URL environment variable without prefixed HTTP or trailing slash"
32 return
33
34 unless username
35 msg.send "SugarCRM username isn't set."
36 msg.send "Please set the HUBOT_SUGARCRM_USERNAME environment variable"
37 return
38
39 unless password
40 msg.send "SugarCRM password isn't set."
41 msg.send "Please set the HUBOT_SUGARCRM_PASSWORD environment variable"
42 return
43
44 unless reportID
45 msg.send "SugarCRM report ID is not set."
46 msg.send "Please set the HUBOT_SUGARCRM_REPORT_ID to the report ID of your pipeline report"
47 return
48
49 unless reportField
50 msg.send "SugarCRM report field is not set."
51 msg.send "Please set the HUBOT_SUGARCRM_REPORT_FIELD to the field of the report that should be totaled"
52 return
53
54 sugarCRMLogin msg, url, username, password, (session) ->
55 data = {
56 session: session,
57 ids: [reportID]
58 }
59 sugarCRMCall msg, url, 'get_report_entries', data, (err, res, body) ->
60 json = JSON.parse(body)
61 entries = json.entry_list[0]
62 fields = json.field_list[0]
63 fieldID = -1
64 pipelineTotal = 0
65
66 for i in [0...fields.length]
67 fieldID = i if fields[i].name == reportField
68
69 if fieldID == -1
70 msg.send "Could not find " + reportField + " in the report fields."
71 msg.send "Please double check HUBOT_SUGARCRM_REPORT_FIELD."
72 return
73
74 for entry in entries
75 if entry.id
76 amount = (entry.name_value_list.filter (field) -> field.name == fieldID)[0]
77 pipelineTotal += parseInt(amount.value.replace(',', ''))
78
79 msg.send "Total: $" + pipelineTotal
80
81 sugarCRMLogin = (msg, url, user_name, password, callback) ->
82 crypto = require('crypto')
83 hashedPassword = crypto.createHash('md5').update(password).digest("hex")
84 data = {
85 user_auth: {
86 user_name: user_name,
87 password: hashedPassword
88 }
89 }
90 sugarCRMCall msg, url, 'login', data, (err, res, body) ->
91 sessionID = JSON.parse(body).id
92 callback(sessionID)
93
94 sugarCRMCall = (msg, url, method, data, callback) ->
95 msg.http('https://' + url + '/service/v4/rest.php')
96 .header('Content-Length', 0)
97 .query
98 method: method,
99 input_type: 'JSON',
100 response_type: 'JSON',
101 rest_data: JSON.stringify data
102 .post() (err, res, body) ->
103 callback(err, res, body)
Something went wrong with that request. Please try again.