Jump to content
Sign in to follow this  
pKris

Notify customer when ticket is set to a particular status

Recommended Posts

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 by kmg7513

Share this post


Link to post
Share on other sites

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');

 

Share this post


Link to post
Share on other sites

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');

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 .

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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');

 

Share this post


Link to post
Share on other sites

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>';

 

Share this post


Link to post
Share on other sites

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');

 

Share this post


Link to post
Share on other sites

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

});

 

Share this post


Link to post
Share on other sites

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());

});

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Screen Shot 2019-03-20 at 9.14.22 AM.png

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

Hooks have a tendency to break without any indication like that.  PHP error log may have shown that error also. 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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

});

Share this post


Link to post
Share on other sites

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, );

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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.

Sign in to follow this  

  • 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