Ejemplo n.º 1
0
    def test_send_message(self):
        """Test facebook message sending."""

        # Test simple text message
        m = Message('test')
        facebook.send_message(self.user, [m])

        # Test image message
        m = Message(image_url='http://i.imgur.com/4loi6PJ.jpg')
        facebook.send_message(self.user, [m])

        # Test button template message
        m = Message(buttons_text='Button template test')
        m.add_button(Message.Button(Message.ButtonType.WEB_URL,
                                    'Google', url='http://www.google.com/'))
        m.add_button(Message.Button(Message.ButtonType.WEB_URL,
                                    '17', url='http://www.17.media/'))
        facebook.send_message(self.user, [m])

        # Test list template message
        m = Message(top_element_style=Message.ListTopElementStyle.LARGE)
        l = Message.ListItem('Google', 'google.com',
                             'http://i.imgur.com/1QfaG1u.png')
        l.set_default_action(Message.Button(Message.ButtonType.WEB_URL,
                                            url='http://www.google.com'))
        l.set_button(Message.Button(Message.ButtonType.WEB_URL,
                                    title='Goto Google',
                                    url='http://www.google.com/'))
        m.add_list_item(l)
        l = Message.ListItem('17', 'http://www.17.media/',
                             'http://i.imgur.com/4loi6PJ.jpg')
        l.set_button(Message.Button(Message.ButtonType.WEB_URL,
                                    title='Goto 17',
                                    url='http://www.17.media/'))
        m.add_list_item(l)
        m.add_button(Message.Button(Message.ButtonType.WEB_URL,
                                    '17', url='http://www.17.media/'))

        facebook.send_message(self.user, [m])

        # Test generic template message
        m = Message()
        bubble = Message.Bubble('Generic template test',
                                'http://www.starwars.com/',
                                'http://i.imgur.com/4loi6PJ.jpg',
                                'Bubble subtitle')
        bubble.add_button(Message.Button(Message.ButtonType.WEB_URL,
                                         'Starwars',
                                         url='http://www.starwars.com/'))
        bubble.add_button(Message.Button(Message.ButtonType.WEB_URL,
                                         'Google',
                                         url='http://www.google.com/'))
        bubble.add_button(Message.Button(Message.ButtonType.WEB_URL,
                                         '17',
                                         url='http://www.17.media/'))
        m.add_bubble(bubble)
        m.add_bubble(bubble)
        m.add_bubble(bubble)

        facebook.send_message(self.user, [m])
Ejemplo n.º 2
0
    def test_message_variable_rendering(self):
        """Test that variable in message can be rendered correctly."""
        variables = {
            'user': {
                'first_name': 'Isaac',
                'last_name': 'Huang',
            },
            'date': 'today'
        }
        m = Message('Hi {{user.first_name}}', variables=variables)
        self.assertEquals(m.as_dict()['text'], 'Hi Isaac')

        m = Message()
        bubble = Message.Bubble('Bubble Test',
                                'http://www.starwars.com/',
                                'http://i.imgur.com/4loi6PJ.jpg',
                                '{{user.first_name}}', variables=variables)
        bubble.add_button(Message.Button(Message.ButtonType.WEB_URL,
                                         '{{user.last_name}}',
                                         url='http://www.starwars.com/',
                                         variables=variables))
        m.add_bubble(bubble)
        bubble = m.as_dict()['attachment']['payload']['elements'][0]

        self.assertEquals(bubble['subtitle'], 'Isaac')
        self.assertEquals(bubble['buttons'][0]['title'], 'Huang')

        # Test error
        wrong_tmpl = 'Hi {{some.key}}'
        m = Message(wrong_tmpl)
        self.assertEquals(m.as_dict()['text'], wrong_tmpl)
Ejemplo n.º 3
0
    def test_Bubble(self):
        b = Message.Bubble('title')
        jsonschema.validate(b.as_dict(), Message.Bubble.schema())
        self.assertEquals(b, b.FromDict(b.as_dict()))

        b = Message.Bubble('title', 'http://test.com/item_url',
                           'http://test.com/image_url', 'subtitle')
        jsonschema.validate(b.as_dict(), Message.Bubble.schema())
        self.assertEquals(b, b.FromDict(b.as_dict()))

        b.add_button(Message.Button(Message.ButtonType.WEB_URL, 'test',
                                    url='http://test.com'))
        b.add_button(Message.Button(Message.ButtonType.POSTBACK, 'test',
                                    payload='payload'))
        jsonschema.validate(b.as_dict(), Message.Bubble.schema())
        self.assertEquals(b, b.FromDict(b.as_dict()))
Ejemplo n.º 4
0
    def test_Message(self):
        but1 = Message.Button(Message.ButtonType.WEB_URL, 'test',
                              url='http://test.com')
        but2 = Message.Button(Message.ButtonType.POSTBACK, 'test',
                              payload='payload')
        b = Message.Bubble('title', 'http://test.com/item_url',
                           'http://test.com/image_url', 'subtitle')
        b.add_button(but1)
        b.add_button(but2)

        m = Message('test')
        jsonschema.validate(m.as_dict(), Message.schema())
        self.assertEquals(m, m.FromDict(m.as_dict()))

        m = Message(image_url='http://test.com/image_url')
        jsonschema.validate(m.as_dict(), Message.schema())
        self.assertEquals(m, m.FromDict(m.as_dict()))

        # Buttons message
        m = Message(buttons_text='question')
        m.add_button(but1)
        m.add_button(but2)
        jsonschema.validate(m.as_dict(), Message.schema())
        self.assertEquals(m, m.FromDict(m.as_dict()))

        transform_keys = reduce(lambda x, y: x + y,
                                [x[0] for x in g.input_transformation], [])
        self.assertTrue('^1$' not in transform_keys)
        self.assertTrue('^2$' in transform_keys)

        # List message
        l = Message.ListItem('title', 'subtitle', 'http://test.com/image_url')
        l.set_button(Message.Button(Message.ButtonType.WEB_URL, 'test',
                                    url='http://test.com'))
        m = Message(top_element_style=Message.ListTopElementStyle.COMPACT)
        m.add_list_item(l)

        with self.assertRaises(jsonschema.ValidationError):
            # Need at least two list items
            jsonschema.validate(m.as_dict(), Message.schema())

        m.add_list_item(l)
        jsonschema.validate(m.as_dict(), Message.schema())

        # Generic message
        g.input_transformation = []
        m = Message()
        m.add_bubble(b)
        m.add_bubble(b)
        jsonschema.validate(m.as_dict(), Message.schema())
        self.assertEquals(m, m.FromDict(m.as_dict()))

        transform_keys = reduce(lambda x, y: x + y,
                                [x[0] for x in g.input_transformation], [])
        self.assertTrue('^1-2$' in transform_keys)
        self.assertTrue('^2-2$' in transform_keys)

        with self.assertRaises(RuntimeError):
            m = Message('test', 'url')