Problem with sending SMS notification for geofencing alert

const { Utils, Account, Analysis, Device, Services } = require("@tago-io/sdk");
const geolib = require(“geolib”);
const sms_service = new Services({ token: context.token }).sms;
const phone_tag = device_info.tags.find(tag => tag.key === ‘phone’);
// This function checks if our device is inside a polygon geofence
function insidePolygon(point, geofence) {
const x = point[1];
const y = point[0];
let inside = false;
for (let i = 0, j = geofence.length - 1; i < geofence.length; j = i++) {
const xi = geofence[i][0];
const yi = geofence[i][1];
const xj = geofence[j][0];
const yj = geofence[j][1];
const intersect = yi > y != yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
if (intersect) inside = !inside;
}
return inside;
}

// This function checks if our device is inside any geofence
function checkZones(point, geofence_list) {
const insideZones = [];
// The line below gets all Polygon geofences that we may have.
const polygons = geofence_list.filter((x) => x.geolocation.type === “Polygon”);
if (polygons.length) {
// Here we check if our device is inside any Polygon geofence using our function above.
for (const polygon of polygons) {
if (insidePolygon(point, polygon.geolocation.coordinates[0])){
insideZones.push(polygon.event);
}
}
}
// The line below gets all Point (circle) geofences that we may have.
const circles = geofence_list.filter((x) => x.geolocation.type === “Point”);
if (circles.length) {
// Here we check if our device is inside any Point geofence using a third party library called geolib.
for (const circle of circles) {
if(
geolib.isPointWithinRadius(
{ latitude: point[1], longitude: point[0] },
{ latitude: circle.geolocation.coordinates[0], longitude: circle.geolocation.coordinates[1] },
circle.geolocation.radius
)) {
insideZones.push(circle.event);
}
}
}
return insideZones;
}

// This function help us get the device using just its id.
async function getDevice(account, device_id) {
const customer_token = await Utils.getTokenByName(account, device_id);
const customer_dev = new Device({ token: customer_token });
return customer_dev;
}

async function startAnalysis(context, scope) {
context.log(“Running”);

if (!scope[0]) throw “Scope is missing”; // doesn’t need to run if scope[0] is null

// The code block below gets all environment variables and checks if we have the needed ones.
const environment = Utils.envToJson(context.environment);
if (!environment.account_token) throw “Missing account_token environment var”;

// The line below starts our notification service.
const notification = new Services({ token: context.token }).Notification;

const account = new Account({ token: environment.account_token });
const device_id = scope[0].origin;

// Here we get the device information using our account data and the device id.
const device = await getDevice(account, device_id);

// This checks if we received a location
const location = scope.find((data) => data.variable === “location”);
if (!location || !location.location) return context.log(“No location found in the scope.”);
// Now we check if we have any geofences to go through.
const geofences = await device.getData({ variable: “geofence”, qty: 10 });
const zones = geofences.map((geofence) => geofence.metadata);

const insideZones = checkZones(location.location.coordinates, zones);
if (insideZones && insideZones.length > 0) {
const notificationMessage = The ${location.value} is inside of ${insideZones.length > 1 ? "these" : "this"} geofence ${insideZones.join(", ")};
notification.send({ title: “Inside geofence”, message: notificationMessage });
await sms_service.send({
to: phone_tag.value,
message: target inside the location!,
}).then(context.log).catch(context.log);
return;
}
}

module.exports = new Analysis(startAnalysis);

Hi @madankris929! Welcome to our community!

Soon our team or users will reply to your question.

Regards!

Hi @madankris929 ,

what type of error are you receiving?