When a friend texts you "where?", the recipe will automatically reply with you location : "City(PostCode)"
To use this on{X} Recipe, simply copy the code below and modify if needed.
// Initializing variables
var friend = { name : "My Friend",phoneNumber : "+33111222333" } ;
var messageText = "where?";
// Sometimes send 2 or more SMS. Boolean to avoid this problem
oneReceive = false;
// End of variables initializing
console.log('Started script: When ' + friend.name + ' texts me \"' + messageText + '\" reply automatically with my location');
// Register callback on sms received event
device.messaging.on('smsReceived', function (sms) {
if (sms.data.from === friend.phoneNumber && sms.data.body.toLowerCase() === messageText) {
console.log('SMS received');
oneReceive = false;
// getting location from cell, which is accurate enough in this case, time interval is 100 milliseconds, to get immediate location sample
var locListener = device.location.createListener('CELL', 100);
locListener.on('changed', function (signal) {
// stop listening to location changed events after getting the current location
locListener.stop();
// Var latitude longitude for the Geocoder Request
lat = signal.location.latitude;
lon = signal.location.longitude;
// Reverse Geocoder Request
var params = {
url: 'https://dev.virtualearth.net/REST/v1/Locations/'+lat+','+lon+'?includeEntityTypes=Address&o=json&key=YourBingMapKEY',
type: 'GET',
headers : {'Content-Type': 'application/javascript'}
};
// Success to revert the location to an adress
var success = function(body, textStatus, response) {
// Parse the JSON Request
var jsonRep = JSON.parse(body);
// Get City and PostalCode of the current location
var locality = jsonRep.resourceSets[0].resources[0].address.locality;
var postalCode = jsonRep.resourceSets[0].resources[0].address.postalCode;
console.log("Fin de récupération du JSON. Ville="+locality+";CodePostal="+postalCode);
// sending text message with the current location
device.messaging.sendSms({
to: friend.phoneNumber,
body: 'I am here: ' +locality+' ('+postalCode+')'
},
function (err) {
if (err) {
console.error('Error sending text message: ' + JSON.stringify(err));
}
}
);
console.log("Message envoyé");
};
// AJAX CALL ERROR FUNCTION
var error = function(textStatus, response) {
console.log(textStatus, response);
};
// Call the AJAX
if (!oneReceive) {
console.log("Sending SMS .... Call Geocoder Bing API");
device.ajax(params, success, error);
oneReceive = true;
}
});
locListener.start();
}
});
console.log('Completed script: When ' + friend.name + ' texts me \"' + messageText + '\" reply automatically with my location');
Views