示例#1
0
def test_quick_reply_json():
    '''Tests the to_json() functionality of the QuickReply class.'''

    text_button = buttons.QuickReply('text',
                                     text='Button',
                                     payload='button_pressed',
                                     image_url='http://www.google.com')
    location_button = buttons.QuickReply('location')

    message = messages.Message(text='Choose a button',
                               quick_replies=[text_button, location_button])

    message_json = {
        'text':
        'Choose a button',
        'quick_replies': [{
            'content_type': 'text',
            'title': 'Button',
            'payload': 'button_pressed',
            'image_url': 'http://www.google.com'
        }, {
            'content_type': 'location'
        }]
    }

    assert message.to_json() == message_json
示例#2
0
def test_text_message_json():
    '''Tests the to_json() functionality of the Message class, when it contains
    a text message.'''
    message = messages.Message(text='dummy_text')

    required_json = {'text': 'dummy_text'}

    assert message.to_json() == required_json
示例#3
0
def test_list_template_json():
    '''Tests the to_json() functionality of the ListTemplate class.'''

    url_button = buttons.URLButton('Google', 'http://www.google.com')
    default_action = messages.DefaultAction('https://i.imgur.com/MBUyt0n.png')
    element = messages.Element('Element',
                               sub_title='Element subtitle',
                               image_url='https://i.imgur.com/MBUyt0n.png',
                               default_action=default_action,
                               buttons=[url_button])

    attachment = messages.ListTemplate([element, element], button=url_button)

    message = messages.Message(attachment=attachment)

    element_json = {
        'title':
        'Element',
        'subtitle':
        'Element subtitle',
        'image_url':
        'https://i.imgur.com/MBUyt0n.png',
        'default_action': {
            'type': 'web_url',
            'url': 'https://i.imgur.com/MBUyt0n.png'
        },
        'buttons': [{
            'type': 'web_url',
            'title': 'Google',
            'url': 'http://www.google.com'
        }]
    }

    payload_json = {
        'template_type':
        'list',
        'elements': [element_json, element_json],
        'buttons': [{
            'type': 'web_url',
            'url': 'http://www.google.com',
            'title': 'Google'
        }],
        'top_element_style':
        'compact'
    }

    message_json = {
        'attachment': {
            'type': 'template',
            'payload': payload_json
        }
    }

    assert message.to_json() == message_json
示例#4
0
def test_generic_template_json():
    '''Tests the to_json() functionality of the GenericTemplate class.'''

    url_button = buttons.URLButton('Google', 'http://www.google.com')
    postback_button = buttons.PostbackButton('Postback', 'dummy_payload')
    default_action = messages.DefaultAction('https://i.imgur.com/MBUyt0n.png')

    element = messages.Element('Element',
                               sub_title='Element subtitle',
                               image_url='https://i.imgur.com/MBUyt0n.png',
                               default_action=default_action,
                               buttons=[url_button, postback_button])

    attachment = messages.GenericTemplate([element, element])

    message = messages.Message(attachment=attachment)

    element_json = {
        'title':
        'Element',
        'subtitle':
        'Element subtitle',
        'image_url':
        'https://i.imgur.com/MBUyt0n.png',
        'default_action': {
            'type': 'web_url',
            'url': 'https://i.imgur.com/MBUyt0n.png'
        },
        'buttons': [{
            'type': 'web_url',
            'title': 'Google',
            'url': 'http://www.google.com'
        }, {
            'type': 'postback',
            'payload': 'dummy_payload',
            'title': 'Postback'
        }]
    }

    payload_json = {
        'template_type': 'generic',
        'elements': [element_json, element_json]
    }

    message_json = {
        'attachment': {
            'type': 'template',
            'payload': payload_json
        }
    }

    assert message.to_json() == message_json
示例#5
0
    async def message_received(self, message):
        print('\nReceived message from {0}'.format(message.user_id))
        print('> {0}'.format(message.text))
        text_button = buttons.QuickReply('text',
                                         text='Button',
                                         payload='button_pressed',
                                         image_url='http://www.google.com')
        location_button = buttons.QuickReply('location')
        reply = messages.Message(text='Choose a button',
                                 quick_replies=[text_button, location_button])

        message_id = await self.send(message.user_id, reply)

        print('MID: ' + str(message_id))
示例#6
0
def test_media_attachment_json():
    '''Tests the to_json() functionality of the MediaAttachment class.'''

    attachment = messages.MediaAttachment('image', 'http://www.google.com')
    attachment_json = {
        'type': 'image',
        'payload': {
            'url': 'http://www.google.com'
        }
    }
    message = messages.Message(attachment=attachment)

    assert attachment.to_json() == attachment_json
    assert message.to_json() == {'attachment': attachment_json}
