Beispiel #1
0
def send_user_subscribe_msg(user_id):
    if user_id is None:
        app.logger.warn('Trying to send a subscribe message without a user_id')
        return None

    # quick_replies = QuickReplies([
    #     QuickReply(content_type="text",
    #                title="Yes",
    #                payload=f"register {user_id}", ),
    #     QuickReply(content_type="text",
    #                title="No",
    #                payload=f"ignore {user_id}", ),
    # ])
    # msg = elements.DynamicText(text='Do you want to get daily quotes?',
    #                            quick_replies=quick_replies)
    btn1 = elements.Button(button_type='postback',
                           title='Yes',
                           payload=f"register {user_id}")
    btn2 = elements.Button(button_type='postback',
                           title='No',
                           payload=f"ignore {user_id}")
    elems = elements.Element(title='Do you want to get daily quotes',
                             buttons=[btn1, btn2])
    msg = templates.GenericTemplate(elements=[elems])
    return messenger.send_msg(msg, user_id)
Beispiel #2
0
    def test_element(self):
        btn = elements.Button(button_type='web_url',
                              title='Web button',
                              url='http://facebook.com')
        default = elements.Button(button_type='web_url',
                                  url='https://facebook.com')
        res = elements.Element(title='Element',
                               item_url='http://facebook.com',
                               image_url='http://facebook.com/image.jpg',
                               subtitle='Subtitle',
                               default_action=default,
                               buttons=[btn])

        expected = {
            'title':
            'Element',
            'item_url':
            'http://facebook.com',
            'image_url':
            'http://facebook.com/image.jpg',
            'subtitle':
            'Subtitle',
            'default_action': {
                'type': 'web_url',
                'url': 'https://facebook.com'
            },
            'buttons': [{
                'type': 'web_url',
                'title': 'Web button',
                'url': 'http://facebook.com'
            }]
        }
        assert expected == res.to_dict()
Beispiel #3
0
 def test_button_template_with_multiple_buttons(self):
     btn = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com')
     btn2 = elements.Button(button_type='postback',
                            title='Postback button',
                            payload='payload')
     res = templates.ButtonTemplate(text='Button template',
                                    buttons=[btn, btn2])
     expected = {
         'attachment': {
             'type': 'template',
             'payload': {
                 'template_type':
                 'button',
                 'text':
                 'Button template',
                 'buttons': [{
                     'type': 'web_url',
                     'title': 'Web button',
                     'url': 'http://facebook.com'
                 }, {
                     'type': 'postback',
                     'title': 'Postback button',
                     'payload': 'payload'
                 }]
             }
         }
     }
     assert expected == res.to_dict()
Beispiel #4
0
 def test_generic_template_with_single_element(self):
     btn = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com')
     elems = elements.Element(title='Element',
                              item_url='http://facebook.com',
                              image_url='http://facebook.com/image.jpg',
                              subtitle='Subtitle',
                              buttons=[btn])
     res = templates.GenericTemplate(elements=elems)
     expected = {
         'attachment': {
             'type': 'template',
             'payload': {
                 'template_type':
                 'generic',
                 'elements': [{
                     'title':
                     'Element',
                     'item_url':
                     'http://facebook.com',
                     'image_url':
                     'http://facebook.com/image.jpg',
                     'subtitle':
                     'Subtitle',
                     'buttons': [{
                         'type': 'web_url',
                         'title': 'Web button',
                         'url': 'http://facebook.com'
                     }]
                 }]
             }
         }
     }
     assert expected == res.to_dict()
Beispiel #5
0
 def test_media_template(self):
     btn = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com')
     attachment = attachments.Image(attachment_id='12345')
     res = templates.MediaTemplate(attachment, buttons=[btn])
     expected = {
         'attachment': {
             'type': 'template',
             'payload': {
                 'template_type':
                 'media',
                 'elements': [{
                     'media_type':
                     'image',
                     'attachment_id':
                     '12345',
                     'buttons': [{
                         'type': 'web_url',
                         'title': 'Web button',
                         'url': 'http://facebook.com'
                     }]
                 }]
             }
         }
     }
     assert expected == res.to_dict()
Beispiel #6
0
 def test_button_webview_height_ratio_validation(self):
     with pytest.raises(ValueError) as err:
         elements.Button(button_type='web_url',
                         title='Button',
                         url='http://facebook.com',
                         webview_height_ratio='wrong')
     assert str(err.value) == 'Invalid webview_height_ratio provided.'
