In the continuation of my last post "integration with Survey Monkey through Zapier".
I had another requirement to implement a customization to directly call the external API of Survey Monkey from Maximo.
Below are the 2 steps procedure, first is the list of APIs we are going to call in a sequence from Maximo Automation Script, and then the detail code I used.
Prerequisites:
- Survey Moneky Account, Token
- Design Survey and enable "Embed First Question" in the Email
- Create an Email collector
- Get the id of the newly created Email Collector
A. Three APIs we are going to call for this project from Automation Script:
- https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messages
- {"type": "invite","subject": "Survey for the Ticket {{myextrafield}} {{CustomData1}}","is_branding_enabled": true,"embed_first_question": true}
- https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messages/{{MESSAGE_ID}}/recipients
- { "email": "abdulqadeer.el@gmail.com", "custom_fields": { "1": "c1"}, "extra_fields": { "myextrafield": "1234" } }
- https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messages/{{MESSAGE_ID}}/send
- { }
B. Jython code can be implemented on any object, for testing I am using SR object before-save trigger.
from java.lang import Stringfrom java.io import BufferedReader, InputStreamReaderfrom java.net import URL, URLEncoderfrom java.nio.charset import StandardCharsetsfrom com.ibm.json.java import JSONObject, JSONArrayfrom psdi.server import MXServerfrom psdi.util.logging import MXLoggerFactorylogger = MXLoggerFactory.getLogger("maximo.script")logger.debug("Monkey Survey script Start")vTicketId = mbo.getString("TICKETID")if vTicketId == '10010':#1##################Start - new message creation in collector ------------------#request#https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messagesbaseUrl = "https://api.surveymonkey.net/v3/collectors/404826991/messages/"requestUrl = URL(baseUrl)conn = requestUrl.openConnection()conn.setRequestMethod('POST')conn.setRequestProperty('Content-Type', 'application/json')conn.setRequestProperty("Authorization", "Bearer XXXXXXXXXXX---TOKEN---XXXXXXXXXXXXXXXXXXXXXXX")conn.setDoOutput(True) # Enable writingpayload = '{ "type": "invite", "subject": "Survey for Ticketid", "is_branding_enabled": true, "embed_first_question": true}'os = conn.getOutputStream()os.write(payload)os.flush()#error logsif conn.getResponseCode() != 201:br = BufferedReader(InputStreamReader(conn.getErrorStream()))output = br.readLine()logger.error("Error: " + output)conn.disconnect()msg = "HTTP Error: " + str(conn.getResponseCode()) + " - " + str(conn.getResponseMessage() + " - " + output)logger.error(msg)service.error(str(conn.getResponseCode()), str(conn.getResponseMessage()) )#responsebr = BufferedReader(InputStreamReader(conn.getInputStream()))output = br.readLine()logger.info("OK: " + output)res = JSONObject.parse(output)vId = res.get("id")mbo.setValue("DESCRIPTION_LONGDESCRIPTION",output)mbo.setValue("description",vId)#conn closeconn.disconnect()#1##################End - new message creation in collector ------------------#2##################Start - adding recipients to a message creation in Step 1---------#request#https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messages/{{MESSAGE_ID}}/recipientsrequestUrl = URL(baseUrl + vId + "/recipients")conn = requestUrl.openConnection()conn.setRequestMethod('POST')conn.setRequestProperty('Content-Type', 'application/json')conn.setRequestProperty("Authorization", "Bearer XXXXXXXXXXX---TOKEN---XXXXXXXXXXXXXXXXXXXXXXX")conn.setDoOutput(True) # Enable writingpayload = '{ "email": "abdulqadeer.el@gmail.com" }'os = conn.getOutputStream()os.write(payload)os.flush()#error logsif conn.getResponseCode() != 201:br = BufferedReader(InputStreamReader(conn.getErrorStream()))output = br.readLine()logger.error("Error: " + output)conn.disconnect()msg = "HTTP Error: " + str(conn.getResponseCode()) + " - " + str(conn.getResponseMessage() + " - " + output)logger.error(msg)service.error(str(conn.getResponseCode()), str(conn.getResponseMessage()) )#responsebr = BufferedReader(InputStreamReader(conn.getInputStream()))output = br.readLine()logger.info("OK: " + output)# res = JSONObject.parse(output)# mbo.setValue("DESCRIPTION_LONGDESCRIPTION",output)# mbo.setValue("description",res.get("id"))#conn closeconn.disconnect()#2##################End - adding recipients to a message creation in Step 1---------#3##################Start - Send the message Now---------#request - POST#https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messages/{{MESSAGE_ID}}/sendrequestUrl = URL(baseUrl + vId + "/send")conn = requestUrl.openConnection()conn.setRequestMethod('POST')conn.setRequestProperty('Content-Type', 'application/json')conn.setRequestProperty("Authorization", "Bearer XXXXXXXXXXX---TOKEN---XXXXXXXXXXXXXXXXXXXXXXX")conn.setDoOutput(True) # Enable writingpayload = '{ }'os = conn.getOutputStream()os.write(payload)os.flush()#responsebr = BufferedReader(InputStreamReader(conn.getInputStream()))output = br.readLine()logger.info("OK: " + output)# res = JSONObject.parse(output)# mbo.setValue("DESCRIPTION_LONGDESCRIPTION",output)# mbo.setValue("description",res.get("id"))#conn closeconn.disconnect()#3##################End - Send the message Now---------
Thanks to these blog posts:
Brunu Portaluri from bportaluri.com
No comments:
Post a Comment