ServiceNowDevProgram / SlackerBot

The official application repository for the bot @Slacker on the sndevs.com workspace.
https://github.com/ServiceNowDevProgram/Hacktoberfest
9 stars 73 forks source link

Update weather to include information about locality #305

Open chelming opened 5 months ago

chelming commented 5 months ago

if you !weather rochester (https://wttr.in/rochester), it doesn't tell you which Rochester the weather is for. It might be worth regexing the location

Location: Rochester, Monroe County, New York, United States of America [43.157285,-77.6152139]

/Location: ([^[]+)\s/ would probably grab the location minus the coordinates.

chelming commented 5 months ago

I think this would work:


/*
activation_example:!weather london
regex:!weather
flags:gmi
*/
var wordCount = current.text.replace('!weather', '').trim().split(' ');

if (wordCount[0] != '') {

    var rm = new sn_ws.RESTMessageV2();
    rm.setHttpMethod('GET');
    rm.setLogLevel('all');
    rm.setEndpoint('https://wttr.in/' + wordCount[0] + '?format=4');
    var response = rm.execute();
    var weatherBody = response.getBody();

    var locrm = new sn_ws.RESTMessageV2();
    locrm.setHttpMethod('GET');
    locrm.setLogLevel('all');
    locrm.setEndpoint('https://wttr.in/' + wordCount[0]);
    var locResponse = locrm.execute();
    var locBody = locResponse.getBody();
    var matches = locBody.match(/Location: ([^[]+)\s/);

    if (matches[1]) weatherBody = weatherBody.replace(/[^:]+/, matches[1]);

    new x_snc_slackerbot.Slacker().send_chat(current, weatherBody);
} else {
    new x_snc_slackerbot.Slacker().send_chat(current, 'Please Provide Location Ex: "!weather london"');
}