Menu

Related issues regarding ITOP webhook customizing the send payload

christ
2024-02-27
2024-03-01
  • christ

    christ - 2024-02-27

    Hello everyone!
    I haven't fully learned the description of ITOP webhook customizing the send payload in the official document. Can someone provide a demo to demonstrate how to use it. I tried to write a PHP file, but it didn't work. The ITOP log gave an error message“
    Error: Prepare payload callback is not callable.

     
    😕
    1
  • Guillaume Lajarige

    Hello Christ,

    Your PHP method must be loaded in iTop, the main way to do this is through XML in an extension (see here).

    Here is an XML sample that contains 2 methods:
    - 1 to prepare a payload for a distant iTop, change the method's code to your purpose
    - 1 to process the response, which just outputs the response in the error log

    <?xml version="1.0" encoding="UTF-8"?>
    <itop_design xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0">
        <snippets>
            <snippet id="StaticMethodsForWebhooks" _delta="define">
                <placement>core</placement>
                <rank>1</rank>
                <content><![CDATA[
    class CallbacksClass
    {
        static public function PrepareRequest($oObject, $aContextArgs, $oLog, $oAction)
        {
            $sActionClass = get_class($oAction);
    
            // Prepare JSON data
            $sJSON = <<<JSON
    {
       "operation": "core/create",
       "comment": "Synchronization from WebHook.",
       "class": "UserRequest",
       "output_fields": "id, friendlyname",
       "fields":
       {
          "org_id": "SELECT Organization WHERE id = \"\$this->org_id$\"",
          "caller_id":
          {
             "name": "\$this->caller_id->name$",
             "first_name": "\$this->caller_id->first_name$"
          },
          "title": "[iTop 3.0 / $sActionClass action / Static request] \$this->title$",
          "description": "\$this->description$",
          "public_log": "Créé depuis mon iTop 3.0"
       }
    }
    JSON;
    
            // Retrieve connection to get REST API version
            $oRemoteApplicationconnection = $oAction->GetRemoteApplicationConnection();
    
            // Retrieve basis parameters
            $aParameters = array(
                'version' => MetaModel::ApplyParams($oRemoteApplicationconnection->Get('version'), $aContextArgs),
                'json_data' => MetaModel::ApplyParams($sJSON, $aContextArgs),
            );
            foreach($aParameters as $sParamName => $sParamValue)
            {
                if(empty($sParamValue))
                {
                    throw new Exception('Missing at least of the following mandatory parameters: '.implode(', ', array_keys($aParameters)));
                }
            }
    
            // Transform array to HTTP query parameters as requested by iTop
            return http_build_query($aParameters);
        }
    
        static public function ProcessResponse($oObject, $oResponse, $oAction)
        {
            \IssueLog::Error('========== ProcessResponse in Static class ===========');
            \IssueLog::Error(print_r($oObject, true));
            \IssueLog::Error(print_r($oResponse, true));
            \IssueLog::Error(print_r($oAction, true));
        }
    }
    ]]></content>
            </snippet>
        </snippets>
    </itop_design>
    

    Then put \CallbacksClass::PrepareRequest in your webhook field.

    Hope it helps,
    Guillame

     

    Last edit: Guillaume Lajarige 2024-02-27
  • christ

    christ - 2024-02-27

    Thank you very much, Guillame.
    I'll try your suggestion later.
    Thanks 。

     
  • christ

    christ - 2024-02-28

    Thank you, Guillaume.
    I tried using your method \CallbacksClass::PrepareRequest, and it allowed me to display a JSON on the property page response. However, another question has arisen. How can I pass this obtained JSON into \CallbacksClass::ProcessResponse for further processing? I've tried many methods, such as outputting $oObject, $oResponse, $oAction to log or other files, but I haven't been able to get this JSON. How can I obtain this JSON to be referenced elsewhere?
    Could you please help me with this?

     
  • Guillaume Lajarige

    Can you share screenshots of what you "property page response"?
    Also can you share how you configured the webhook action and the PHP script (XML like the one I proposed, something else, ...) that you are using ?

     
    • christ

      christ - 2024-02-29

      I've established a basic extension demo and proceeded with a standard installation of this extension. I populated your XML demo within the "datamodel.cztech-extension.xml" file. Subsequently, I created an "iTop webservices call", as depicted in Image 1, and retrieved the data JSON for one server. However, I'm unsure how to transmit this JSON into the CallbacksClass::ProcessResponse function, as depicted in Image 2. I've attempted to inspect $oObject, $oResponse, and $oAction, but none of these variables contained the JSON data.

       
    • Guillaume Lajarige

      In the example I gave you, the response should be output in the iTop error.log, did you check if there was anything in it?

       
      • christ

        christ - 2024-02-29

        Indeed, I have obtained all the content related to the Server Object, as depicted in Figure 4. Nevertheless, I am solely interested in retrieving this JSON, illustrated in Figure 3. How can I extract this JSON? Which variable's value does it correspond to?

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.