To create a function that handles a command coming from Slack.
NOTE: The weather API used in this lab will only be temporarily available and will have to be remplaced by another API after CASCON 2018 closes
- The weather can be obtained by calling the following API:
https://casconweather.mybluemix.net/weather?address=${address}
where the ${address}
will be an address for which you want the weather.
Note that this API is a simplified API that does not require an API key or authentication which would be the case with a real API.
Inspire your code from the code created in the slackapp-command.js
and call the API listed above, process the results and respond back to the user with the requested weather information.
Solution to call the Weather API:
const getWeather = (address, callback) => {
request({
url: `https://casconweather.mybluemix.net/weather?address=${address}`,
json: true
}, (error, response, body) => {
if (error) {
return callback("Unable to connect to Cascon Weather Server");
} else if (response.statusCode === 400) {
return callback("Invalid geolocation.");
} else if (response.statusCode === 200) {
console.log("Response: ", response);
console.log("Body: ", body);
try {
return callback(undefined, {
message: body.message,
address: body.address,
summary: body.weatherSummary,
temperature: body.temperature
});
} catch (e) {
return callback(e);
}
}
});
};
Solution to provide a response:
// Reply to the message
function (registration, user, callback) {
if (command.command === "/weather") {
getWeather(command.text, (errorMessage2, resultingWeather) => {
if (errorMessage2) {
return callback(errorMessage2);
}
console.log("Before Reply: ", JSON.stringify(resultingWeather, undefined, 2));
return callback(null, `Hey ${user.real_name}, the temperature at ${resultingWeather.address} is ${resultingWeather.summary} and ${resultingWeather.temperature} Celcius`);
});
} else {
return callback("Un-implemented slash command!");
}
}
-
Modify the
deploy.sh
script to include the command deployment (Uncomment the commands that refer to the slackapp-command.js) -
Reuse the parameters.json configurations previously prepared from lab0, copying it to the same directory as your deploy.sh
cp ../../lab00-preparation/parameters.json .
-
Deploy the command:
./deploy.sh --install
-
Go to https://api.slack.com and select Your apps
-
Select your application
-
Under Slash Commands, Create New Command
-
Set the values
-
Command: /weather
-
Request URL: URL of the
slackapp-command
web action. The URL should look likehttps://us-south.functions.cloud.ibm.com/api/v1/web/your-org_your-space/slackapp/slackapp-command
-
Short Description: A command that displays the weather for the address provided
-
Usage Hint: [address]
-
-
Save
-
If you see the following prompt (yellow), click the link to reinstall the app
-
And confirm the identity of the service by clicking Authorize
-
Navigate to your Slack workspace and test your newly created Slash Command by typing
/weather <some city>
The Slash command executes and returns the weather for the city specified and responds with a message that looks:
Hey <user name>, the temperature at <city> is <weather information returned>
You are now ready to continue with one of the following: