def test_invalid_list_template(): '''Tests the error-checking in 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]) # Between 2 and 4 elements must be provided with pytest.raises(exceptions.BoomerangException): list_template = messages.ListTemplate([]) with pytest.raises(exceptions.BoomerangException): list_template = messages.ListTemplate( [element, element, element, element, element]) # Ensure that the first element of a ListTemplate in non-compact form # contains an image imageless_element = messages.Element('Element', default_action=default_action, buttons=[url_button]) with pytest.raises(exceptions.BoomerangException): messages.ListTemplate([imageless_element, element], compact=False)
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
def test_generic_template_element_limit(): '''Ensures that GenericTemplate raises an exception when the number of elements is greater than 10.''' 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) with pytest.raises(exceptions.BoomerangException): messages.GenericTemplate([element] * 11)
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
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))