Jump to content

New To Hooks - TicketAdminReply


vmhosts_ge

Recommended Posts

Afternoon All,

First time poster here so apologies in advance if I miss anything.

 

I have a requirement where I'd like other admin users to be notified when an admin of a department sends a reply to the customer. After speaking with WHMCS support they've pointed me in the direction of the following article https://developers.whmcs.com/hooks-reference/ticket/#ticketadminreply

 

From what I understand, I would need to write a function using the sendadminemail API https://developers.whmcs.com/api-reference/sendadminemail/ and put that function into the hook above.

 

I have a couple of questions, the first is, what file would you place the code in to execute this?

 

I'm fairly rusty / not great at coding, I've had a go at writing what I would need based on the linked documentation but not sure if what I have is a good start, also not sure on what code would be required to pull the admin email addresses that are assigned to a specific department. So my second question is would you mind taking a look and helping me along the way?

 

function Support_Admin_Reply_Notification_Function ($vars) {
$command = 'SendAdminEmail';
$AdminUsers = get-adminUsers | where $vars['deptid'] ==;  //No idea on this part
$values ['mergefields'] = array(
'messagename' => 'Admin Ticket Reply',
'ticket_id' => $vars['ticketid'],
'ticket_department' => $vars['deptname'],
'ticket_subject' => $vars['subject'],
'ticket_priority' => $vars['priority'],
'ticket_message' => nl2br($vars['message']),
'type' => 'support',
'deptid' => $vars['deptid'])


foreach ($AdminUser in $AdminUsers) {
$results = localAPI($command,$values,$AdminUser);
logActivity($AdminUser . ' attempted to notify admins of reply to ticket #'.$vars['ticketid'].' ');
}

}

add_hook(TicketAdminReply',1,'Support_Admin_Reply_Notification_Function');

 

Thank you in advance

Gary

Link to comment
Share on other sites

You need to create an addon module and then enable that addon. There is an article here but let me give you a quick run down..

 

Your addon needs a name! Lets call it sarn ("Support Admin Reply Notifications")

 

The structure for an addon module is like this:

 

modules/addons/MODULE_NAME/MODULE_NAME.php -- main module file

modules/addons/MODULE_NAME/hooks.php -- hooks file

 

All the functions for your module are prefixed with MODULE_NAME, and at minimum your main module file needs the _config() function. This would be called MODULE_NAME_config()

 

So if we are calling it sarn your structure changes to this:

 

modules/addons/sarn/sarn.php -- main module file

modules/addons/sarn/hooks.php -- hooks file

 

The _config() function is to give WHMCS some information about your addon, you return a small array. Here is an example:

 

<?php

sarn_config() {
   $info = [
       "name" => "SARN",
       "description" => "Providing awesome notifications!",
       "version" => "1.0",
       "author" => "markhughes"
   ];
   return $info;
}

 

There are more functions you can put in this file if you want to read up on that. But since you only want to use hooks, we can move to the hooks file.

 

You can add exactly what you've written into the hooks file.

 

Then you can go ahead and enable the module in WHMCS. PROTIP: If you ever make changes to the hooks file you will have to disable and then reenable the module - as it is cached.

 

Good luck! Hopefully that makes sense.

Link to comment
Share on other sites

Hi Mark,

Thanks for the detailed info there, that's really helpful and a great place for me to start.

 

One part I can see myself getting stuck on however, is the query to store all admin users for the department the ticket is in, ready to send out emails to.

 

I'm not sure if I need a conditional statement to filter the admin users if they're assigned to the department the ticket is in or not?

 

I'm not even sure on what the correct syntax is to query admin users within WHMCS.

 

Any ideas?

 

Thanks in advance :)

Link to comment
Share on other sites

So WHMCS is built on Laravel (I think? Partially? or at least it creates the models on eloquent and uses capsule through it..) and has a bunch of classes we can use that are built on it.

 

These things aren't documented very well (like a lot of things in the dev of WHMCS). But this is the best way I can think of to do this:

 

// I think we could even filter this somehow using eloquent, but this is how you get them all! 
foreach (\WHMCS\User\Admin::all() as $admin) {
if (in_array($vars["deptid"], $admin->supportDepartmentIds)) {
	// Woah! lets send an email or something:-D 
	print_r($admin); // this will be gross, don't do it :'(

	$id = $admin->id; // all columns are here now 
}
}

 

