Send random message (out of a list of messages provided by you) to someone when you unlock your phone first time in the morning
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);
}
});
Views