示例#7
0
async def message_received(message):

    logging.info('Handler called')

    # Print the received message
    logging.info('\nReceived message from {0}'.format(message.user_id))
    logging.info('> {0}'.format(message.text))

    # Inform the sender that their message is being processed.
    await app.acknowledge(message)
    logging.info('Acknowledged message')

    # Create a Message object with the text received
    reply = messages.Message(text=message.text)

    # Send the new Message object
    message_id = await app.respond_to(message, reply)
    logging.info('Reply sent with message id: attribute{0}'.format(message_id))
示例#8
0
    async def message_received(self, message):
        print('\nReceived message from {0}'.format(message.user_id))
        print('> {0}'.format(message.text))
        try:
            url_button = buttons.URLButton('Google', 'http://www.google.com')
            postback_button = buttons.PostbackButton('Postback',
                                                     'dummy_payload')
            call_button = buttons.CallButton('Call', '+15105551234')
            print('It is not the buttons')
            attachment = messages.ButtonTemplate(
                'Select a URL', [url_button, postback_button, call_button])
            print('It is not the attachment')
            reply = messages.Message(attachment=attachment)
            print('It is not the reply')

            message_id = await self.send(message.user_id, reply)
            print('It is not the sending')
        except Exception as e:
            print('EXCEPTION: ' + str(e))
        print('MID: ' + str(message_id))
示例#9
0
    async def message_received(self, message):
        print('\nReceived message from {0}'.format(message.user_id))
        print('> {0}'.format(message.text))
        try:
            url_button = buttons.URLButton('Google', 'http://www.google.com')
            postback_button = buttons.PostbackButton('Postback', 'dummy_payload')
            default_action = messages.DefaultAction('https://i.imgur.com/MBUyt0n.png')

            element = messages.Element('Element', sub_title='Element subtitle',
                                    image_url='https://i.imgur.com/MBUyt0n.png',
                                    default_action=default_action,
                                    buttons=[url_button, postback_button])

            attachment = messages.GenericTemplate([element, element])

            reply = messages.Message(attachment=attachment)
            message_id = await self.send(message.user_id, reply)
        except Exception as e:
            print('EXCEPTION: ' + str(e))
        print('MID: ' + str(message_id))
示例#10
0
    async def message_received(self, message):
        try:
            # Print the received message
            print('\nReceived message from {0}'.format(message.user_id))
            print('> {0}'.format(message.text))

            # Inform the sender that their message is being processed.
            # await self.acknowledge(message)

            # Create a Message object with the text received
            # reply = messages.Message(text=message.text)

            attachment = await self.host_attachment('image',
                                                    '/home/cadel/test.JPG')
            print(attachment.url)
            reply = messages.Message(attachment=attachment)
            message_id = await self.respond_to(message, reply)
            print('Reply sent with message id: {0}'.format(message_id))

        except Exception as e:
            print(e)
示例#11
0
def test_button_template_json():
    '''Tests the to_json() functionality of the ButtonTemplate class.'''

    url_button = buttons.URLButton('Google', 'http://www.google.com')
    postback_button = buttons.PostbackButton('Postback', 'dummy_payload')
    call_button = buttons.CallButton('Call', '+15105551234')
    attachment = messages.ButtonTemplate(
        'Select a URL', [url_button, postback_button, call_button])
    message = messages.Message(attachment=attachment)

    payload_json = {
        'template_type':
        'button',
        'text':
        'Select a URL',
        'buttons': [{
            'type': 'web_url',
            'url': 'http://www.google.com',
            'title': 'Google'
        }, {
            'type': 'postback',
            'payload': 'dummy_payload',
            'title': 'Postback'
        }, {
            'type': 'phone_number',
            'payload': '+15105551234',
            'title': 'Call'
        }]
    }

    message_json = {
        'attachment': {
            'type': 'template',
            'payload': payload_json
        }
    }

    assert message.to_json() == message_json
示例#12
0
def test_button_template_json_share():
    '''Tests the to_json() functionality of the ButtonTemplate class,
    when a ShareButton is present.'''

    share_button = buttons.ShareButton()
    attachment = messages.ButtonTemplate('Select a URL', [share_button])
    message = messages.Message(attachment=attachment)

    payload_json = {
        'template_type': 'button',
        'text': 'Select a URL',
        'buttons': [{
            'type': 'element_share'
        }]
    }

    message_json = {
        'attachment': {
            'type': 'template',
            'payload': payload_json
        }
    }

    assert message.to_json() == message_json
示例#13
0
def test_invalid_message():
    '''Ensures that Message objects require either a text message or Attachment
    when being initialised.'''
    with pytest.raises(exceptions.BoomerangException):
        messages.Message()