Accepts a valid zip code and returns information about the zip code in an embeded message
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
//get zip code from message
let zip = context.params.event.content.split(' ')[1]
//define 5 digit zip code regeg
const zReg = new RegExp(/\d{5}/);
//test against zip code regex to verify valid zip code
if(zReg.test(zip)){
//get zip code info using http request
let response = await lib.http.request['@1.1.6'].get({
url: `api.zippopotam.us/us/${zip}`
});
//if we received data continue
if(response.data['post code'] != undefined){
//shorten response data
let data = response.data;
//build description of embed message with response data
let desc = `Zip Code: ${data['post code']}\nCountry: ${data.country}\n\nCities: \n`;
//loop through each place returned to list out each one
data.places.forEach(pl => desc += `${pl['place name']}, ${pl.state}\n`)
//send zip code information message
await lib.discord.channels['@0.2.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": "",
"tts": false,
"embeds": [
{
"type": "rich",
"title": `Zip Code Information`,
"description": desc, //description variable created earlier
"color": 0x000000
}
]
});
}
//No data received, most likely invalid zip code
else{
//send zip not found message
await lib.discord.channels['@0.2.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": "",
"tts": false,
"embeds": [
{
"type": "rich",
"title": `Zip Code Information`,
"description": `Zip code not found!`,
"color": 0x000000
}
]
});
}
}
//no data received, send error msg
else{
await lib.discord.channels['@0.2.0'].messages.create({
"channel_id": `${context.params.event.channel_id}`,
"content": "",
"tts": false,
"embeds": [
{
"type": "rich",
"title": `Zip Code Information`,
"description": `Please enter a valid zip code.`,
"color": 0x000000
}
]
});
}