Skip to end of metadata
Go to start of metadata

Author: Heber Olivar Silva
Submitted: 2010-04-11
Related Links:

  • N/A

Description

/* Using the SOAP state code generated automatically by BCM IVR Admin, */
/* I used a available internet web-service just to query a dummy value */
 

  # --------------------------------------------------------------------------------
  # STATE_200 (SOAP)
  #
  # *** QUERY THIRD-PARTY VALUES WITH WEBSERVICE ***
  #
  # No state start delay
  # State timeout (ms):                                          10000
  # SOAP server URL:                                             http://www.webservicex.net/convertmetricweight.asmx?wsdl
  # SOAP server port:                                            80
  # SOAP method:
  # - Name:                                                      ChangeMetricWeightUnit
  # - Parameters:                                                3
  #   [fromMetricWeightUnit] = "gram"
  #   [MetricWeightValue] = <LASTDTMFVALUE>
  #   [toMetricWeightUnit] = "kilogram"
  # Return data array required.
  # Return data save mode                                        <OVERWRITE>
  # Next state when SOAP ok:                                     STATE_210
  # Next state if SOAP failed:                                   STATE_140
  # --------------------------------------------------------------------------------
  def STATE_200(self):

    if self.ON_ENTER_STATE():
      if self.g_DebugLevel >= 7: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Starting SOAP-type state")
      self.g_StateDataArray = {}
      if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Trying to start SOAP timer with client state method [STATE_200_SOAPMethod] and wait (10000 ms) for return value...")
      self.NEW(TIMER,"SOAPTIMER")
      try:
        self.SOAPTIMER("START",
                       10000,
                       _EXEC_FUNC = self.STATE_200_SOAPMethod)
      except(Exception), err:
        self.m_E.exc("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Exception occurred (" + str(err) + ") - Starting SOAP timer failed (invalid client state method or not defined?), going to the alternative state [STATE_140]...")
        self.NEXT_STATE(self.STATE_140)

    elif self.MORE() and self("SOAPTIMER",
                              "OK"):
      self.g_StateDataArray = self.EVT("_RETURN") # Get return data (dictionary expected).
      if len(self.g_StateDataArray) > 0:
        if self.g_DebugLevel >= 9: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: SOAP request returned an array (dictionary) with " + str(len(self.g_StateDataArray)) + " items...")
        if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Overwriting current external data array with received data...")
        self.g_ExternalDataArray = self.g_StateDataArray
        if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Going to the next state [STATE_210]...")
        self.NEXT_STATE(self.STATE_210)
      else:
        if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: SOAP request did not return any valid data, going to the alternative state [STATE_140]...")
        self.NEXT_STATE(self.STATE_140)

    elif self.MORE() and self("SOAPTIMER",
                              "TIMEOUT"):
      if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: State timeout (10000 ms) reached and SOAP answer still not received, going to the alternative state [STATE_140]...")
      self.NEXT_STATE(self.STATE_140)

    elif self.MORE() and self("SOAPTIMER",
                              "ERROR"):
      if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: SOAP operation failed (SOAP server not connected, invalid request or invalid return data type?), going to the alternative state [STATE_140]...")
      self.NEXT_STATE(self.STATE_140)

    elif self.MORE() and self("CALL",
                              "WAVE_CMD_DONE"):
      if self.g_DebugLevel >= 9: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Unexpected enhanced file session event received, ignoring...")
      self.NEXT_STATE(self.STATE_200) # RESUME.

    elif self.MORE() and self("CALL",
                              "ERROR"):
      sCmd = self.EVT("Cmd")
      self.m_E.err("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Call error occurred [" + str(sCmd) + "].")
      if sCmd == "STOP_PLAY" or \
         sCmd == "STOP_RECORD" or \
         sCmd == "STOP_DTMF":
        if self.g_DebugLevel >= 9: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Ignoring...")
        self.NEXT_STATE(self.STATE_200) # RESUME.
      else:
        if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Going to the alternative state [STATE_140]...")
        self.NEXT_STATE(self.STATE_140)

    elif self.MORE() and self("CALL",
                              "CALL_DISCONNECTED"):
      if self.g_DebugLevel >= 9: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Call disconnected...")
      if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: Going to the end state [END_STATE]...")
      self.NEXT_STATE(self.END_STATE)

    elif self.MORE() and self("SMTIMER",
                              "TIMEOUT"):
      if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200]: State machine timeout (1800000 ms) reached, going to the end state [END_STATE]...")
      self.NEXT_STATE(self.END_STATE)

    elif self.ON_LEAVE_STATE():
      self.g_StateDataArray = {}

  # --------------------------------------------------------------------------------
  # STATE_200 customized SOAP method.
  # --------------------------------------------------------------------------------
  def STATE_200_SOAPMethod(self, cmd, params):

    if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200_SOAPMethod]: Trying to open connection to SOAP server from address [http://www.webservicex.net/convertmetricweight.asmx?wsdl] and port [80]...")
    try:
      import soaplib
      sAddress = "HTTP://www.webservicex.net/convertMetricWeight.asmx?WSDL"
      lAddress     = string.split(string.replace(sAddress, "HTTP://", ""), "/")
      sSOAPAddress = string.replace(sAddress, lAddress[0], lAddress[0] + ":80")
      sproxy       = soaplib.ServerProxy(sSOAPAddress)
      if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200_SOAPMethod]: SOAP connection opened, sending request method [ChangeMetricWeightUnit] with 3 parameters to SOAP server (" + sSOAPAddress + ")...")
      response = sproxy.ChangeMetricWeightUnit(fromMetricWeightUnit = "gram",
                                              MetricWeightValue = self.g_LastReceivedDTMFString,
                                              toMetricWeightUnit = "kilogram")
    except(Exception), err:
      self.m_E.exc("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_200_SOAPMethod]: Exception occurred (" + str(err) + "), returning ERROR.")
      return "ERROR"
    p_DictionaryKeys = []
    if len(response) > 1 and len(response[1]) > 0: # response: (0 = List, 1 = Dictionary).
      p_DictionaryKeys = response[1].keys()
    if len(p_DictionaryKeys) > 0: # Check received dictionary.
      return ("OK", response[1])
    else:
      return ("OK", {})


 
