Skip to content

Commit

Permalink
Twilio contribution, take 1.
Browse files Browse the repository at this point in the history
-Conributed by NetConnexions.
  • Loading branch information
bradymiller committed Mar 6, 2013
1 parent f33c777 commit eb676a4
Show file tree
Hide file tree
Showing 112 changed files with 12,601 additions and 49 deletions.
63 changes: 23 additions & 40 deletions library/globals.inc.php
Expand Up @@ -945,6 +945,29 @@
'', // default
xl('API key for SMS Gateway.')
),

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Create an additional global here called enable_twilio.


'TWILIO_ACCOUNT_SID' => array(

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

use all lowercase (ie. do not use uppercase in the globals)

xl('Twilio Account Sid'),
'text', // data type
'', // default
xl('Account Sid for Twilio.')
),
'TWILIO_AUTHTOKEN' => array(

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

use all lowercase (ie. do not use uppercase in the globals)

xl('Twilio Auth Token'),
'text', // data type
'', // default
xl('Auth Token for Twilio.')
),

'TWILIO_FROM' => array(

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

use all lowercase (ie. do not use uppercase in the globals)

xl('Twilio From name'),
'text', // data type
'', // default
xl('Twilio From name')
),



'phone_notification_hour' => array(
xl('Phone Notification Hour'),
Expand Down Expand Up @@ -1049,32 +1072,6 @@
'1', // default
xl('Enable Reporting of Tracking Date For Automated Measure Calculations (AMC)')
),

'cdr_report_nice' => array(
xl('CDR Reports Processing Priority'),
array(
'' => xl('Default Priority'),
'5' => xl('Moderate Priority'),
'10' => xl('Moderate/Low Priority'),
'15' => xl('Low Priority'),
'20' => xl('Lowest Priority')
),
'', // default
xl('Set processing priority for CDR engine based reports.')
),

'pat_rem_clin_nice' => array(
xl('Patient Reminder Creation Processing Priority'),
array(
'' => xl('Default Priority'),
'5' => xl('Moderate Priority'),
'10' => xl('Moderate/Low Priority'),
'15' => xl('Low Priority'),
'20' => xl('Lowest Priority')
),
'', // default
xl('Set processing priority for creation of Patient Reminders (in full clinic mode).')
),

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Will need to add these back (this can be done in the future; don't worry about this now)


),

Expand Down Expand Up @@ -1138,13 +1135,6 @@
xl('Enable logging of all SQL SELECT queries.').' ('.xl('Note that Audit Logging needs to be enabled above').')'
),

'audit_events_cdr' => array(
xl('Audit CDR Engine Queries'),
'bool', // data type
'0', // default
xl('Enable logging of CDR Engine Queries.').' ('.xl('Note that Audit Logging needs to be enabled above').')'
),

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Will need to add this back (this can be done in the future; don't worry about this now)

'enable_atna_audit' => array(
xl('Enable ATNA Auditing'),
'bool', // data type
Expand Down Expand Up @@ -1375,13 +1365,6 @@
xl('Enable Offsite Patient Portal.')
),

'portal_offsite_providerid' => array(
xl('Offsite Patient Portal Provider ID'),
'text', // data type
'',
xl('Offsite Patient Portal Provider ID(Put Blank If not Registered).')
),

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Will need to add this back (this can be done in the future; don't worry about this now)

'portal_offsite_username' => array(
xl('Offsite Patient Portal Username'),
'text', // data type
Expand Down
64 changes: 55 additions & 9 deletions modules/sms_email_reminder/cron_sms_notification.php
Expand Up @@ -16,8 +16,9 @@

// email notification
$ignoreAuth=1;
include_once("../../interface/globals.php");
include_once("cron_functions.php");
//print_r($_SERVER);

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

remove this commented out debugging line

include_once("/var/www/openemr/interface/globals.php");
include_once("/var/www/openemr/modules/sms_email_reminder/cron_functions.php");

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Can't hard-code this path. Instead, do the following:

require_once(dirname(__FILE__) . "/../../interface/globals.php");       
require_once(dirname(__FILE__) . "/cron_functions.php");

The dirname essentially just places the path to the directory of where the file is, so will also work in cases where you are croning the script.


// check command line for quite option
$bTestRun = 0;
Expand All @@ -36,13 +37,51 @@

// object for sms
global $mysms;
$sql="select * from globals where gl_name in ('TWILIO_ACCOUNT_SID','TWILIO_AUTHTOKEN','TWILIO_FROM')";
$q=mysql_query($sql);
while($r=mysql_fetch_assoc($q)){
if($r['gl_name']=='TWILIO_ACCOUNT_SID')
$AccountSid=$r['gl_value'];
if($r['gl_name']=='TWILIO_AUTHTOKEN')
$AuthToken=$r['gl_value'];
if($r['gl_name']=='TWILIO_FROM')
$from=$r['gl_value'];
}

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

All of above code can be removed. Note that these globals are already stored in automatically:

$GLOBALS['TWILIO_ACCOUNT_SID']

etc.

if($AccountSid)

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Use this instead:

if($GLOBALS['enable_twilio'] && $GLOBALS['TWILIO_ACCOUNT_SID'])
{
include_once("/var/www/openemr/modules/sms_email_reminder/sendnotifications.php");

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Again, don't hardcode the path, use the dirname() function as shown at top of this script.

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Actually, you do not even need this conditional at all. Simply inlucde the sendnotifications.php script at the top of this script no matter what (it is simply a function and brings in classes, so much easier to do it that way).

}else
if( $db_email_msg['sms_gateway_type']=='CLICKATELL' )
{
include_once("sms_clickatell.php");
include_once("/var/www/openemr/modules/sms_email_reminder/sms_clickatell.php");

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Again, don't hardcode the path, use the dirname() function as shown at top of this script.


}else if($db_email_msg['sms_gateway_type']=='TMB4')
{
include_once("sms_tmb4.php");
include_once("/var/www/openemr/modules/sms_email_reminder/sms_tmb4.php");

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Again, don't hardcode the path, use the dirname() function as shown at top of this script.

}

function mysql2dmy($input) {

$output = false;

$input1 = $input;

$input = substr($input, 0, 10);

$d = explode('-', $input);

if (is_array($d) && count($d) >= 3) {

if (checkdate($d[1], $d[2], $d[0]) || ($d[2] == "00" && $d[1] == "00")) {

$output = "$d[2]/$d[1]/$d[0]";
}

if (substr($input1, 11))
$output.=" " . substr($input1, 11);
}

return $output;
}

// get notification settings
Expand All @@ -56,6 +95,7 @@
//echo "\nDEBUG :: user=".$vectNotificationSettings['SMS_gateway_username']."\n";

// create sms object
if($AccountSid=='')
$mysms = new sms( $SMS_GATEWAY_USENAME, $SMS_GATEWAY_PASSWORD, $SMS_GATEWAY_APIKEY );

$db_patient = cron_getAlertpatientData($TYPE);
Expand All @@ -69,14 +109,13 @@
//echo "\n-----\nDEBUG :cron_sms: found patient = ".$prow['fname']." ".$prow['lname']."\n";

// my_print_r($prow);
/*

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

don't comment out code like this. Better to remove it if no longer used.

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

See below comment before removing this, though. Since will be better to keep using this anyways for the official commit.


if($prow['pc_eventDate'] < $check_date)
{
$app_date = date("Y-m-d")." ".$prow['pc_startTime'];
}else{
$app_date = $prow['pc_eventDate']." ".$prow['pc_startTime'];
}
*/
$app_date = $prow['pc_eventDate']." ".$prow['pc_startTime'];
$app_time = strtotime($app_date);

Expand All @@ -85,7 +124,6 @@

$remaining_app_hour = round($app_time_hour - $curr_total_hour);
$remain_hour = round($remaining_app_hour - $SMS_NOTIFICATION_HOUR);

// larry :: debug
//echo "\nDEBUG :: checkdate=$check_date, app_date=$app_date, apptime=$app_time remain_hour=$remain_hour -- CRON_TIME=$CRON_TIME\n";

Expand All @@ -100,10 +138,18 @@
cron_InsertNotificationLogEntry($TYPE,$prow,$db_email_msg);

//set message
$db_email_msg['message'] = cron_setmessage($prow,$db_email_msg);
$db_email_msg['message'] = "This is a reminder of your appointment with Vijay Optica on ".mysql2dmy($app_date).". Thank you.";//cron_setmessage($prow,$db_email_msg);

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

This appears to be a specific customization, so would remove it and keep what was there before. Also don't need the mysql2dm() function above since appears to be a specific date formatting function, which would be better done with the php date() function anyways. So would also not comment out the above section that you commented out.


// send sms to patinet - if not in test mode
if( $bTestRun == 0 )
//echo $AccountSid;

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

remove this comment

if($AccountSid){

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Use this instead:

if($GLOBALS['enable_twilio'] && $GLOBALS['TWILIO_ACCOUNT_SID'])
$people=array(
$prow['phone_cell']=>"Patient"
);
$body=$db_email_msg['message'];
sendtsms($AccountSid, $AuthToken, $from, $people, $body);
}
elseif( $bTestRun == 0 )
{
cron_SendSMS( $prow['phone_cell'], $db_email_msg['email_subject'],
$db_email_msg['message'], $db_email_msg['email_sender'] );
Expand Down
47 changes: 47 additions & 0 deletions modules/sms_email_reminder/sendnotifications.php
@@ -0,0 +1,47 @@
<?php
/* Send an SMS using Twilio. You can run this file 3 different ways:
*
* - Save it as sendnotifications.php and at the command line, run
* php sendnotifications.php
*
* - Upload it to a web host and load mywebhost.com/sendnotifications.php
* in a web browser.
* - Download a local server like WAMP, MAMP or XAMPP. Point the web root
* directory to the folder containing this file, and load
* localhost:8888/sendnotifications.php in a web browser.
*/

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Use a standard script header; here is an example of how it should look like:
http://www.open-emr.org/wiki/index.php/How_to_Document_Your_Code_Properly#Page-Level_DocBlock

(and of course substitute the script details/instructions, copyright, author with your info)
It does appear, though that most of the specific instructions here are incorrect (since it appears a function senedtsms() is called, not the sendnotifications.php script itself.)

// Include the PHP Twilio library. You need to download the library from
// twilio.com/docs/libraries, and move it into the folder containing this
// file.

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Not needed, since twilio library is included.



require "twilio/Services/Twilio.php";
function sendtsms($AccountSid,$AuthToken,$from,$people,$body){
// Set our AccountSid and AuthToken from twilio.com/user/account
//$AccountSid = "AC32fb0536abf15c7e01025c4e82ac5d5a";
//$AuthToken = "98f097691adf92aab71888233aa3bd0a";

// Instantiate a new Twilio Rest Client
$client = new Services_Twilio($AccountSid, $AuthToken);

/* Your Twilio Number or Outgoing Caller ID */
//$from = '+919924927267';

// make an associative array of server admins. Feel free to change/add your
// own phone number and name here.
/*$people = array(
"9924927267" => "Johnny",
"4158675310" => "Helen",
"4158675311" => "Virgil",
);*/

// Iterate over all admins in the $people array. $to is the phone number,
// $name is the user's name
foreach ($people as $to => $name) {
// Send a new outgoing SMS */
//$body = "Bad news $name, the server is down and it needs your help";
$client->account->sms_messages->create($from, $to, $body);
echo "Sent message to $name";
}
}
?>

This comment has been minimized.

Copy link
@bradymiller

bradymiller Mar 9, 2013

Author Owner

Remove the comments that are for debugging purposes and make the spacing more clear. For example, it should be clear that all these commands are nested within the sendtsms() function.

7 changes: 7 additions & 0 deletions modules/sms_email_reminder/twilio/.gitignore
@@ -0,0 +1,7 @@
docs/_build/
vendor/*
composer.lock
composer.phar
coverage
package.xml
*.tgz
6 changes: 6 additions & 0 deletions modules/sms_email_reminder/twilio/.travis.yml
@@ -0,0 +1,6 @@
language: php
php:
- 5.3
- 5.4
script: "make test"
before_install: "composer install --dev"
19 changes: 19 additions & 0 deletions modules/sms_email_reminder/twilio/AUTHORS
@@ -0,0 +1,19 @@
A huge thanks to all of the contributors to this library.

In alphabetical order:

Manuel Amador (Rudd-O)
Adam Ballai (minddog)
Chris Barr (chrisbarr)
John Britton (johndbritton)
Kevin Burke (kevinburke)
Keith Casey (caseysoftware)
Alex Chan (alexcchan)
Kyle Conroy (derferman)
Aaron Foss (aaronfoss)
Trenton McManus (TMcManus)
Shawn Parker (Gipetto)
Bulat Shakirzyanov (avalanche123)
Asher Snyder (noloh)
Neuman Vong (luciferous)

2 comments on commit eb676a4

@bradymiller
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modules/sms_email_reminder/twilio package needs to be moved to library/twilio directory.

@bradymiller
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review is complete. Look very forward to the next revision.
-brady

Please sign in to comment.