Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

osTicket v1.10 (stable) and Maintenance Release v1.9.15 are now available! Go get it now

SMS notification

JjSJjS
edited October 2013 in Mods and Customizations
I have added SMS notification on ticket creation, assignment and overdue, sent along with the e-mail. Additionally I've added SMS notification priority variable to the staff profile, so there is a control which notifications are sent out. There is a switch (drop down list) in staff member form accessible form the Admin panel, so None, Low, Normal, High or Emergency priority tickets are subject to SMS notification.

Step by step instruction:
1. Add column to staff table in the database (I used myPHP to do it).
Column inserted after mobile, name SMS_priority, INT (10) unsigned, default value 4


2. Modify file: class.staff.php
Add function

function getMobile() {
return $this->ht['mobile'];
}
function getSMSPriority () {
return $this->ht['SMS_priority'];
}

Alter function

function updateProfile($vars, &$errors)
Add SQL field:
$sql='UPDATE '.STAFF_TABLE.' SET updated=NOW() '
.' ,firstname='.db_input($vars['firstname'])
.' ,lastname='.db_input($vars['lastname'])
.' ,email='.db_input($vars['email'])
.' ,phone=\"'.db_input(Format::phone($vars['phone']),false).'\"'
.' ,phone_ext='.db_input($vars['phone_ext'])
.' ,mobile=\"'.db_input(Format::phone($vars['mobile']),false).'\"'
.' ,SMS_priority='.db_input($vars['SMS_priority']) //MOD
.' ,signature='.db_input($vars['signature'])
.' ,timezone_id='.db_input($vars['timezone_id'])
.' ,daylight_saving='.db_input(isset($vars['daylight_saving'])?1:0)
.' ,show_assigned_tickets='.db_input(isset($vars['show_assigned_tickets'])?1:0)
.' ,max_page_size='.db_input($vars['max_page_size'])
.' ,auto_refresh_rate='.db_input($vars['auto_refresh_rate'])
.' ,default_signature_type='.db_input($vars['default_signature_type'])
.' ,default_paper_size='.db_input($vars['default_paper_size']);

Alter function

function save
$sql='SET updated=NOW() '
.' ,isadmin='.db_input($vars['isadmin'])
.' ,isactive='.db_input($vars['isactive'])
.' ,isvisible='.db_input(isset($vars['isvisible'])?1:0)
.' ,onvacation='.db_input(isset($vars['onvacation'])?1:0)
.' ,assigned_only='.db_input(isset($vars['assigned_only'])?1:0)
.' ,dept_id='.db_input($vars['dept_id'])
.' ,group_id='.db_input($vars['group_id'])
.' ,timezone_id='.db_input($vars['timezone_id'])
.' ,daylight_saving='.db_input(isset($vars['daylight_saving'])?1:0)
.' ,username='.db_input($vars['username'])
.' ,firstname='.db_input($vars['firstname'])
.' ,lastname='.db_input($vars['lastname'])
.' ,email='.db_input($vars['email'])
.' ,phone=\"'.db_input(Format::phone($vars['phone']),false).'\"'
.' ,phone_ext='.db_input($vars['phone_ext'])
.' ,mobile=\"'.db_input(Format::phone($vars['mobile']),false).'\"'
.' ,SMS_priority='.db_input($vars['SMS_priority']) //MOD
.' ,signature='.db_input($vars['signature'])
.' ,notes='.db_input($vars['notes']);


Modify file staff.inc.php by adding drop box to control SMS notification

<td width=\"180\">
Mobile Number:
</td>

<td>
<input type=\"text\" size=\"18\" name=\"mobile\" value=\"<?php echo $info['mobile']; ?>\">
&nbsp;<span class=\"error\">&nbsp;<?php echo $errors['mobile']; ?></span>
SMS notification from ticket priority <select name=\"SMS_priority\">
<?php
$priorities= db_query('SELECT priority_id,priority_desc FROM '.TICKET_PRIORITY_TABLE);
$id=5; // Higher than the highest used by the system
$tag='None';
do { ?>
<option value=\"<?php echo $id; ?>\"<?php echo ($info['SMS_priority']==$id)?'selected':''; ?>><?php echo $tag; ?></option>
<?php
} while (list($id,$tag) = db_fetch_row($priorities))?>
</select>
&nbsp;<span class=\"error\">*&nbsp;<?php echo $errors['SMS_priority'];?></span>