Beispiel #7
0
 def test_generic_template_with_quick_replies(self):
     btn = elements.Button(
         button_type='web_url',
         title='Web button',
         url='http://facebook.com'
     )
     elems = elements.Element(
         title='Element',
         item_url='http://facebook.com',
         image_url='http://facebook.com/image.jpg',
         subtitle='Subtitle',
         buttons=[
             btn
         ]
     )
     qr = quick_replies.QuickReply(title='QR', payload='QR payload')
     qrs = quick_replies.QuickReplies(quick_replies=[qr] * 2)
     res = templates.GenericTemplate(
         elements=[elems],
         quick_replies=qrs
     )
     expected = {
         'attachment': {
             'type': 'template',
             'payload': {
                 'template_type': 'generic',
                 'sharable': False,
                 'elements': [
                     {
                         'title': 'Element',
                         'item_url': 'http://facebook.com',
                         'image_url': 'http://facebook.com/image.jpg',
                         'subtitle': 'Subtitle',
                         'buttons': [
                             {
                                 'type': 'web_url',
                                 'title': 'Web button',
                                 'url': 'http://facebook.com'
                             }
                         ]
                     }
                 ],
             },
         },
         'quick_replies': [
             {
                 'content_type': 'text',
                 'title': 'QR',
                 'payload': 'QR payload'
             },
             {
                 'content_type': 'text',
                 'title': 'QR',
                 'payload': 'QR payload'
             },
         ],
     }
     assert expected == res.to_dict()
Beispiel #8
0
    def test_share_button(self):
        btn = elements.Button(button_type='web_url',
                              title='Web button',
                              url='http://facebook.com')
        element = elements.Element(title='Element',
                                   item_url='http://facebook.com',
                                   image_url='http://facebook.com/image.jpg',
                                   subtitle='Subtitle',
                                   buttons=[btn])
        template = GenericTemplate(elements=[element], )

        res = elements.Button(button_type='element_share',
                              share_contents=template.to_dict())
        expected = {
            'type': 'element_share',
            'share_contents': {
                'attachment': {
                    'type': 'template',
                    'payload': {
                        'template_type':
                        'generic',
                        'sharable':
                        False,
                        'elements': [
                            {
                                'title':
                                'Element',
                                'item_url':
                                'http://facebook.com',
                                'image_url':
                                'http://facebook.com/image.jpg',
                                'subtitle':
                                'Subtitle',
                                'buttons': [{
                                    'type': 'web_url',
                                    'title': 'Web button',
                                    'url': 'http://facebook.com'
                                }]
                            },
                        ]
                    }
                }
            }
        }
        assert expected == res.to_dict()
Beispiel #9
0
 def test_button_template_with_too_many_buttons(self):
     btn = elements.Button(button_type='web_url', title='Web button', url='http://facebook.com')
     with pytest.raises(ValueError) as err:
         res = templates.ButtonTemplate(
             text='Button template',
             buttons=[btn]*4,
         )
         res.to_dict()
     assert str(err.value) == 'You cannot have more than 3 buttons in the template.'
Beispiel #10
0
 def test_default_action_validation(self):
     defaults = [(elements.Button(button_type='web_url',
                                  url='https://facebook.com',
                                  title='Facebook'),
                  'The default_action button may not have a title'),
                 (elements.Button(button_type='postback', payload='foo'),
                  'The default_action button must be of type web_url')]
     btn = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com')
     for default, msg in defaults:
         with pytest.raises(ValueError) as err:
             elements.Element(title='Element',
                              item_url='http://facebook.com',
                              image_url='http://facebook.com/image.jpg',
                              subtitle='Subtitle',
                              default_action=default,
                              buttons=[btn])
         assert str(err.value) == msg
Beispiel #11
0
 def test_button_with_title_over_limit(self, caplog):
     with caplog.at_level(logging.WARNING, logger='fbmessenger.elements'):
         res = elements.Button(button_type='web_url',
                               title='This button text is over the limit',
                               url='http://facebook.com')
         res.to_dict()
         assert caplog.record_tuples == [
             ('fbmessenger.elements', logging.WARNING,
              CHARACTER_LIMIT_MESSAGE.format(field='Title', maxsize=20))
         ]
Beispiel #12
0
 def test_web_button(self):
     res = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com')
     expected = {
         'type': 'web_url',
         'title': 'Web button',
         'url': 'http://facebook.com'
     }
     assert expected == res.to_dict()
Beispiel #13
0
 def test_postback_button(self):
     res = elements.Button(button_type='postback',
                           title='Postback button',
                           payload='payload')
     expected = {
         'type': 'postback',
         'title': 'Postback button',
         'payload': 'payload'
     }
     assert expected == res.to_dict()
Beispiel #14
0
 def test_list_template(self):
     btn = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com')
     elems = elements.Element(title='Element',
                              image_url='http://facebook.com/image.jpg',
                              subtitle='Subtitle',
                              buttons=[btn])
     res = templates.ListTemplate(
         elements=[elems] * 2,
         buttons=[btn],
         top_element_style='large',
     )
     expected = {
         'attachment': {
             'type': 'template',
             'payload': {
                 'template_type':
                 'list',
                 'top_element_style':
                 'large',
                 'elements': [{
                     'title':
                     'Element',
                     'image_url':
                     'http://facebook.com/image.jpg',
                     'subtitle':
                     'Subtitle',
                     'buttons': [{
                         'type': 'web_url',
                         'title': 'Web button',
                         'url': 'http://facebook.com'
                     }]
                 }, {
                     'title':
                     'Element',
                     'image_url':
                     'http://facebook.com/image.jpg',
                     'subtitle':
                     'Subtitle',
                     'buttons': [{
                         'type': 'web_url',
                         'title': 'Web button',
                         'url': 'http://facebook.com'
                     }]
                 }],
                 'buttons': [{
                     'type': 'web_url',
                     'title': 'Web button',
                     'url': 'http://facebook.com'
                 }],
             }
         }
     }
     assert expected == res.to_dict()
