Jump to content

Webhook setup for New ticket generate in WHMCS


Go to solution Solved by simranjeet,

Recommended Posts

I have a question regarding setting up a webhook in WHMCS for ticket creation. Could you please guide me on how to configure a webhook in WHMCS to trigger when a ticket is opened?

If possible, could you also share some screenshots to illustrate the setup process?

Link to comment
Share on other sites

I've already looked into this, but I want to confirm something. Is there an option in WHMCS to directly set up a webhook URL without using a hook?
Specifically, is there a setting where I can configure the webhook URL for the "open ticket" event?

Thanks

Link to comment
Share on other sites

  • Solution

I’ve created the file as per the instructions in the document you mentioned and added the hook for the open ticket event. However, when I trigger the hook, it's not working, and I’m not receiving any response at the webhook URL I set up.

When I try to access this file directly through the browser, I get the error: "This file cannot be accessed directly."

What does this error mean, and how can I resolve it?
I’m using this script—could you please review the screenshot and let me know what I might be doing wrong?

Screenshot_1.png

Link to comment
Share on other sites

The point of hooks is to hook onto an event - ticket creation for example. If you want to access the file directly, remove lines 3-5 - but accessing the file won't help you since it needs to have data passed to it.

Glancing over your code, it should work. Data is being sent to the get_hook_data.php script.

Link to comment
Share on other sites

Yes, I understand that direct access isn’t possible. The hook should trigger when a ticket is opened. However, when I created an open ticket in my WHMCS system, the hook didn’t trigger, and I didn’t receive any response at my webhook URL. Here is the code in my webhook URL:

<?php
$data = file_get_contents('php://input');
file_put_contents('open.json', $data);
?>

The hook should automatically trigger when a ticket is created, but it hasn’t worked, and no response is recorded in my webhook file. I’ve also tried removing a few lines of code as suggested, but it’s still not working. Additionally, I added this line for debugging:

file_put_contents('hook_debug.log', print_r($vars, true), FILE_APPEND);

but the hook_debug.log file isn’t being created either. 

Link to comment
Share on other sites

An easy way to see if it's working is doing something like:

logActivity("Test hook: $subject")

right after defining the $subject variable. After triggering the hook, look at the activity log in WHMCS.
As long as the .php file is placed inside /includes/hooks/ and has valid syntax, it should work just fine.

Edited by DennisHermannsen
Link to comment
Share on other sites

13 minutes ago, simranjeet said:

However, when I created an open ticket in my WHMCS system, the hook didn’t trigger, and I didn’t receive any response at my webhook URL. Here is the code in my webhook URL:

Do you create the ticket through Client Area or Admin Area ?

Edited by pRieStaKos
Link to comment
Share on other sites

Additionally, when I run my hook file in the browser, I encounter the following error:->[22-Aug-2024 12:24:05 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function add_hook() in /home/mydomain/public_html/clients/includes/hooks/open_ticket.php:3
Stack trace:
#0 {main}
  thrown in /home/nextsulting/public_html/clients/includes/hooks/open_ticket.php on line 3

Link to comment
Share on other sites

 

Just to make this clear again: Don't try to directly access a WHMCS hook. It's not how it suppose to work.

Now, I'm attaching a hook and get_hook_data that I can confirm work:

<?php

add_hook('TicketOpen', 1, function ($vars) {
  try {
    // Send ticket data to webhook
    $webhook_url = ".../get_hook_data.php";

    // Set post data
    $data = [
      "ticketid" => $vars["ticketid"],
      "ticketmask" => $vars["ticketmask"],
      "userid" => $vars["userid"],
      "deptid" => $vars["deptid"],
      "deptname" => $vars["deptname"],
      "subject" => $vars["subject"],
      "message" => $vars["message"],
      "priority" => $vars["priority"],
    ];

    $ch = curl_init($webhook_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
      'Content-Type: application/json'
    ]);

    $response = curl_exec($ch);
    $data = json_decode($response, true);

    if ($response) {
      logActivity('[TicketOpen] Response: ' . $response);
    }
    curl_close($ch);

  } catch (Exception $e) {
      // If error, write to Activity log
      logActivity('[TicketOpen] Error: ' . $e->getMessage());
  }
});

 

 

<?php

// Receive data from curl
$data = json_decode(file_get_contents('php://input'), true);

// Check if data is empty
if (empty($data)) {
  // Return an error response
  $response = [
    'status' => 'error',
    'message' => 'No data received',
  ];

  header('Content-Type: application/json');
  echo json_encode($response);
}

// Process the received data
$vars = file_get_contents('php://input');
file_put_contents('vars.json', $vars);

// Send a success response
$response = [
  'status' => 'success',
  'message' => 'Data received successfully',
];

header('Content-Type: application/json');
echo json_encode($response);

Adjust get_hook_data to your needs.....You must get the point.

Edited by pRieStaKos
Link to comment
Share on other sites

1 hour ago, pRieStaKos said:

Just to make this clear again: Don't try to directly access a WHMCS hook. It's not how it suppose to work.

Accessing the file directly shouldn't cause a fatal error.
By the looks of it, it could seem like the hook file is somehow not registered in WHMCS. If it was, it would know of the add_hook() function.

Nevermind, of course it should. WHMCS isn't booted up when accessing the file directly.

By the looks of it, you haven't configured WHMCS correctly. When I try to access your WHMCS installation, I'm told there was a problem establishing a database connection.

Edited by DennisHermannsen
Link to comment
Share on other sites

I'm still facing the same issue. It seems to be related to my server. Whenever I create a file on my server, it only works the first time I run it, but then it stops working. Do you have any idea why this might be happening? For example, if I create a new file with just echo "hello"; and run it in the browser, it displays "hello" as expected. But if I change the code to something like echo "text"; and run the file again, it still displays the original "hello" message instead of the updated "text".

Link to comment
Share on other sites

I don't think it's a cache issue because I've already cleared the cache from my browser. The problem is that the file only runs once, and if I try to run it again, it doesn't work. However, if I rename the file and run it, it starts working again

Link to comment
Share on other sites

Hi,

The cache issue has been resolved, and I can now access a file multiple times without any problems. I’ve created a file inside the [includes/hooks] directory to capture the data of a ticket created in WHMCS. However, despite using the same code, I’m not receiving any data for the open ticket. I’ve tried creating a ticket using the API as well as manually in WHMCS, but I still don’t get any data.

I'll share some screenshots with you so you can see exactly what I'm trying to accomplish. If you think I'm making a mistake, please let me know and guide me on how to proceed.

Screenshot_11.png

Screenshot_12.png

Screenshot_13.png

Link to comment
Share on other sites

TicketOpen and TicketOpenAdmin is not triggered for tickets created by API. There is no hook index for this

Please read the hook documentation and set the proper hook index for the action you are making.

Try opening a ticket through client and admin area and check if hook is working.

 

Link to comment
Share on other sites

I have already attempted to create tickets through both the client and admin interfaces, but it’s not working. Additionally, creating a ticket via the API was only for testing purposes.

I placed my file in the directory specified in the documentation: includes/hooks/open_ticket.php. I have provided the code from this file earlier, but I’m still encountering the same issue.

I’m unsure what might be causing the problem or what steps I need to take to resolve it, as I haven't received any errors to help diagnose the issue.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated