Skip to content

Commit

Permalink
Merge pull request #9 from susom/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
Jordan-M-Schultz authored Jan 21, 2025
2 parents 54943d6 + f1bce2a commit 94718fa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
14 changes: 13 additions & 1 deletion README_DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ The following notes should help set up REDCap with What's App by Twilio
6. Create a Message Service to handle these messages and associate it to your sender
7. You must register approved What's App Templates. What's App and Twilio have complex rules about what content you can sent to a What's App number. The content depends on whether or not your are in an 'open session' or not. An open session lasts 24 hours and begins when a participant replies to your phone number. In most cases you will NOT have an open session with a participant and will have to use a 'template' to message them. We suggest creaing an 'ice breaker' template that can be used to open a new open session.

## Local development
#### Getting inbound requests to work properly
Under the PACE-DEV developer settings in the integration tab make sure to set the webhook url:
- `[NGROK_URL]/api/?type=module&prefix=whats-app-alerts&page=pages%2Finbound&NOAUTH`
- Ignore the delivery status callback url (this is set in overridden in EM System settings)
- Phone number designated in EM settings determines which inbound callback to use (prod and dev will be different)

TODO: Document better the next time someone goes through this process... It was long and difficult for us the first time around.
TODO: Second time around it was still very complex



Expand All @@ -36,4 +44,8 @@ TODO: Document better the next time someone goes through this process... It was

## XDebug configuration
1. Each time refreshing ngrok urls, you must update path mappings in PHPStorm for Xdebug
2. Add /var/www/html as the path mapping to the www folder on the server to the new entry
2. In the upper right hand corner click edit configuations
3. Create a new remote debug service copying the newly refreshed NGROK url as the host
- Ex : `c17a-99-35-20-190.ngrok-free.app`
4. Update path mappings of www folder to /var/www/html

28 changes: 28 additions & 0 deletions WhatsAppAlerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,34 @@ class WhatsAppAlerts extends \ExternalModules\AbstractExternalModule {

private $wah;


public function redcap_save_record ( int $project_id, string $record = NULL, string $instrument, int $event_id, int $group_id = NULL, string $survey_hash = NULL, int $response_id = NULL, int $repeat_instance = 1 ) {
/**
* Required function that executes before redcap_email
* This function will set the URL field in enrollment, so it can be sent via What's App alerts
* Alert execution (email hook) triggers before default save, thus: this workaround will ensure the field is never empty
*/
$pSettings = $this->getProjectSettings();

if(empty($_POST[$pSettings['url-field']])) { // execute only the first time enrollment is saved -- survey url has not been calculated
$pro = new \Project($project_id);
$primary_field = $pro->table_pk;
$pSettings = $this->getProjectSettings();

$url = \REDCap::getSurveyLink($record, $pSettings['survey-instrument'], $pSettings['survey-event'], '1', $project_id);
$saveData = [
[
$primary_field => $record,
$pSettings['url-field'] => $url,
]
];

$this->emLog("Saving url data for project $project_id, record $record. Url: $url");
// Save data using REDCap's saveData function
$res = \REDCap::saveData($project_id, 'json', json_encode($saveData), 'overwrite');
}
}

/**
* Generic REDCap Email Hook
*/
Expand Down
23 changes: 21 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"permissions": [
"redcap_email",
"redcap_module_system_enable",
"redcap_every_page_top"
"redcap_every_page_top",
"redcap_save_record"
],

"enable-every-page-hooks-on-system-pages": false,
Expand Down Expand Up @@ -110,7 +111,25 @@
"name": "<b>Enable Debug Logging</b><br><i>(optional)</i> Requires installation and configuration of emLogger",
"required": false,
"type": "checkbox"
}
},
{
"key": "survey-instrument",
"name": "<b>Survey Link Instrument</b><br><i>(optional)</i>Specific form that will be used to generate the survey link sent via What's App after enrollment is saved",
"required": false,
"type": "form-list"
},
{
"key": "survey-event",
"name": "<strong>Survey Link Event</strong><br><i>The event / arm the survey link should be in ",
"required": true,
"type": "event-list"
},
{
"key": "url-field",
"name": "<strong>Url Field</strong><br><i>The field the survey url (sent via what's app) for consent will be stored within",
"required": true,
"type": "field-list"
}
],


Expand Down

0 comments on commit 94718fa

Please sign in to comment.