def test_buttons(self): btns1 = Template.Buttons(text="Title", buttons=[ { 'type': 'web_url', 'title': 'title', 'value': 'https://test.com' }, { 'type': 'postback', 'title': 'title', 'value': 'TEST_PAYLOAD' }, { 'type': 'phone_number', 'title': 'title', 'value': '+82108011' }, { 'type': 'element_share' }, ]) btns2 = Template.Buttons( text="Title", buttons=[ Template.ButtonWeb(title="title", url="https://test.com"), Template.ButtonPostBack(title="title", payload="TEST_PAYLOAD"), Template.ButtonPhoneNumber(title="title", payload="+82108011"), Template.ButtonShare() ]) self.assertEquals(utils.to_json(btns1), utils.to_json(btns2))
def webhook(): page.greeting( "Bem vindo, a nossa loja de produtos recreativos, por favor, pergunte-me algo!" ) #Info da pagina que o bot disponibiliza page.show_starting_button( "START_PAYLOAD" ) #Butão de começar que me deu uma dor de cabeça do clrh page.show_persistent_menu([ Template.ButtonWeb('Website', 'https://www.leafly.com/'), Template.ButtonWeb('Suprasumo da sapiência', 'https://darioquental.herokuapp.com/get_image') ]) #Mostra um menu todo pimposo ao lado payload = request.get_data( as_text=True) #Faz um request ao webhook e obtem a data print(payload) #Cria o log # Processa msg page.handle_webhook(payload) return "ok", 200
def test_persistent_menu(self): exp = """ { "persistent_menu": [ { "locale":"default", "call_to_actions": [ { "type": "postback", "title": "yes", "payload": "hobbang" }, { "type": "web_url", "title": "url", "url": "url" }, { "type": "postback", "title": "ho", "payload": "bbang" } ] } ] } """ with MessengerAPIMock(subpath="messenger_profile", expected=exp, utest=self) as m: self.page.show_persistent_menu([{ 'type': 'postback', 'title': 'yes', 'payload': 'hobbang' }, { 'type': 'web_url', 'title': 'url', 'value': 'url' }, Template.ButtonPostBack( 'ho', 'bbang')]) with MessengerAPIMock(subpath="messenger_profile", expected=exp): with self.assertRaises(ValueError): self.page.show_persistent_menu("hi") with self.assertRaises(ValueError): self.page.show_persistent_menu([ Template.ButtonPhoneNumber('ho', 'bbang'), Template.ButtonWeb('title', 'url') ]) with self.assertRaises(ValueError): self.page.show_persistent_menu([{'type': 'ho'}])
def test_button_shortcut(self): btns = Template.Buttons.convert_shortcut_buttons([ { 'type': 'web_url', 'title': 'title', 'value': 'https://test.com' }, { 'type': 'postback', 'title': 'title', 'value': 'TEST_PAYLOAD' }, { 'type': 'phone_number', 'title': 'title', 'value': '+82108011' }, Template.ButtonWeb(title="title", url="https://test.com"), ]) self.assertEquals( '[{"title": "title", "type": "web_url", "url": "https://test.com"},' ' {"payload": "TEST_PAYLOAD", "title": "title", "type": "postback"},' ' {"payload": "+82108011", "title": "title", "type": "phone_number"},' ' {"title": "title", "type": "web_url", "url": "https://test.com"}]', utils.to_json(btns)) with self.assertRaises(ValueError) as context: Template.Buttons.convert_shortcut_buttons([{ 'type': 'url', 'title': 'title', 'value': 'https://test.com' }]) with self.assertRaises(ValueError) as context: Template.Buttons.convert_shortcut_buttons(['hello']) self.assertEquals(None, Template.Buttons.convert_shortcut_buttons(None))
def test_button_web(self): btn = Template.ButtonWeb(title="title", url="https://test.com") self.assertEquals( '{"title": "title", "type": "web_url", "url": "https://test.com"}', utils.to_json(btn)) print(utils.to_json(btn))
def test_persistent_menu(self): self.page.show_persistent_menu([{ 'type': 'postback', 'title': 'yes', 'payload': 'hobbang' }, { 'type': 'web_url', 'title': 'url', 'value': 'url' }, Template.ButtonPostBack('ho', 'bbang')]) self.page._send_thread_settings.assert_called_with( json.dumps({ "setting_type": "call_to_actions", "thread_state": "existing_thread", "call_to_actions": [{ 'type': 'postback', 'title': 'yes', 'payload': 'hobbang' }, { 'type': 'web_url', 'title': 'url', 'url': 'url' }, { 'type': 'postback', 'title': 'ho', 'payload': 'bbang' }] })) self.page.show_persistent_menu([ Template.ButtonPostBack('ho', 'bbang'), Template.ButtonWeb('title', 'url') ]) self.page._send_thread_settings.assert_called_with( json.dumps({ "setting_type": "call_to_actions", "thread_state": "existing_thread", "call_to_actions": [{ 'type': 'postback', 'title': 'ho', 'payload': 'bbang' }, { 'type': 'web_url', 'title': 'title', 'url': 'url' }] })) with self.assertRaises(ValueError): self.page.show_persistent_menu("hi") with self.assertRaises(ValueError): self.page.show_persistent_menu([ Template.ButtonPhoneNumber('ho', 'bbang'), Template.ButtonWeb('title', 'url') ]) with self.assertRaises(ValueError): self.page.show_persistent_menu([{'type': 'ho'}]) self.page.hide_persistent_menu() self.page._send_thread_settings.assert_called_with( json.dumps({ "setting_type": "call_to_actions", "thread_state": "existing_thread" }))