Callback type block

Updated 5 months ago by Enrico

Learn how to easily configure Callback Blocks with this step by step guide

Note: the Callback type block is only available on the Business Plan. If you don't see it in the builder please upgrade your account from the Subscription section. 

For this example, we’ll use OpenWeatherMap API’s as it’s easy to understand and free, just like landbot! 😄 You can try the WeatherBot here.



(If you want to try out the landbot first, click here!)

As you can see here, to get the current weather of a specific city all you have to do is make a request to https://api.openweathermap.org/data/2.5/weather?q={city name} with our APPID and we’ll get a JSON Response like this one:

{   
          "cod": 200,   
          "id": 2643743,   
          "dt": 1485789600,   
          "name": "London",   
          "base": "stations",   
          "visibility": 10000,   
          "clouds": {"all": 90},   
          "wind": {"speed": 4.1, "deg": 80},   
          "coord": {"lon": -0.13, "lat": 51.51},   
          "main": {"temp": 280.32, "pressure": 1012, "humidity": 81, "temp_min": 279.15, "temp_max": 281.15},   
          "weather": [{"id": 300, "main": "Clouds", "description": "light intensity drizzle", "icon": "09d"}],   
          "sys": {"type": 1, "id": 5091, "message": 0.0103, "country": "GB", "sunrise": 1485762037, "sunset": 1485794875} 
          }
          
The flow
  1. To start, we’ll ask visitors the city they want to know the weather in and we’ll store the variable with the name @city by using an Ask a question > Custom block:



  2. Now, we create a Send Webhook block with the previous URL, add the APPID variables and a “q” to make the query. We select the header “Accept: application/json” (important: this callback processes JSON responses only) and we’ll pick the GET method as API’s documentation shows:




  3. The first callback we’ll be using is RESPONSE CODE ROUTER, so we can separate the requests that retrieved results (200) from those that haven’t:




  4. The second callback will be a SAVE type one to save the temperature. We can see that the variable appears on the path [“main”, “temp”] and we store it in our variable @temp to be able to send it later.




  5. The last callback is a KEYWORD ROUTING type, and we’ll use it in case we want to check if it’s going to be sunny or cloudy. To do so, we’ll use the path ["weather", 0, "main"] and, depending on if it’s “clouds”, “clear” or any other option, we’ll display different messages.




And that was it for now! Still have questions? Drop us a line to help@landbot.io and we'll help you out!


How did we do?