I have a simple form that provides the user with vaccination statistics by location. We have per-country data, and we are planning to add per-city data too. E.g.
User: What percentage of the population is vaccinated in Italy?
Bot: As of today, 78% have received one shot and 77% both shots.
User: Thats great, thanks!
For countrywide results, we had a very simple form:
(domain.yml)
vaccine_stats_form:
location:
- type: from_entity
entity: country
But now, we want to enable user declaring either a country or a city. Ideally, we would want something like this
vaccine_stats_form:
location:
- type: from_entity
entity: country OR city
Is there something else we can do? Or do we have to unify the two entities into one umbrella entity (i.e. location)?
In your case, it’s the extract method that you want to modify. You check if the message has a city entity. If yes, you fill it in the location slot; if not, you try the same with the country entity:
class ValidateRestaurantForm(FormValidationAction):
def name(self):
return 'validate_vaccine_stats_form'
async def required_slots(self, slots_from_domain, dispatcher, tracker, domain):
return slots_from_domain
async def extract_location(self, dispatcher, tracker, domain):
city = next(tracker.get_latest_entity_values('city'), None)
country = next(tracker.get_latest_entity_values('country'), None)
location = city if city else country
return {'location': location}
If you don’t/won’t need to differentiate between cities and countries, I’d suggest going with the simpler approach of merging them into one location entity.
Thank you so much for your timely response!
We had missed the extract_slot_name feature. We will use it to solve another problem, that of extracting time slots from specific entities of the user, apart from the duckling-extracted ones.
One more question is (and I haven’t found the answer in the docs): what do we do with the required slots declared inside the form declaration (in the domain.yml) ? I think it doesn’t matter anymore, so we can just leave it like before, right?
vaccine_stats_form:
location:
- type: from_entity
entity: country