Sends a text message to a specified phone number with a player's Halo Infinite match results within 1 minute of completing a game.
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let phoneNumber = process.env.PHONE_NUMBER;
if (phoneNumber.startsWith('+')) {
// remove +
phoneNumber = phoneNumber.slice(1);
}
if (!phoneNumber.startsWith('1')) {
// add 1 - US, CA only
phoneNumber = `1${phoneNumber}`;
}
let gamertag = process.env.GAMERTAG;
let matchResult = await lib.halo.infinite['@0.0.3'].stats.matches.list({
gamertag: gamertag,
limit: {
count: 1,
offset: 0,
},
mode: 'matchmade',
});
// retrieve last match id from the key-value store
let lastMatchId = await lib.utils.kv['@0.1.16'].get({
key: `halo-infinite-last-match`,
defaultValue: null,
});
let match = matchResult.data[0];
if (match.id !== lastMatchId) {
await lib.utils.sms['@2.0.2']({
to: phoneNumber,
body: [
`🎮 ${gamertag} just ${match.player.outcome === 'win' ? 'won' : 'lost'} `,
`a ${match.details.category.name} game on ${match.details.map.name}!\n\n`,
`🔫 Kills: ${match.player.stats.core.summary.kills}\n`,
`☠️ Deaths: ${match.player.stats.core.summary.deaths}\n`,
`🎯 KDR: ${match.player.stats.core.kdr.toFixed(2)}\n\n`,
`⏱ ${match.duration.human}`,
].join(''),
});
// set new last match id in the key-value store
await lib.utils.kv['@0.1.16'].set({
key: `halo-infinite-last-match`,
value: `${match.id}`,
});
return {
result: `sent`,
match: match,
};
} else {
return {
result: `unsent`,
match: match,
};
}