CiscoSE / pyadaptivecards

Author adaptive cards in native python
Other
30 stars 9 forks source link

Not able to use Choices #3

Closed nrao-nmdp closed 4 years ago

nrao-nmdp commented 4 years ago
import json

from pyadaptivecards.card import AdaptiveCard
from pyadaptivecards.inputs import Text, Number, Choices
from pyadaptivecards.components import TextBlock
from pyadaptivecards.actions import Submit

from webexteamssdk import WebexTeamsAPI

api = WebexTeamsAPI(access_token="XXXXXXXXX")

room_id='XXXXXXXXXXXXXXX'

greeting = TextBlock("Hey hello there! I am a adaptive card")

first_name = Text('first_name', placeholder="First Name")
age = Number('age', placeholder="Age")
choice = Choices(id = "instance-choice", choices=["asdf"], style="compact")
submit = Submit(title="Send me!")
card = AdaptiveCard(body=[greeting, first_name, age, choice], actions=[submit])
attachment = {
    "contentType": "application/vnd.microsoft.card.adaptive",
    "content": card.to_dict()
}
api.messages.create(roomId=room_id, text="Fallback", attachments=[attachment])

ERROR:

$ python individual_test_api.py 
Traceback (most recent call last):
  File "individual_test_api.py", line 34, in <module>
    print(card.to_json(pretty=True))
  File "/Users/pyadaptivecards/abstract_components.py", line 67, in to_json
    ret = json.dumps(self.to_dict(), indent=4, sort_keys=True)
  File "/Users/pyadaptivecards/card.py", line 82, in to_dict
    ret = super().to_dict()
  File "/Users/pyadaptivecards/abstract_components.py", line 106, in to_dict
    l.append(i.to_dict())
  File "/Users/pyadaptivecards/abstract_components.py", line 106, in to_dict
    l.append(i.to_dict())
AttributeError: 'str' object has no attribute 'to_dict'

Any help will be appreciated, Thanks.

sQu4rks commented 4 years ago

Hi,

thanks for raising this. This seems to be an error in the internal serialization component. I'll have a look at the issue and come back to you.

sQu4rks commented 4 years ago

Hi @nrao-nmdp

the issue is that the choices parameter in the Choices class (see docs here) requires a list of Choice(see the documentation here) objects not just a list of strings.

The following code works.

import json

from pyadaptivecards.card import AdaptiveCard
from pyadaptivecards.inputs import Text, Number, Choices
from pyadaptivecards.components import TextBlock, Choice
from pyadaptivecards.actions import Submit

from webexteamssdk import WebexTeamsAPI

api = WebexTeamsAPI()
greeting = TextBlock("Hey hello there! I am a adaptive card")

first_name = Text('first_name', placeholder="First Name")
age = Number('age', placeholder="Age")

c = Choice("asdf", "asdf")
choice_set = Choices(id = "instance-choice", choices=[c], style="compact")
submit = Submit(title="Send me!")
card = AdaptiveCard(body=[greeting, first_name, age, choice_set], actions=[submit])

attachment = {
    "contentType": "application/vnd.microsoft.card.adaptive",
    "content": card.to_dict()
}

api.messages.create(toPersonEmail="mneiding@cisco.com", text="Fallback", attachments=[attachment])

The error message clearly needs improvement. I have opened an issue(#4) for this.