python-3.xrasa-corerasarasa-x

How do I edit a URL's location and use it for my code


I am trying to create a simple bot using rasa which runs on Python to extract data from an api.

To make explanations easier let us assume it is a corona tracker bot. So the api URL for the corona data is made so that the final section of it filters the api data to a specific set of data so for example:

https://exampleurl.com/api/corona/country/Germany

This means it only contains the data from Germany, but if i were to change it to Russia for example, then it would display the data from Russia.

What I want is that when someone tells my bot "Please tell me how Russia is doing" the bot will recognize first the correct intent and also that the entity is Russia. With that entity I want it to place it into the correct place of the URL and put that into a custom action which will extract the data from the api.

So to summarize I want that entity <Country> gets inputted into https://exampleurl.com/api/corona/country/<country>


Solution

    1. Use python string concatenation to join 2 strings like so:
    country = "Russia"
    url = "https://exampleurl.com/api/corona/country"
    new_url = url + '/' + country
    
    1. Use formatted string literals, introduced in Python 3.6:
    new_url = f"{url}/{country}"