Then you can make your API call (or call internal methods - but this

 

- - - Updated - - -

 

Half my message got cut off:

 

(or call internal methods - but this is getting a bit complex)

Link to comment
Share on other sites

Hi Mark,

Thanks for this, what I have so far is:

 

hooks.php

function Support_Admin_Reply_Notification_Function ($vars) {
$command = 'SendAdminEmail';
$values ['mergefields'] = array(
'messagename' => 'Admin Ticket Reply',
'ticket_id' => $vars['ticketid'],
'ticket_department' => $vars['deptname'],
'ticket_subject' => $vars['subject'],
'ticket_priority' => $vars['priority'],
'ticket_message' => nl2br($vars['message']),
'type' => 'support',
'deptid' => $vars['deptid'])

// I think we could even filter this somehow using eloquent, but this is how you get them all! 
foreach (\WHMCS\User\Admin::all() as $admin) { 
   if (in_array($vars["deptid"], $admin->supportDepartmentIds)) { 
       // Woah! lets send an email or something:-D  
       $results = localAPI($command,$values,$AdminUser);
       logActivity($AdminUser . ' attempted to notify admins of reply to ticket #'.$vars['ticketid'].' '); 

       $id = $admin->id; // all columns are here now 
   } 
}

}

add_hook(TicketAdminReply',1,'Support_Admin_Reply_Notification_Function');

 

AdminNotifications.php

<?php

AdminNotifications_config() {
   $info = [
       "name" => "Admin Notifications",
       "description" => "Send Notifications to other admins when an Admin replies to a support ticket",
       "version" => "1.0",
       "author" => "Gary"
   ];
   return $info;
}

 

I noticed that at the befinning of the main module file you opened with <?php do I therefore need to close that somewhere within the module?

Link to comment
Share on other sites

No worries!

 

So with PHP I always start with my opening tag <?php on my first line. It's not required at the end of a document though, I think its best just to drop it unless you want to insert some html.

 

Just some notes:

 

After 'deptid' => $vars['deptid']) you forgot your end semicolon

 

Your logActivity call is going to be called multiple times. Did you only want it called once? I would put it outside the loop.

 

The first variable used in your add_hook is missing its first quote.

 

Do you have $AdminUser defined for your localAPI function? I usually create a seperate WHMCS account called "api" and put it in it's own role called "API Only" with all it's permission unticked except for "API Access" (this permission grants all privileges on the API). I would not use a user from the loop to access the API - they probably won't have API Access (and shouldn't, for security purposes)

 

BUT that API method will send an email to ALL admins. You will have to use the internal function sendAdminMessage for this.

 

function Support_Admin_Reply_Notification_Function($vars) {
   $mergefields = [
       'messagename' => 'Admin Ticket Reply',
       'ticket_id' => $vars['ticketid'],
       'ticket_department' => $vars['deptname'],
       'ticket_subject' => $vars['subject'],
       'ticket_priority' => $vars['priority'],
       'ticket_message' => nl2br($vars['message']),
       'type' => 'support',
       'deptid' => $vars['deptid']
   ];

   foreach (\WHMCS\User\Admin::all() as $admin) { 
       if (in_array($vars["deptid"], $admin->supportDepartmentIds)) { 
           sendAdminMessage("Admin Ticket Reply", $mergefields, null, $admin->id);
       } 
   }
}

 

You can find the current admin username that is send the ticket in $vars["admin"]

 

Because this is a support email they must have support emails enabled. :)

 

If you're not a developer I'm happy to package this into a module for you.

Link to comment
Share on other sites

Just a quick heads up here.

You don't need to create an addon for this. In fact, since you probably aren't going to look for any admin or user output, you probably don't want to.

Take your hook code, put it in a php file inside of whmcs/includes/hooks

 

An addon should only be used if you need to tweak the module config, need output on either admin, or user side.

Link to comment
Share on other sites

  • 2 weeks later...

I prefer putting things into modules than the hooks folder personally, seems more organised IMO! But then again I guess I tend to organise all my modules a little oddly too :P

 

If you wanna add my on Skype or something just so you can test it quickly when I'm done? Won't charge you for it, just a better communication method :)

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