pKris Posted December 14, 2018 Share Posted December 14, 2018 (edited) Hey there Community! We've got a handful of custom ticket statuses that I want to be able to send email templates for, such as: Notify customer when ticket is set to status "Under investigation" Notify customer when ticket is set to status "On hold" I'd love your input on how I can make this possible, with hooks or otherwise! Edited December 14, 2018 by kmg7513 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted December 15, 2018 Share Posted December 15, 2018 Use TicketStatusChange hook point to trigger a SendEmail API command. Something like follows: <?php use WHMCS\Database\Capsule; function prefix_TicketStatusChange($vars) { $userID = Capsule::table('tbltickets')->where('id', $vars['ticketid'])->first(['userid'])->userid; $postData = array( 'id' => $userID, 'customtype' => 'general', 'customsubject' => 'Stop opening tickets!', 'custommessage' => 'Right now I\'m playing League of Legend therefore I can\'t reply.<br><br>Your ticket status has been changed to <strong>' .$vars['status'] . '</strong>' ); $results = localAPI('SendEmail', $postData); } add_hook('TicketStatusChange', 1, 'prefix_TicketStatusChange'); 0 Quote Link to comment Share on other sites More sharing options...
pKris Posted December 20, 2018 Author Share Posted December 20, 2018 If I wanted to update the ticket with a reply so it's logged in the ticket rather than just emailed to them, would this work? <?php use WHMCS\Database\Capsule; function prefix_TicketStatusChange($vars) { $command = 'AddTicketReply'; $postData = array( 'ticketid' => '['ticketid'])', 'message' => 'Hey there,<br><br>Your ticket has been assigned to one of our technical team members and is currently under review, they will reply back here shortly with an update or to request more information about your issue., 'clientid' => '(['userid'])', 'useMarkdown' => true, ); $adminUsername = 'api_user'; // Optional for WHMCS 7.2 and later $results = localAPI($command, $postData, $adminUsername); print_r($results); } add_hook('TicketStatusChange', 1, 'prefix_TicketStatusChange'); 0 Quote Link to comment Share on other sites More sharing options...
kgerm7513 Posted February 21, 2019 Share Posted February 21, 2019 Hey there just wanted to bump this - I'd like the reply to be logged into the ticket and not just sent as an email directly to the customer, much like an Escalation Reply works. 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted February 21, 2019 Share Posted February 21, 2019 You're close, the clientid and ticket id need to get the values from $var and currently you are basically giving it an array with one element called "clientid" and "ticketid". Try this one out: <?php use WHMCS\Database\Capsule; add_hook('TicketStatusChange', 1, function($vars) { if ($vars['status'] === "Under investigation") { $Message = "Hey there,<br><br>Your ticket has been assigned to one of our technical team members and is currently under review, they will reply back here shortly with an update or to request more information about your issue."; } elseif ($vars['status'] === "On Hold") { $Message = "Hello, your ticket has been changed to ".$vars['status']." status."; } if (isset($Message) and $Message) { //We have a message, so add it as a reply $command = 'AddTicketReply'; $postData = array( 'ticketid' => $vars['ticketid'], 'message' => $Message, 'name'=>'TicketBot' 'useMarkdown' => true, ); $results = localAPI($command, $postData); } }); Note I added a status check as this would fire when closing a ticket and so I doubt you want to send the message at that point. Also it appears using the clientid in the addticketreply makes the reply show as coming from the client and IMO it should show as a system / admin reply instead and so do not give it . 0 Quote Link to comment Share on other sites More sharing options...
pKris Posted March 19, 2019 Author Share Posted March 19, 2019 I'm AWFUL at replying, sorry. It looks like that hasn't worked, sadly. I'm seeing no ticket reply (or outbound notification) when I add this to hooks/ticketstatuschange.php 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted March 19, 2019 Share Posted March 19, 2019 On TicketStatusChange this hook sends the email and add a reply to ticket. <? use WHMCS\Database\Capsule; function prefix_TicketStatusChange($vars) { $adminUsername = 'admin'; // Change me accordingly. This Admin user is the one used to automatically add replies to tickets when status is updated. Set false if you want to open the ticket using your own customers $userID = Capsule::table('tbltickets')->where('id', $vars['ticketid'])->first(['userid'])->userid; // Send email notification to customer when ticket status changes $EmailData = array( 'id' => $userID, 'customtype' => 'general', 'customsubject' => 'Stop opening tickets!', 'custommessage' => 'Right now I\'m playing League of Legend therefore I can\'t reply.<br><br>Your ticket status has been changed to <strong>' .$vars['status'] . '</strong>' ); localAPI('SendEmail', $EmailData); // Add reply to ticket when its status changes $TicketData = array( 'ticketid' => $vars['ticketid'], 'message' => 'Stop opening tickets!', 'clientid' => $userID, 'adminusername' => $adminUsername, ); localAPI('AddTicketReply', $TicketData); } add_hook('TicketStatusChange', 1, 'prefix_TicketStatusChange'); Here's the result. 1 Quote Link to comment Share on other sites More sharing options...
pKris Posted March 19, 2019 Author Share Posted March 19, 2019 Thank you Kian, I've added this to TicketStatusChange.php in includes/hooks/ and still nothing - there is no reply in the ticket. <? use WHMCS\Database\Capsule; function prefix_TicketStatusChange($vars) { $adminUsername = 'admin'; // Change me accordingly. This Admin user is the one used to automatically add replies to tickets when status is updated. Set false if you want to open the ticket using your own customers $userID = Capsule::table('tbltickets')->where('id', $vars['ticketid'])->first(['userid'])->userid; // Send email notification to customer when ticket status changes $EmailData = array( 'id' => $userID, 'customtype' => 'general', 'customsubject' => '', 'custommessage' => 'Hey there,<br><br>Your ticket has been assigned to one of our technical team members and is currently under review, they will reply back here shortly with an update or to request more information about your issue.' .$vars['status'] . '</strong>' ); localAPI('SendEmail', $EmailData); // Add reply to ticket when its status changes $TicketData = array( 'ticketid' => $vars['ticketid'], 'message' => 'Hey there,<br><br>Your ticket has been assigned to one of our technical team members and is currently under review, they will reply back here shortly with an update or to request more information about your issue.' .$vars['status'] . '</strong>', 'clientid' => $userID, 'adminusername' => $adminUsername, ); localAPI('AddTicketReply', $TicketData); } add_hook('TicketStatusChange', 1, 'prefix_TicketStatusChange'); 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted March 19, 2019 Share Posted March 19, 2019 Print both localAPI requests. Open a ticket and view it as administrator from backend then open Options tab and change ticket status. Press Save Changes. You should see the result of print on screen. You can print in this way... $results = localAPI('SendEMail', $EmailData); echo '<pre>'; print_r($results); echo '</pre>'; // and... $results = localAPI('AddTicketReply', $TicketData); echo '<pre>'; print_r($results); echo '</pre>'; 0 Quote Link to comment Share on other sites More sharing options...
pKris Posted March 19, 2019 Author Share Posted March 19, 2019 I must be losing it - I see nothing when I add this to my TicketStatusChange.php file - it doesn't print anything: <? use WHMCS\Database\Capsule; function prefix_TicketStatusChange($vars) { $adminUsername = 'myusername'; // Change me accordingly. This Admin user is the one used to automatically add replies to tickets when status is updated. Set false if you want to open the ticket using your own customers $userID = Capsule::table('tbltickets')->where('id', $vars['ticketid'])->first(['userid'])->userid; // Send email notification to customer when ticket status changes $EmailData = array( 'id' => $userID, 'customtype' => 'general', 'customsubject' => '', 'custommessage' => 'Hey there,<br><br>Your ticket has been assigned to one of our technical team members and is currently under review, they will reply back here shortly with an update or to request more information about your issue.' .$vars['status'] . '</strong>' ); localAPI('SendEmail', $EmailData); echo '<pre>'; print_r($results); echo '</pre>'; // Add reply to ticket when its status changes $TicketData = array( 'ticketid' => $vars['ticketid'], 'message' => 'Hey there,<br><br>Your ticket has been assigned to one of our technical team members and is currently under review, they will reply back here shortly with an update or to request more information about your issue.' .$vars['status'] . '</strong>', 'clientid' => $userID, 'adminusername' => $adminUsername, ); localAPI('AddTicketReply', $TicketData); echo '<pre>'; print_r($results); echo '</pre>'; } $results = localAPI('SendEMail', $EmailData); add_hook('TicketStatusChange', 1, 'prefix_TicketStatusChange'); 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 19, 2019 Share Posted March 19, 2019 And just to confirm, you have uploaded that to whmcs_root/includes/hooks ? 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 19, 2019 Share Posted March 19, 2019 I have tested the following hook and it works. <?php use WHMCS\Database\Capsule; add_hook('TicketStatusChange', 1, function($vars) { $Ticket = localAPI('GetTicket', array('ticketid'=>$vars['ticketid'])); if ($Ticket['result'] == "success") { // We got ticket, now get department for email to use for reply try { $Department = Capsule::table('tblticketdepartments')->where( array('id'=>$Ticket['deptid']))->first(); } catch (Exception $e) { // Failed to get department, should not happen but oh well return false; } if ($Department) { $Email = $Department->email; if ($vars['status'] == "In Progress") { $Message = "Hey there,<br><br>Your ticket has been assigned to one of our technical team members and is currently under review, they will reply back here shortly with an update or to request more information about your issue."; } elseif ($vars['status'] == "On Hold") { $Message = "Hello, your ticket has been changed to ".$vars['status']." status."; } if (isset($Message) and $Message) { //We have a message, so add it as a reply $command = 'AddTicketReply'; $postData = array( 'ticketid' => $vars['ticketid'], 'message' => $Message, 'name'=>'TicketBot', 'email'=>$Email, 'useMarkdown' => true, ); $results = localAPI($command, $postData); } // IF good message } // if department } // if get ticket was success }); 0 Quote Link to comment Share on other sites More sharing options...
pKris Posted March 19, 2019 Author Share Posted March 19, 2019 I must be screwing something up then - I’ll take a look at what I’ve done. 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 19, 2019 Share Posted March 19, 2019 Odd, try this basic one that just logs to the module log (Utiltiles -> Log -> Module log -- enable logging if it says it is not ) . Again it should be going in to whmcs root -> includes -> hooks with name of "hook_ticketstatuchange.php" (or any other name as long as it ends in .php) Also, may have missed it but what version of WHMCS are you using? <?php add_hook('TicketStatusChange', 1, function($vars) { logModuleCall("hook", "TicketStatusChange", $vars , print_r($vars,true), "", array()); }); 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted March 20, 2019 Share Posted March 20, 2019 13 hours ago, steven99 said: Also, may have missed it but what version of WHMCS are you using? always a good question to remember to ask.... i've forgotten to ask it often too! 🙂 generally, if the OP is a newbie (eg registered in this place this year), you can assume it's a very recent release (unless told otherwise)... for someone here from 2015, all bets are off and it could be any version they're using... as an example, both of the API functions you're calling have each been updated a handful of times during the v7 lifetime. 0 Quote Link to comment Share on other sites More sharing options...
pKris Posted March 20, 2019 Author Share Posted March 20, 2019 1 hour ago, brian! said: always a good question to remember to ask.... i've forgotten to ask it often too! 🙂 generally, if the OP is a newbie (eg registered in this place this year), you can assume it's a very recent release (unless told otherwise)... for someone here from 2015, all bets are off and it could be any version they're using... as an example, both of the API functions you're calling have each been updated a handful of times during the v7 lifetime. A good assumption @brian!!, I'm on 7.7.1. @steven99 here is a screenshot of the module debug log for this. When I proved that the previous test hook appeared in the module debug log, I retried the original I had that I posted above and it did not show in the debug log as having run. 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted March 20, 2019 Share Posted March 20, 2019 Maybe it's due to PHP shot tags. In my hook I forgot to use <?php instead of <?. Update the hook at let us know if it works. 0 Quote Link to comment Share on other sites More sharing options...
pKris Posted March 20, 2019 Author Share Posted March 20, 2019 Given the control characters in the [status] segment I took a shot in the dark and removed the apostrophe from my status (was 'We're investigating', changed to 'Under investigation'), and that seems to have been the reason for the breakage. Thank you all for your help! 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 20, 2019 Share Posted March 20, 2019 Hooks have a tendency to break without any indication like that. PHP error log may have shown that error also. 0 Quote Link to comment Share on other sites More sharing options...
pKris Posted March 20, 2019 Author Share Posted March 20, 2019 One more question here - any time I set the ticket status now it changes the status back to "Customer Reply" because the reply comes from the customer. Is there a tag to make the reply from the system, or at least the current admin user? 0 Quote Link to comment Share on other sites More sharing options...
steven99 Posted March 20, 2019 Share Posted March 20, 2019 Had not noticed that behavior. In the addticketreply API call, add in: "status" => $params['status']. See below for updated code of the above hook. You could also use "adminusername" and set that to a general admin / ticketbot admin though may still have to set status. <?php use WHMCS\Database\Capsule; add_hook('TicketStatusChange', 1, function($vars) { $Ticket = localAPI('GetTicket', array('ticketid'=>$vars['ticketid'])); if ($Ticket['result'] == "success") { // We got ticket, now get department for email to use for reply try { $Department = Capsule::table('tblticketdepartments')->where( array('id'=>$Ticket['deptid']))->first(); } catch (Exception $e) { // Failed to get department, should not happen but oh well return false; } if ($Department) { $Email = $Department->email; if ($vars['status'] == "In Progress") { $Message = "Hey there,<br><br>Your ticket has been assigned to one of our technical team members and is currently under review, they will reply back here shortly with an update or to request more information about your issue."; } elseif ($vars['status'] == "On Hold") { $Message = "Hello, your ticket has been changed to ".$vars['status']." status."; } if (isset($Message) and $Message) { //We have a message, so add it as a reply $command = 'AddTicketReply'; $postData = array( 'ticketid' => $vars['ticketid'], 'message' => $Message, 'name'=>'TicketBot', 'email'=>$Email, 'useMarkdown' => true, 'status'=>$vars['status']); $results = localAPI($command, $postData); } // IF good message } // if department } // if get ticket was success }); 0 Quote Link to comment Share on other sites More sharing options...
pKris Posted March 20, 2019 Author Share Posted March 20, 2019 Hey there @steven99 that's correct yes the status changing to "Answered" is still an issue; however, after changing this line it's working the way I expect it to: //We have a message, so add it as a reply $command = 'AddTicketReply'; $postData = array( 'ticketid' => $vars['ticketid'], 'message' => $Message, 'status' => 'Under investigation', 'name'=>'TicketBot', 'email'=>$Email, 'useMarkdown' => true, ); 0 Quote Link to comment Share on other sites More sharing options...
hoya8 Posted February 18, 2021 Share Posted February 18, 2021 Hello We are on WHMCS 8.1 is there any way to let us notify clients about "ticket status change" with a email subject with ticket ID on it? It looked like none of above could achieve this purpose, thanks 0 Quote Link to comment Share on other sites More sharing options...
hoya8 Posted February 19, 2021 Share Posted February 19, 2021 On 3/19/2019 at 6:12 PM, Kian said: On TicketStatusChange this hook sends the email and add a reply to ticket. <? use WHMCS\Database\Capsule; function prefix_TicketStatusChange($vars) { $adminUsername = 'admin'; // Change me accordingly. This Admin user is the one used to automatically add replies to tickets when status is updated. Set false if you want to open the ticket using your own customers $userID = Capsule::table('tbltickets')->where('id', $vars['ticketid'])->first(['userid'])->userid; // Send email notification to customer when ticket status changes $EmailData = array( 'id' => $userID, 'customtype' => 'general', 'customsubject' => 'Stop opening tickets!', 'custommessage' => 'Right now I\'m playing League of Legend therefore I can\'t reply.<br><br>Your ticket status has been changed to <strong>' .$vars['status'] . '</strong>' ); localAPI('SendEmail', $EmailData); // Add reply to ticket when its status changes $TicketData = array( 'ticketid' => $vars['ticketid'], 'message' => 'Stop opening tickets!', 'clientid' => $userID, 'adminusername' => $adminUsername, ); localAPI('AddTicketReply', $TicketData); } add_hook('TicketStatusChange', 1, 'prefix_TicketStatusChange'); Here's the result. Hi could I know that how to notify clients "only if" status is changed to "in progress" based on your script? We want to email clients about this "in progress "change only. Currently this script email clients about "replied" too which WHMCS has it already thanks again 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.