This example brings together several ideas about variables, flow control and widgets.
The purpose of the exercise is to send a random 'On Vacation' SMS in reply to a received SMS, and write a log file of which message we sent to who.
We only want to do this when we set a particular variable %ONVAC, so we need two tasks; one to turn on/off the vacation messaging, and one to send the response to SMSs when necessary.
Data File: vac.txt
First, we need to create a data file with our random responses. Each record consists of the message to send, and a tag that we'll write to a log file log.text so we know which message we sent to who.
Hello, Fred here.
Thanks for your message of %SMSRD. I'm away at the moment but back soon!
;;;
Sent Away Message 1 to %SMSRN at %SMSRT.
Hello, Fred here. I'm on the beach. Wish you were here ?
;;;
Sent Beach Message 2 to %SMSRN at %SMSRT.
There must be a blank line between the two records, because we are going to be reading paragraphs from the file.
Note that the items in each record are divided into fields by ;;;, so we can access them separately in our task.
%SMSRN, %SMSRD and %SMSRT are built-in variables which store the last received SMS number, date and time.
We'll put this file a /sdcard/vac.txt
Responder Task
1. Stop
If %ONVAC = no
Don't do anything if we're not on vacation.
2. Variable Randomize %RECNO, 1, 2
Pick a random record between 1 and 2.
3. Read Paragraph vac.txt, %RECNO, %RECORD
Read it into a variable %RECORD.
4. Variable Split %RECORD, ;;;
Split it into the different fields %RECORD1 and %RECORD2.
5. Send SMS %SMSRN, %RECORD1
Send the first field (the message) off to the sender of the SMS we received.
To enter %SMSRN, click the Var button and select Last SMS From.
6. Write File log.txt, %RECORD2, Yes
Append a line to the log file with the second field.
This task is set as the Enter task of a profile containing an SMS Received event.
Control Task
We still need a task to toggle the value of %ONVAC. This would be useful as a homescreen widget, so we create one called TOGGLER to run it.
1. Goto 4
If %ONVAC = yes
If we're vacationing, jump to the part that turns off vacationing.
2. Variable Set %ONVAC, yes
If we got here, we're not vacationing. Turn it on.
3. Goto 5
Jump to the part that sets the widget label.
4. Variable Set %ONVAC, no
Turn off vacationing.
5. Set Widget Label TOGGLER, %ONVAC
Set the label of TOGGLER so we can see the current state.
So if %ONVAC is yes, the action flow is 1, 4, 5, otherwise if it's no it's 1, 2, 3, 5.
You could instead set the icon of the TOGGLER widget for a more visual indication
with the Set Widget Icon action.