Send random message to someone when you unlock phone

Send random message (out of a list of messages provided by you) to someone when you unlock your phone first time in the morning

This code allows you to send a random message (out of a list of messages provided by you in the code) to any number you want, whenever you unlock your phone for the first time in the morning.

So this random message will only be sent once each day.

Also, you can vary the list of messages you want to include any number of messages.

You can also specify a time so that the message will be sent only after that time.
For eg, suppose you set the time as 10AM.. So if you unlock your phone at 9:30AM, the random message will not be sent. But when you unlock your phone after 10AM, a random text out of the list will automatically be sent to the designated number.

To use this on{X} Recipe, simply copy the code below and modify if needed.

var message = new Array();
message[0] = "Text 1";
message[1] = "Text 2";
message[2] = "Text 3";
message[3] = "Text 4";
message[4] = "Text 5";
message[5] = "Text 6";
// You can add any number of messages in a similar manner.

var contact = { name : "XYZ", phoneNumber : "+12345" } ;
var todayDate = new Date().toLocaleDateString();

var time = "9:00 AM";
// You can change this time to suit your needs. The message
// will be sent when you unlock your phone for the first time
// in the day, after this time. So if I unlock my phone at
// 8:30AM, the message wont be sent, but if I open it at
// 9:15AM, the message will be sent

// End of variables initializing

function sendTextMessage(rand_no){
device.messaging.sendSms({
to: contact.phoneNumber,
body: message[rand_no]
},
function (err) {
if (err) {
console.error('Error sending text message: ' + JSON.stringify(err));
}
}
);
}

device.screen.on('unlock', function () {
var lastDateScreenUnlocked = device.localStorage.getItem('lastDateScreenUnlocked');
var today = new Date().toLocaleDateString();
var dailyTime = new Date(today + ' ' + time);
var now = new Date();
var rand_no = Math.floor((message.length-0)*Math.random());

if (now > dailyTime) {
if (!lastDateScreenUnlocked || lastDateScreenUnlocked !== today) {
sendTextMessage(rand_no);
console.log('Message sent: ' + message[rand_no]);
}
device.localStorage.setItem('lastDateScreenUnlocked', today);
}
});

Recipe Wall

2,983

Views