/* Using the EXTERNALDATA state code generated automatically by BCM IVR Admin,                                           */
/* I enhanced this code to validade the returned value from webservice and create a condition to decide the call routing */
 
 
  # --------------------------------------------------------------------------------
  # STATE_210 (EXTERNALDATA)
  #
  # *** CHECK THE RETURNED VALUE !!! ***
  #
  # No state start delay
  # External data validity fields:                 1
  #   [ChangeMetricWeightUnitResult] = <REQUIRENUMERICVALUE>
  # Save as data buffer value:                     External data field [ChangeMetricWeightUnitResult] value
  # Next state when external data validated:       STATE_220
  # Next state if external data not validated:     STATE_100
  # --------------------------------------------------------------------------------
  def STATE_210(self):

    if self.ON_ENTER_STATE():
      if self.g_DebugLevel >= 7: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Starting EXTERNALDATA-type state")
      if len(self.g_ExternalDataArray) > 0:
        try:
          if IsNumeric(self.g_ExternalDataArray.get("ChangeMetricWeightUnitResult")) == "TRUE":
            if self.g_DebugLevel >= 9: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: All external data field values were successfully validated...")
            if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Saving external data field [ChangeMetricWeightUnitResult] value [" + str(self.g_ExternalDataArray.get("ChangeMetricWeightUnitResult")) + "] to data buffer...")
            self.g_LastFoundDBValue = str(self.g_ExternalDataArray.get("ChangeMetricWeightUnitResult"))

 
            # --- Enhanced by Heber Olivar Silva ---
            #
            if self.g_LastFoundDBValue == "1":
              if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Value = 1, Going to the next state [STATE_220]...")
              self.NEXT_STATE(self.STATE_220)
            else:
              if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Value <> 1, Going to the next state [STATE_110]...")
              self.NEXT_STATE(self.STATE_110)
            #
            # if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Going to the next state [STATE_220]...")
            # self.NEXT_STATE(self.STATE_220)
            #
            # --- Enhanced by Heber Olivar Silva ---
 

          else:
            if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: All external data field values were not validated, going to the alternative state [STATE_100]...")
            self.NEXT_STATE(self.STATE_100)
        except:
          self.m_E.exc("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Exception occurred - Unable to handle current external data array (undefined field name?), going to the alternative state [STATE_100]...")
          self.NEXT_STATE(self.STATE_100)
      else:
        if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Current external data array size (" + str(len(self.g_ExternalDataArray)) + ") does not meet the requirements of validating (1 fields), going to the alternative state [STATE_100]...")
        self.NEXT_STATE(self.STATE_100)

    elif self.MORE() and self("CALL",
                              "WAVE_CMD_DONE"):
      if self.g_DebugLevel >= 9: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Unexpected enhanced file session event received, ignoring...")
      self.NEXT_STATE(self.STATE_210) # RESUME.

    elif self.MORE() and self("CALL",
                              "ERROR"):
      sCmd = self.EVT("Cmd")
      self.m_E.err("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Call error occurred [" + str(sCmd) + "].")
      if sCmd == "STOP_PLAY" or \
         sCmd == "STOP_RECORD" or \
         sCmd == "STOP_DTMF":
        if self.g_DebugLevel >= 9: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Ignoring...")
        self.NEXT_STATE(self.STATE_210) # RESUME.
      else:
        if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Going to the alternative state [STATE_100]...")
        self.NEXT_STATE(self.STATE_100)

    elif self.MORE() and self("CALL",
                              "CALL_DISCONNECTED"):
      if self.g_DebugLevel >= 9: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Call disconnected...")
      if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: Going to the end state [END_STATE]...")
      self.NEXT_STATE(self.END_STATE)

    elif self.MORE() and self("SMTIMER",
                              "TIMEOUT"):
      if self.g_DebugLevel >= 8: self.m_E.dbg("FILE=[IVR_2191_URA] CLASS=[IVR_2191] STATE=[STATE_210]: State machine timeout (1800000 ms) reached, going to the end state [END_STATE]...")
      self.NEXT_STATE(self.END_STATE)

    elif self.ON_LEAVE_STATE():
      pass

# --------------------------------------------------------------------------------