Sunday, 23 May 2021

SurveyMonkey Integration – Call External API from Automation Script | Maximo

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: 

  1. https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messages
    1. {
        "type""invite",
        "subject""Survey for the Ticket {{myextrafield}} {{CustomData1}}",
        "is_branding_enabled"true,
        "embed_first_question"true
      }
  2. https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messages/{{MESSAGE_ID}}/recipients
    1. {     "email""abdulqadeer.el@gmail.com",     "custom_fields": {    "1""c1"},     "extra_fields": {     "myextrafield""1234"   }    }
  3. https://api.surveymonkey.net/v3/collectors/{{COLLECTOR_ID}}/messages/{{MESSAGE_ID}}/send
    1. { }

B. Jython code can be implemented on any object, for testing I am using SR object before-save trigger. 

from java.lang import String
from java.io import BufferedReader, InputStreamReader
from java.net import URL, URLEncoder
from java.nio.charset import StandardCharsets
from com.ibm.json.java import JSONObject, JSONArray
from psdi.server import MXServer
from psdi.util.logging import MXLoggerFactory

logger = 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}}/messages
    baseUrl = "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 writing

    payload = '{   "type": "invite",  "subject": "Survey for Ticketid",  "is_branding_enabled": true,  "embed_first_question": true}'
    
    os = conn.getOutputStream()
    os.write(payload)
    os.flush()

#error logs  
    if 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()) )

#response    
    br = 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 close    
    conn.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}}/recipients
    requestUrl = 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 writing

    payload = '{    "email": "abdulqadeer.el@gmail.com"    }'
    os = conn.getOutputStream()
    os.write(payload)
    os.flush()

#error logs  
    if 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()) )

#response    
    br = 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 close    
    conn.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}}/send
    requestUrl = 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 writing

    payload = '{ }'
    os = conn.getOutputStream()
    os.write(payload)
    os.flush()

#response    
    br = 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 close    
    conn.disconnect()
#3##################End - Send the message Now---------


Thanks to these blog posts:

IBM support Page

Brunu Portaluri from bportaluri.com

Velibor Djukic from bpdzenith

Steven Shull from ProjectTech

No comments:

Post a Comment