davidchua / pymessenger

A Python Wrapper for the FB Messenger Bot API (Send/Receive API)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pymessenger.Element is not JSON serializable

vquigley opened this issue · comments

Hi

I'm trying to use the send_generic_message function but am getting an error pymessenger.Element is not JSON serializable

elements = []
element = Element(title="test", image_url="<arsenal_logo.png>", subtitle="subtitle", item_url="http://arsenal.com")
elements.append(element)
bot.send_generic_message(id, elements)

Here is my pip freeze output:

coverage==3.7.1
decorator==4.0.9
ecdsa==0.10
Fabric==1.8.2
feedparser==5.2.1
Flask==0.10.1
Flask-SQLAlchemy==1.0
Flask-WTF==0.9.4
gunicorn==19.3.0
itsdangerous==0.23
Jinja2==2.7.2
MarkupSafe==0.18
paramiko==1.12.2
pycrypto==2.6.1
pymessenger==0.0.4.0
requests==2.10.0
requests-toolbelt==0.6.2
six==1.10.0
SQLAlchemy==0.9.3
validators==0.10.1
Werkzeug==0.9.4
WTForms==1.0.5

I've added the following to fix this in my PR:

class Element(dict):
    __acceptable_keys = ['title', 'item_url', 'image_url', 'subtitle', 'buttons']
    def __init__(self, _args, *kwargs):
        kwargs = {k:v for k, v in kwargs.iteritems() if k in self.__acceptable_keys}
        super(Element, self).__init_
(_args, *_kwargs)

    def to_json(self):
        return json.dumps({k:v for k, v in self.iteritems() if k in self.__acceptable_keys})

I don't think Element should be anything more than a dictionary. The API may change and we don't need to update the Element class if optional attributes are added. The silent enforcement of attributes is not that helpful either.

This is fixed in PR #5, release 0.0.5