When anybody txt me "where?" reply with a link to my current location

Save Recipe

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

	// Initializing variables 

	var messageText = "where?";

	// End of variables initializing 

	console.log('Started script: When anyone texts me \"' +  messageText + '\" reply automatically with my location');

    //  Register callback on sms received event
    device.messaging.on('smsReceived', function (sms) {
        console.log('received text message: ' + JSON.stringify(sms.data));

        if (sms.data.body.toLowerCase() === messageText.toLowerCase()) {
            // 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();

                // url for bing maps website, it will redirect to maps app on Windows Phone
                // where1 is the user's location and cp is the center point, which is the same as the user's location in our case
                var mapUrlPattern = 'http://maps.google.com/maps?z=1&t=m&q=loc:lat+lon';
                var mapUrl =  mapUrlPattern.replace(/lat/g, signal.location.latitude).replace(/lon/g, signal.location.longitude);

                // sending text message  with the current location
                console.log('Sending txt back ' + mapUrl);
                device.messaging.sendSms({
                        to: sms.data.from,
                        body: 'Yo, I am here: ' + mapUrl
                    },
                    function (err) {
                        if (err) {
                            console.error('Error sending text message: ' + JSON.stringify(err));
                        }
                    }
                );
            });
            locListener.start();
        }
    });
    console.log('Completed script: When anyone texts me \"' +  messageText + '\" reply automatically with my location');

Recipe Wall

973

Views