In Steve Jobs' words,
The most precious resource we all have is time.
And rightly so, aren't we all looking for quick ways to perform our tasks? Automating even the most trivial actions, can save us some time and make us more productive. If you are looking for an interface to quickly carry out your actions, then Slash Command is the answer. So today, lets learn about slash commands and also try programming a simple command.
Now that I've kindled your curiosity, let me give a brief overview about slash commands. Slash commands is an interface, with a fixed syntax and can be triggered to complete your task or perform an action right inside the chat window. If using a slash command is easy, creating one is way more easier and interesting. More about
creating and managing slash commands are explained in our help page. Additionally, watch this video on how to create and execute a slash command!
Let us try building a command /weather that can fetch you the weather information of a location. The use case we are looking at here, is to get the weather details for the day or to fetch the weather forecast details for the upcoming five days.
The next step would be to get necessary APIs. The APIs used are from
https://developer.accuweather.com/apis. Once you source out the necessary APIs, create the command by giving the name, description, hint followed by command parameters.
The command parameters (options) we require here are :
-
city : To get the location from the user. ( Drop a hint to the user to mention the place name! )
The command execution flow is given in the flowchart below:
Breaking down the command execution flow in 2 simple steps:
1. Each location is assigned with a location key. To get the weather details for a location, the first step would be to get this location key for the place mentioned in the command input parameter.
2. This location key is then used in either the current conditions URL or the weather forecast conditions URL. This again depends on the command input.
Take a look at the /weather command execution code:
- //Define new empty Map expressions, Message and Card - to post message on command execution.
-
- message = Map();
- card = Map();
- card.put("theme","modern-inline");
- card.put("title","Weather Information");
-
- //Store the user entries (options) in the a variable
-
- place = options.get("city");
- locationURL = "http://dataservice.accuweather.com/locations/v1/cities/search?apikey=API_KEY&q=" + encodeurl(place) + "";
- response = getURL(locationURL);
-
- // Convert the string response to a map to get the "Location Key" from the response. Store the location key in a variable.
- locationkey = response.toMap().get("Key");
-
- // Any string passed while executing a command will be stored in"arguments"
-
- if(arguments.containsIgnoreCase("forecast"))
- {
- //API to get weather forecast for the specified location
- ForecastURL = "http://dataservice.accuweather.com/forecasts/v1/daily/5day/" + encodeurl(locationkey) + "?apikey=API_KEY";
- forecastdata = getURL(ForecastURL);
-
- // Convert the response to a map.
- DailyForecasts = forecastdata.toMap().get("DailyForecasts");
-
- // From the response obtained, get the information you would like to show in your command response. And organize it in a table and post as a message card!
- slides = List();
- slidedata = Map();
- slidedata.put("type","table");
- rows = List();
- for each DailyForecast in DailyForecasts
- {
- clock = DailyForecast.get("Date");
- Time = clock.getDay()+"/"+clock.getMonth();
- Temp = DailyForecast.get("Temperature").toMap().get("Minimum").toMap().get("Value");
- Celsius = ((Temp - 32) * 0.5556).round(1) + "°C";
- row = Map();
- row.put("Date", Time);
- row.put("Temperature",Celsius);
- rows.add(row);
- }
- data = Map();
- headers = List();
- headers.add("Date");
- headers.add("Temperature");
- data.put("headers",headers);
- data.put("rows",rows);
- slidedata.put("data",data);
- slides.add(slidedata);
- message.put("slides",slides);
- message.put("text","Weather forecast for the next 5 days in "+place );
- }
- else
- {
- CurrentWeatherURL = "http://dataservice.accuweather.com/currentconditions/v1/" + encodeurl(locationkey) + "?apikey=API_KEY";
-
- // API to get current weather conditions for the specified location
- current_weather_data = getURL(CurrentWeatherURL);
-
- // Convert the string response to a map.
- WeatherInfo = current_weather_data.toMap();
-
- // From the response obtained, get the information you would like to show in your command response.
- climateinfo = WeatherInfo.get("WeatherText");
- climate = WeatherInfo.get("Temperature");
- Metrics = climate.get("Metric");
- Celcius = Metrics.get("Value");
- Unit = Metrics.get("Unit");
-
- // The required information from the response will be posted as a message
- message.put("text","The weather in " + place + " is " + climateinfo + " with a temparature of " + Celcius + "°" + Unit + "");
- }
-
- //Post the message to a chat with the help of the deluge task : zoho.cliq.postToChat ();
- aa = zoho.cliq.postToChat(chat.get("id"),message);
- message.put ("card",card);
- return message;
And that's how you can use a simple slash command to fetch weather details, right inside your chat window! Let us know how this post helped you. Feel free to post your queries , suggestions here!
Cheers,
Manasa
Cliq