Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# REDCap-Twilio SMS Notification System
This system automatically sends SMS notifications to participants in a REDCap project after they complete registration. The system monitors for new records in REDCap, waits for a configurable delay period, and then sends a customized SMS message with a survey link.
## System Flow
### First Run Process
```
[Start]
│
▼
[Get All Records from REDCap]
│
▼
[Save All Record IDs to existing_records.json]
│
▼
[Exit Program]
```
On the first run, the system:
1. Retrieves all current record IDs from REDCap
2. Saves them to `existing_records.json`
3. Exits without sending any messages
### Subsequent Runs Process
```
▼
[Check Each Record]
│
├─── If in existing_records.json ──► [Skip]
│
└─── If New Record ────────────────► [Process]
│
▼
[Check Registration Status]
│
├─── If Complete ──► [Add to Processing Queue]
│
└─── If Incomplete ──► [Skip]
│
▼
[Check Processing Queue]
│
├─── If Delay Complete ──► [Send SMS]
│ │
│ ▼
│ [Save to processed_records.json for logging]
│ │
│ ▼
│ [Save to existing_records.json]
│
└─── If Delay Not Complete ──► [Wait]
```
On subsequent runs, the system:
1. Gets all records from REDCap
2. Identifies new records (not in `existing_records.json`)
3. For each new record:
- Checks if registration is complete
- Adds complete records to the processing queue with current timestamp
4. For each record in the processing queue:
- Checks if the configured delay time has passed since registration
- If delay completed:
- Gets a survey link for the participant
- Finds the participant's phone number
- Sends an SMS with the survey link
- Logs the message in `processed_records.json`
- Adds the record ID to `existing_records.json`
- If delay not completed:
- Waits until next run
## Key Features
- **First Run Detection**: Automatically detects first run and initializes system
- **New Record Detection**: Only processes records not seen before
- **Configurable Delay**: Waits a configurable time before sending messages
- **REDCap Integration**: Directly interfaces with REDCap API
- **Twilio SMS Integration**: Sends SMS messages via Twilio
- **Message Logging**: Logs all sent messages in REDCap and locally
- **Fallback Mechanisms**: Includes robust error handling and fallbacks
- **Command Line Interface**: Supports various commands for maintenance
## Configuration
All configuration is stored in a PostgreSQL database but includes fallbacks if database access fails:
### REDCap Configuration
- API URL and Token
- Event Name and Repeat Instance
### Twilio Configuration
- Account SID and Auth Token
- Phone Number
### Processing Configuration
- Registration delay time (minutes)
- Polling interval (seconds)
- Registration status fields to check
- Phone number fields to check
- Survey instruments to use
- Phone formatting options
- Message template
## Usage
```
python app.py - Run the monitoring process
python app.py force <id> - Force send a message to record <id>
python app.py sync - Synchronize processed records with REDCap
python app.py help - Show help message
```
## Files
- `app.py`: Main application file
- `redcap.py`: REDCap integration functions
- `existing_records.json`: Stores known record IDs
- `processed_records.json`: Stores message history
## Requirements
- Python 3.6+
- REDCap API access
- Twilio account
- PostgreSQL database (optional, for configuration)
## Installation
1. Clone the repository
2. Install required packages: `pip install -r requirements.txt`
3. Configure database connection or edit fallback values
4. Run the application: `python app.py`
## Troubleshooting
If you encounter issues:
1. Check database connection
2. Verify REDCap API access
3. Confirm Twilio credentials
4. Check logs for detailed error messages
5. Use the `force` command to test specific records
## Features
- Automatically monitors REDCap for new registrations
- Sends personalized survey links via SMS
- Implements a 1-minute delay for new participants
- Prevents duplicate message sending
- Maintains a permanent record of sent messages
- Secure credential management using environment variables
## Prerequisites
- Python 3.6 or higher
- REDCap API access
- Twilio account with SMS capabilities
- Required Python packages (listed in requirements.txt)
## Configuration
### REDCap Setup
1. Ensure your REDCap project has:
- A registration form with a completion field
- A phone number field for participants
- An interest form survey
2. The registration completion field should be named "registration_complete" with value "2" when complete
### Phone Number Field
The system will look for the phone number in the following fields (in order):
- phone_number
- phone
- mobile
- mobile_number
- cell
- cell_number
- contact_phone
- participant_phone
If your REDCap project uses a different field name, modify the `possible_phone_fields` list in the code.
## Usage
1. Start the monitoring system:
```bash
python app.py
```
2. The system will:
- Check for new registrations every 10 seconds
- Wait 1 minute after registration completion
- Send the survey link via SMS
- Keep track of sent messages to prevent duplicates
## File Structure
- `app.py`: Main application code
- `.env`: Environment variables (create this file)
- `processed_records.json`: Stores records of sent messages
- `requirements.txt`: Python package dependencies
## Error Handling
The system includes comprehensive error handling for:
- API connection issues
- Missing or invalid credentials
- SMS sending failures
- Record processing errors
All errors are logged with detailed information for debugging.
## Logging
The system provides detailed logging of:
- Record processing status
- Registration completion checks
- SMS sending attempts
- Error messages
- Available REDCap fields
## Security
- Credentials are stored in environment variables
- No sensitive data is stored in the code
- API tokens and keys are never logged
- Processed records are stored locally only
## Maintenance
- The system maintains a permanent record of sent messages
- Duplicate messages are prevented even after system restarts
- Failed message attempts are logged for review
## Troubleshooting
1. If messages aren't sending:
- Check Twilio credentials in .env
- Verify phone number format
- Check Twilio account balance
2. If records aren't being processed:
- Verify REDCap API credentials
- Check registration_complete field value
- Review available fields in REDCap
3. If the system isn't starting:
- Verify all environment variables are set
- Check Python version
- Ensure all dependencies are installed
## Support
For issues or questions:
1. Check the logs for error messages
2. Verify all configuration settings
3. Contact system administrator