Exemplo n.º 1
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.º 2
0
def broadcast_task(broadcast_id):
    with DatabaseSession():
        broadcast = Broadcast.get_by(id=broadcast_id, single=True)
        # The broadcast entry could be delted by user
        if not broadcast:
            return

        # Either broadcast in progress or already sent
        if broadcast.status != BroadcastStatusEnum.QUEUED:
            return

        # The user may have changed the broadcasting time, ignore it as it
        # should be automatically rescheduled.
        if broadcast.scheduled_time >= datetime.now():
            return

        broadcast.status = BroadcastStatusEnum.SENDING
        try:
            DatabaseManager.commit()
        except (InvalidRequestError, IntegrityError):
            # The broadcast task have changed during our modification, retry.
            DatabaseManager.rollback()
            return broadcast_task(broadcast_id)

        # Do the actually broadcast
        bot = Bot.get_by(id=broadcast.bot_id,
                         account_id=broadcast.account_id,
                         single=True)

        # Bot may have been deleted
        if not bot:
            return

        messages = [Message.FromDict(m) for m in broadcast.messages]
        broadcast_message_async(bot, messages)
Exemplo n.º 3
0
    def test_input_transformation_reconstruction(self):
        """Test that input transformation is applied when constructing message
        with Message.FromDict."""

        msg_dict = {
            "text": "test",
            "quick_replies": [
                {
                    "content_type": "text",
                    "title": "A",
                },
                {
                    "content_type": "text",
                    "title": "B"
                },
                {
                    "content_type": "location",
                }
            ]
        }

        g.input_transformation = []
        Message.FromDict(msg_dict)
        transform_keys = reduce(lambda x, y: x + y,
                                [x[0] for x in g.input_transformation], [])
        self.assertTrue('^A$' in transform_keys)
        self.assertTrue('^B$' in transform_keys)
        self.assertFalse(None in transform_keys)
Exemplo n.º 4
0
    def test_QuickReply(self):
        q1 = Message.QuickReply(Message.QuickReplyType.TEXT, 'quick_reply_1',
                                acceptable_inputs=['1', '2'])
        jsonschema.validate(q1.as_dict(), Message.QuickReply.schema())
        self.assertEquals(q1, q1.FromDict(q1.as_dict()))

        q2 = Message.QuickReply(Message.QuickReplyType.LOCATION)
        jsonschema.validate(q2.as_dict(), Message.QuickReply.schema())
        self.assertEquals(q2, q2.FromDict(q2.as_dict()))

        q3 = Message.QuickReply(Message.QuickReplyType.TEXT, 'quick_reply_2',
                                acceptable_inputs=['3', '4'])

        m = Message('test')
        m.add_quick_reply(q1)
        m.add_quick_reply(q2)
        m.add_quick_reply(q3)

        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('^quick_reply_1$' in transform_keys)
        self.assertTrue('1' in transform_keys)
        self.assertTrue('3' in transform_keys)
Exemplo n.º 5
0
    def Broadcast(self, request, unused_context):
        with DatabaseSession():
            bot = Bot.get_by(id=request.bot_id, single=True)
            if not bot:
                raise RuntimeError('Bot<%d> does not exist' % request.bot_id)

            eta = None if request.eta == 0 else request.eta
            messages_dict = cPickle.loads(request.messages_object)
            if request.static:
                msgs = [Message.FromDict(m, {}) for m in messages_dict]
                messaging_tasks.broadcast_message_async(bot, msgs, eta)
            else:
                users = User.get_by(bot_id=request.bot_id)
                messaging_tasks.push_message_from_dict_async(
                    users, messages_dict, eta)

        return app_service_pb2.Empty()