How to add Dynamic Entities in Alexa with Python?

Viewed 328

I am developing a Alexa Skill in Python and I need to dynamically add Entities for the user to update a Slot Type so the user can choose an option.

I the page Use Dynamic Entities for Customized Interactions there is documentation and example for Node.js and Java, but no examples for Python. Looking at the Documentation for the Python SDK, it was not clear to me how to do the same in Python.

I created a Slot Type called test and tried the following code:

test_directive = {"object_type": "Dialog.UpdateDynamicEntities", "update_behavior": "REPLACE", "types": [{"name": "test", "values": [{"id": "round-rock", "name": {"value": "Round Rock Express", "synonyms": ["Round Rock", "Express"]}}, {"id": "corpus-christi", "name": {"value": "Corpus Christi Hooks", "synonyms": ["Corpus Christi", "Hooks", "Corpus"]}}]}]}

response_builder.add_directive(test_directive)

But I get the error:

'dict' object has no attribute 'object_type'

What is the correct way to add dynamic entities in Python?

1 Answers

I found the answer in the link: https://forums.developer.amazon.com/questions/210684/dynamic-entities-in-python.html. A short example in Python:

entity_synonyms_1 = EntityValueAndSynonyms(value="entity 1", synonyms=["synonym 1", "synonym 2"])
entity_1 = Entity(id="entity_id_1", name=entity_synonyms_1)

entity_synonyms_2 = EntityValueAndSynonyms(value="entity 2", synonyms=["synonym 3", "synonym 4"])
entity_2 = Entity(id="entity_id_2", name=entity_synonyms_2)                

replace_entity_directive = DynamicEntitiesDirective(update_behavior=UpdateBehavior.REPLACE, types=[EntityListItem(name="custom_type", values=[entity_1, entity_2])])

response_builder.add_directive(replace_entity_directive)
Related