cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1492
Views
0
Helpful
2
Replies

Working with Webex Cards

nathy1984
Level 1
Level 1

Hello

 

I try to work with webex cards.

I can send a request on the website https://developer.webex.com/docs/api/v1/messages/create-a-message and it works

See print screen

webex_response.png

 

If I try the same with Python requests it does not work:

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder

m = MultipartEncoder ( {
    "roomId": "Y2lzY29zcGFyazovL3VzL1ddddddddddjEyMi0zZTE5LWI4MTQtOWEdddddddddd",
    "attachments": {
        "contentType": "application/vnd.microsoft.card.adaptive",
        "content": {
            "type": "AdaptiveCard",
            "version": "1.0",
            "body": [
                {
                    "type": "TextBlock",
                    "text": "Adaptive Cards",
                    "size": "large"
                }
            ],
            "actions": [
                {
                    "type": "Action.OpenUrl",
                    "url": "http://adaptivecards.io",
                    "title": "Learn More"
                }
            ]
        }
    },
    "text": "test"
} )

r = requests.post ( 'https://api.ciscospark.com/v1/messages', data=m,
                    headers={
                        'Authorization': 'Bearer MyKEY',
                        'Content-Type': m.content_type} )

print ( r.text )

Error message:

AttributeError: 'dict' object has no attribute 'encode'

 

1 Accepted Solution

Accepted Solutions

dstaudt
Cisco Employee
Cisco Employee

This kind of 'attachment' is different from the usual mime-multipart attachment, i.e. used to upload files...perhaps you were making things a bit harder than needed :)

 

This seems to work for me:

import requests
# from requests_toolbelt.multipart.encoder import MultipartEncoder

# m = MultipartEncoder ( {
m = {
    "roomId": "Y2lzY29zcGFyazovL3VzL1JPT00vOGQ4YmEwZTAtMTlkNy0xMWU2LTg1OWEtOWYxODkzNjRkMGUx",
    "attachments": {
        "contentType": "application/vnd.microsoft.card.adaptive",
        "content": {
            "type": "AdaptiveCard",
            "version": "1.0",
            "body": [
                {
                    "type": "TextBlock",
                    "text": "Adaptive Cards",
                    "size": "large"
                }
            ],
            "actions": [
                {
                    "type": "Action.OpenUrl",
                    "url": "http://adaptivecards.io",
                    "title": "Learn More"
                }
            ]
        }
    },
    "text": "test"
}

# r = requests.post ( 'https://api.ciscospark.com/v1/messages', data=m,
r = requests.post ( 'https://api.ciscospark.com/v1/messages', json=m,
                    headers={
                        'Authorization': 'Bearer {redacted}',
                        'Content-Type': 'application/json' } )

print ( r.text )

 

View solution in original post

2 Replies 2

dstaudt
Cisco Employee
Cisco Employee

This kind of 'attachment' is different from the usual mime-multipart attachment, i.e. used to upload files...perhaps you were making things a bit harder than needed :)

 

This seems to work for me:

import requests
# from requests_toolbelt.multipart.encoder import MultipartEncoder

# m = MultipartEncoder ( {
m = {
    "roomId": "Y2lzY29zcGFyazovL3VzL1JPT00vOGQ4YmEwZTAtMTlkNy0xMWU2LTg1OWEtOWYxODkzNjRkMGUx",
    "attachments": {
        "contentType": "application/vnd.microsoft.card.adaptive",
        "content": {
            "type": "AdaptiveCard",
            "version": "1.0",
            "body": [
                {
                    "type": "TextBlock",
                    "text": "Adaptive Cards",
                    "size": "large"
                }
            ],
            "actions": [
                {
                    "type": "Action.OpenUrl",
                    "url": "http://adaptivecards.io",
                    "title": "Learn More"
                }
            ]
        }
    },
    "text": "test"
}

# r = requests.post ( 'https://api.ciscospark.com/v1/messages', data=m,
r = requests.post ( 'https://api.ciscospark.com/v1/messages', json=m,
                    headers={
                        'Authorization': 'Bearer {redacted}',
                        'Content-Type': 'application/json' } )

print ( r.text )

 

Thank you. That works