Beispiel #15
0
    def message(self, message):
        print('message')
        print(message)                
        get_sender = Text(text= str(message['sender']['id']))
        get_text = Text(text= str(message['message']['text']))
        try:
        	chck_payload = Text(text= str(message['message']['quick_reply']['payload']))
        except KeyError:
        	chck_payload = 'text'
        text = get_text.to_dict()
        text = text['text']
        check = self.read(text, chck_payload)
        print(check)
        # problem hereeeee
        if check != 2:
        	print('check')
        	response = self.movie_route()
        	if self.questions_check[response[0]] == 'quick':
        		print(response)
        		quick_reply = [quick_replies.QuickReply(title=i, payload='quick') for i in response[1]]
        		quick_reply += [quick_replies.QuickReply(title='Go back', payload='quick')]
        		quick_replies_set = quick_replies.QuickReplies(quick_replies=quick_reply)
        		text = { 'text': response[0]}
        		text['quick_replies'] = quick_replies_set.to_dict()
        		self.send(text, 'RESPONSE')
        	else:
        		text = {'text': response[0]}
        		self.send(text, 'RESPONSE')
        		print('carousel')
        		print(self.movies)
        		elems  = []
        		i = 0
        		while i < len(self.movies[0]):
        			btn = elements.Button(title='Read More', payload=movies[2][i], button_type='postback')
        			elem = elements.Element(
        				title=self.movies[2][i],
        				image_url=self.movies[0][i],
        				subtitle=self.movies[1][i],
        				buttons=[
        					btn
        				]
        				)
        			elems.append(elem)
        			i+=1
        		res = templates.GenericTemplate(elements=elems)        		
        		messenger.send(res.to_dict(), 'RESPONSE')
	        	quick_reply = [quick_replies.QuickReply(title='Go back', payload='quick')]
	        	quick_replies_set = quick_replies.QuickReplies(quick_replies=quick_reply)
	        	default = {'quick_replies': quick_replies_set.to_dict()}
	        	self.send(default, 'RESPONSE')
        		print(self.movie_responses['Movies'])
Beispiel #16
0
 def test_url_button(self):
     res = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com',
                           webview_height_ratio='full',
                           messenger_extensions=True,
                           fallback_url='https://facebook.com')
     expected = {
         'type': 'web_url',
         'title': 'Web button',
         'url': 'http://facebook.com',
         'webview_height_ratio': 'full',
         'messenger_extensions': 'true',
         'fallback_url': 'https://facebook.com'
     }
     assert expected == res.to_dict()
Beispiel #17
0
 def test_template_with_too_many_elements(self):
     btn = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com')
     elem = elements.Element(title='Element',
                             item_url='http://facebook.com',
                             image_url='http://facebook.com/image.jpg',
                             subtitle='Subtitle',
                             buttons=[btn])
     elem_list = [elem] * 11
     with pytest.raises(ValueError) as err:
         res = templates.GenericTemplate(elements=elem_list)
         res.to_dict()
     assert str(
         err.value
     ) == 'You cannot have more than 10 elements in the template.'
Beispiel #18
0
 def test_element_subtitle_validation(self, caplog):
     with caplog.at_level(logging.WARNING, logger='fbmessenger.elements'):
         btn = elements.Button(button_type='web_url',
                               title='Web button',
                               url='http://facebook.com')
         res = elements.Element(
             title='Title',
             item_url='http://facebook.com',
             image_url='http://facebook.com/image.jpg',
             subtitle='The subtitle is too long and should throw an error.'
             'The maximum allowed number of characters is 80',
             buttons=[btn])
         res.to_dict()
         assert caplog.record_tuples == [
             ('fbmessenger.elements', logging.WARNING,
              CHARACTER_LIMIT_MESSAGE.format(field='Subtitle', maxsize=80))
         ]
Beispiel #19
0
 def test_button_template_with_single_button(self):
     btn = elements.Button(button_type='web_url',
                           title='Web button',
                           url='http://facebook.com')
     res = templates.ButtonTemplate(text='Button template', buttons=btn)
     expected = {
         'attachment': {
             'type': 'template',
             'payload': {
                 'template_type':
                 'button',
                 'text':
                 'Button template',
                 'buttons': [{
                     'type': 'web_url',
                     'title': 'Web button',
                     'url': 'http://facebook.com'
                 }]
             }
         }
     }
     assert expected == res.to_dict()
Beispiel #20
0
 def test_button_type_validation(self):
     with pytest.raises(ValueError) as err:
         elements.Button(button_type='incorrect',
                         title='Button',
                         url='http://facebook.com')
     assert str(err.value) == 'Invalid button_type provided.'