Exemplo 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])
Exemplo n.º 2
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')
Exemplo n.º 3
0
    def test_ListItem(self):
        l = Message.ListItem('title', 'subtitle', 'http://test.com/image_url')
        jsonschema.validate(l.as_dict(), Message.ListItem.schema())
        self.assertEquals(l, l.FromDict(l.as_dict()))

        l.set_default_action(Message.Button(Message.ButtonType.WEB_URL, 'test',
                                            url='http://test.com'))
        l.set_button(Message.Button(Message.ButtonType.WEB_URL, 'test',
                                    url='http://test.com'))
        jsonschema.validate(l.as_dict(), Message.ListItem.schema())
        self.assertEquals(l, l.FromDict(l.as_dict()))