</td>

to be continued....

Comments

  • Part 2

    In file class.ticket.php
    Add include the sms sending module

    include_once(INCLUDE_DIR.'sms.php');


    Alter functions to send SMS along with e-mail notification:

    function onNewTicket($message, $autorespond=true, $alertstaff=true) {
    global $cfg;

    //Log stuff here...

    if(!$autorespond && !$alertstaff) return true; //No alerts to send.

    /* ------ SEND OUT NEW TICKET AUTORESP && ALERTS ----------*/

    $this->reload(); //get the new goodies.
    $dept= $this->getDept();

    if(!$dept || !($tpl = $dept->getTemplate()))
    $tpl= $cfg->getDefaultTemplate();

    if(!$tpl) return false; //bail out...missing stuff.

    if(!$dept || !($email=$dept->getAutoRespEmail()))
    $email =$cfg->getDefaultEmail();

    //Send auto response - if enabled.
    if($autorespond && $email && $cfg->autoRespONNewTicket()
    && $dept->autoRespONNewTicket()
    && ($msg=$tpl->getAutoRespMsgTemplate())) {

    $msg = $this->replaceVars($msg,
    array('message' => $message,
    'signature' => ($dept && $dept->isPublic())?$dept->getSignature():'')
    );

    if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()))
    $msg['body'] =\"\n$tag\n\n\".$msg['body'];

    $email->sendAutoReply($this->getEmail(), $msg['subj'], $msg['body']);
    }

    if(!($email=$cfg->getAlertEmail()))
    $email =$cfg->getDefaultEmail();
    //MOD
    //Compose SMS
    $smsMsg='New ticket->priority: '.$this->getPriority().' from '.$this->getName().' ('.$this->getEmail().')';
    $smsMsg.=' tel: '.$this->getPhone().' Due: '.$this->getSLADueDate();
    //ENDMOD

    //Send alert to out sleepy & idle staff.
    if($alertstaff && $email
    && $cfg->alertONNewTicket()
    && ($msg=$tpl->getNewTicketAlertMsgTemplate())) {

    $msg = $this->replaceVars($msg, array('message' => $message));

    $recipients=$sentlist=array();
    //Alert admin??
    if($cfg->alertAdminONNewTicket()) {
    $alert = str_replace('%{recipient}', 'Admin', $msg['body']);
    $email->sendAlert($cfg->getAdminEmail(), $msg['subj'], $alert);
    $sentlist[]=$cfg->getAdminEmail();
    }

    //Only alerts dept members if the ticket is NOT assigned.
    if($cfg->alertDeptMembersONNewTicket() && !$this->isAssigned()) {
    if(($members=$dept->getMembers()))
    $recipients=array_merge($recipients, $members);
    }

    if($cfg->alertDeptManagerONNewTicket() && $dept && ($manager=$dept->getManager()))
    $recipients[]= $manager;

    foreach( $recipients as $k=>$staff) {
    if(!is_object($staff) || !$staff->isAvailable() || in_array($staff->getEmail(), $sentlist)) continue;
    $alert = str_replace('%{recipient}', $staff->getFirstName(), $msg['body']);
    $email->sendAlert($staff->getEmail(), $msg['subj'], $alert);
    //MOD
    if ($staff->getSMSPriority() <= $this->getPriorityID()) {
    send_SMS($staff->getMobile(),$smsMsg);
    }
    //END MOD
    $sentlist[] = $staff->getEmail();
    }


    }

    return true;
    }



    function onAssign($assignee, $comments, $alert=true) {
    global $cfg, $thisstaff;

    if($this->isClosed()) $this->reopen(); //Assigned tickets must be open - otherwise why assign?

    //Assignee must be an object of type Staff or Team
    if(!$assignee || !is_object($assignee)) return false;

    $this->reload();

    $comments = $comments?$comments:'Ticket assignment';
    $assigner = $thisstaff?$thisstaff:'SYSTEM (Auto Assignment)';

    //Log an internal note - no alerts on the internal note.
    $this->logNote('Ticket Assigned to '.$assignee->getName(), $comments, $assigner, false);

    //See if we need to send alerts
    if(!$alert || !$cfg->alertONAssignment()) return true; //No alerts!

    $dept = $this->getDept();

    //Get template.
    if(!$dept || !($tpl = $dept->getTemplate()))
    $tpl = $cfg->getDefaultTemplate();

    //Email to use!
    if(!($email=$cfg->getAlertEmail()))
    $email = $cfg->getDefaultEmail();

    //recipients
    $recipients=array();
    if(!strcasecmp(get_class($assignee), 'Staff')) {
    if($cfg->alertStaffONAssignment())
    $recipients[] = $assignee;
    } elseif(!strcasecmp(get_class($assignee), 'Team')) {
    if($cfg->alertTeamMembersONAssignment() && ($members=$assignee->getMembers()))
    $recipients+=$members;
    elseif($cfg->alertTeamLeadONAssignment() && ($lead=$assignee->getTeamLead()))
    $recipients[] = $lead;
    }

    //Get the message template
    if($email && $recipients && $tpl && ($msg=$tpl->getAssignedAlertMsgTemplate())) {

    $msg = $this->replaceVars($msg,
    array('comments' => $comments,
    'assignee' => $assignee,
    'assigner' => $assigner
    ));
    //MOD
    //Compose SMS message body
    $smsMsg='New ticket assigned->priority: '.$this->getPriority().' from '.$this->getName().' ('.$this->getEmail().')';
    $smsMsg.=' tel: '.$this->getPhone().' Due: '.$this->getSLADueDate();
    // END MOD

    //Send the alerts.
    $sentlist=array();
    foreach( $recipients as $k=>$staff) {
    if(!is_object($staff) || !$staff->isAvailable() || in_array($staff->getEmail(), $sentlist)) continue;
    $alert = str_replace('%{recipient}', $staff->getFirstName(), $msg['body']);
    $email->sendAlert($staff->getEmail(), $msg['subj'], $alert);
    $sentlist[] = $staff->getEmail();
    //MOD - Send SMS if priority high enough
    if ($staff->getSMSPriority() <= $this->getPriorityID()) {
    send_SMS($staff->getMobile(),$smsMsg);
    }
    //END MOD
    }
    }

    return true;
    }

    function onOverdue($whine=true, $comments=\"\") {
    global $cfg;

    if($whine && ($sla=$this->getSLA()) && !$sla->alertOnOverdue())
    $whine = false;

    //check if we need to send alerts.
    if(!$whine || !$cfg->alertONOverdueTicket())
    return true;

    $dept = $this->getDept();
    //Get department-defined or default template.
    if(!$dept || !($tpl = $dept->getTemplate()))
    $tpl= $cfg->getDefaultTemplate();

    //Email to use!
    if(!($email=$cfg->getAlertEmail()))
    $email =$cfg->getDefaultEmail();

    //Get the message template
    if($tpl && ($msg=$tpl->getOverdueAlertMsgTemplate()) && $email) {

    $msg = $this->replaceVars($msg, array('comments' => $comments));
    // MOD
    // Compose SMS body

    $smsMsg='Ticket overdue!!! -> priority: '.$this->getPriority().' from '.$this->getName().' ('.$this->getEmail().')';
    $smsMsg.=' tel: '.$this->getPhone().' Due by: '.$this->getSLADueDate();
    // END MOD

    //recipients
    $recipients=array();
    //Assigned staff or team... if any
    if($this->isAssigned() && $cfg->alertAssignedONOverdueTicket()) {
    if($this->getStaffId())
    $recipients[]=$this->getStaff();
    elseif($this->getTeamId() && ($team=$this->getTeam()) && ($members=$team->getMembers()))
    $recipients=array_merge($recipients, $members);
    } elseif($cfg->alertDeptMembersONOverdueTicket() && !$this->isAssigned()) {
    //Only alerts dept members if the ticket is NOT assigned.
    if(($members=$dept->getMembers()))
    $recipients=array_merge($recipients, $members);
    }
    //Always alert dept manager??
    if($cfg->alertDeptManagerONOverdueTicket() && $dept && ($manager=$dept->getManager()))
    $recipients[]= $manager;

    $sentlist=array();
    foreach( $recipients as $k=>$staff) {
    if(!is_object($staff) || !$staff->isAvailable() || in_array($staff->getEmail(), $sentlist)) continue;
    $alert = str_replace(\"%{recipient}\", $staff->getFirstName(), $msg['body']);
    $email->sendAlert($staff->getEmail(), $msg['subj'], $alert);
    $sentlist[] = $staff->getEmail();
    //MOD - Send SMS if priority high enough
    if ($staff->getSMSPriority() <= $this->getPriorityID()) {
    send_SMS($staff->getMobile(),$smsMsg);
    }
    // END MOD
    }

    }

    return true;

    }


    Add sms.php, containing finction to send sms. Alter accordingly to your sms api

    <?php
    function send_SMS($to_num, $msg)
    {
    $params = array(
    'username' => 'login', //login z konta SMSAPI
    'password' => md5('********), //lub $password=\"ci&#261;g md5\"
    'to' => $to_num, //recipient
    'from' => 'Serwis', //sender
    'message' => $msg, //message body
    );
    if ($params['username'] && $params['password'] && $params['to'] && $params['message'])
    {
    $data = '?'.http_build_query($params);
    $plik = fopen('https://ssl.smsapi.pl/sms.do'.$data,'r'); $wynik = fread($plik,1024);
    fclose ($plik);
    return $wynik;
    }
    }
    ?>


    Still testing, but seems to work just fine. This is my first php programming exercise ;-)
  • Self service

    In order to enable user to set sms alert lever alter profile.inc.php:


    Mobile Number:
    </td>
    <td>
    <input type=\"text\" size=\"18\" name=\"mobile\" value=\"<?php echo $info['mobile']; ?>\">
    &nbsp;<span class=\"error\">&nbsp;<?php echo $errors['mobile']; ?></span>
    SMS notification from ticket priority <select name=\"SMS_priority\">
    <?php
    $priorities= db_query('SELECT priority_id,priority_desc FROM '.TICKET_PRIORITY_TABLE);
    $id=5; // Higher than the highest used by the system
    $tag='None';
    do { ?>
    <option value=\"<?php echo $id; ?>\"<?php echo ($info['SMS_priority']==$id)?'selected':''; ?>><?php echo $tag; ?></option>
    <?php
    } while (list($id,$tag) = db_fetch_row($priorities))?>
    </select>
    &nbsp;<span class=\"error\">*&nbsp;<?php echo $errors['SMS_priority'];?></span>

    </td>

  • Any help with this would be very much appreciated.

    Can anyone please give me the file and line number where new tickets are created? Note: I am assuming that there is a single point where new tickets are created (instantiated) from either web form or created by staff?

    The reason is that I would like to send an SMS message to the $phone with the ticket number - this is accomplished by calling another php script using $phone as an input parameter e.g. include 'sendSMS.php'; 

    Case 1 (user creates ticket via web form): You have created a new ticket # 123. 

    Case2 (staff member creates ticket for customer): A new ticket # 123 has been created for you. 

    I just need to know where to insert ---> include 'sendSMS.php'; 

     A big thank you to anyone that can help. I will gladly post my finished code for others.
  • rafeeque,

    Isn't that almost exactly what this mod thread that you posted to actually do?
  • Hey JjS, this looks great. Is it working and functional? Also what source/method does it use for sending sms messages? The only ones I am aware of charge a fee (eg. twilio).
  • Hi Jjs
    Can you send me your email to maile.halatuituia@tcc.to
  • Hii Jjs,
    My website is built in JAVA I want to integrate Java SMS API on my website for SMS notification can your please provide me Java code here?
Sign In or Register to comment.