def test_element(self): weburl = Button('web_url', "Button title", "www.test.com") payload = Button('postback', "Button title2", "USR_DEFINE") button_list = [weburl, payload] element = Element("Grey Shirt", "soft shirt", "www.greyshirt.com", button_list) assert element.title == "Grey Shirt", "Element title error" assert element.image_url == "www.greyshirt.com", "Element URL error" assert element.subtitle == "soft shirt", "Element subtitle error" assert element.buttons == [weburl, payload], "Element buttons error"
def test_constructor(self): weburl = Button('web_url', "Button title", "www.test.com") assert weburl.type == "web_url", 'WebUrl type failed' assert weburl.title == "Button title", "WebUrl title failed" assert weburl.payload == "www.test.com", "WebUrl URL failed" payload = Button('postback', "Button title2", "USR_DEFINE") assert payload.type == "postback", "Payload type failed" assert payload.title == "Button title2", "Button title failed" assert payload.payload == "USR_DEFINE", "Button payload failed"
def test_element_json(self): weburl = Button('web_url', "Button title", "www.test.com") payload = Button('postback', "Button title2", "USR_DEFINE") button_list = [weburl, payload] element = Element("Grey Shirt", "soft shirt", "www.greyshirt.com", button_list) element_json = element.to_json() assert element_json['title'] == "Grey Shirt" assert element_json['image_url'] == "www.greyshirt.com" assert element_json['subtitle'] == "soft shirt" assert weburl.to_json() in element_json['buttons'] assert payload.to_json() in element_json['buttons']
def test_to_json(self): weburl = Button('web_url', "Button title", "www.test.com") payload = Button('postback', "Button title2", "USR_DEFINE") webjson = weburl.to_json() assert webjson['type'] == "web_url", 'WebUrl type failed' assert webjson['title'] == "Button title", "WebUrl title failed" assert webjson['url'] == "www.test.com", "WebUrl URL failed" payloadjson = payload.to_json() assert payloadjson['type'] == "postback", "Payload type failed" assert payloadjson['title'] == "Button title2", "Button title failed" assert payloadjson['payload'] == "USR_DEFINE", "Button payload failed"
def test_button_template_button(self): buttons = [Button('web_url', 'title', 'url') for _ in range(3)] message = Template( Template.button_type, buttons=buttons, title='title', ) payload = message.to_json() assert payload['template_type'] == 'button' assert payload['text'] == 'title' assert payload['buttons'][0]['title'] == 'title' assert len(payload['buttons']) == 3
def test_generic_template(self): title = 't' img_url = 'www.e.com' subtitle = 's' buttons = [Button('web_url', 'title', 'url') for i in range(3)] largs = [title, img_url, subtitle, buttons] elements = [Element(title, img_url, subtitle, buttons)] message = Template( Template.generic_type, elements=elements, ) payload = message.to_json() assert payload['template_type'] == 'generic' assert payload['elements'][0]['title'] == 't'
def test_send_button_template_message(self): httpretty.register_uri(httpretty.POST, FACEBOOK_MESSAGES_POST_URL + '/me/messages', body='{ \ "recipient_id": "1008", \ "message_id": "mid.1" \ }', status=201) bot = Bot("abc") buttons = [] for i in range(3): buttons.append(Button('web_url', str(i), "http://ex.com")) message = Template( Template.button_type, title='Title', buttons=buttons, ) bot.send_message(message, '123abc')
def webhook(): # For webhook verification when registering app if request.args.get("hub.verify_token") == 'test_token': return request.args.get("hub.challenge") # Recieve a list of available messages msgs = bot.messages_for_request(request) for m in msgs: text = m.text # Retrieve what user sent recipient = m.sender # Retrieve who sent it if text == 'template': web_button = Button( type='web_url', title='My Button Image', payload='http://www.newton.ac.uk/files/covers/968361.jpg') postback_button = Button( type='postback', title='My Postback', payload="<USER DEFINED PAYLOAD>", ) element = Element( title='Generic Template Element Title', subtitle='My subtitle', image_url='http://www.buffettworld.com/images/news_trump.jpg', buttons=[ web_button, ]) generic_template = Template(Template.generic_type, elements=[ element, ]) button_template = Template(Template.button_type, title='My Button template title', buttons=[ web_button, postback_button, ]) msg = Message('template', button_template) response, error = bot.send_message(msg, recipient) msg2 = Message('template', generic_template) response, error = bot.send_message(msg2, recipient) elif text == 'receipt': element = ReceiptElement( title='My Title', subtitle='A very good subtitle', quantity='15', price=1999, currency='CAD', image_url='http://www.newton.ac.uk/files/covers/968361.jpg') receipt = Template( Template.receipt_type, recipient_name='A name', order_number='12345678902', currency='CAD', payment_method='Visa', order_url='http://petersapparel.com/order?order_id=123456', elements=[ element, ], address={ 'street_1': '1 Hacker Way', 'city': 'Vancouver', 'state': 'BC', 'country': 'CA', 'postal_code': '1A1A1A' }, summary={ 'subtotal': 75.00, 'shipping_cost': 4.95, 'total_tax': 6.19, 'total_cost': 56.14 }) msg = Message('template', receipt) response, error = bot.send_message(msg, recipient) else: payload = text # Create image message msg = Message('image', 'http://www.buffettworld.com/images/news_trump.jpg') # Create audio message msg = Message('audio', 'http://www.stephaniequinn.com/Music/Canon.mp3') # Create video message msg = Message('video', 'http://clips.vorwaerts-gmbh.de/VfE_html5.mp4') # Create file message msg = Message('file', 'http://www.pdf995.com/samples/pdf.pdf') # Echo back to user msg = Message('text', payload) # Send text message quick = QuickReply( 'text', title="Click One", payload="", url='http://www.buffettworld.com/images/news_trump.jpg') quick2 = QuickReply('location') msg = Message('quick', "My msg text", quick_replies=[ quick, quick2, ]) bot.send_message(msg, recipient) return 'OK'
msg = Message('quick', "My msg text", quick_replies=[ quick, quick2, ]) bot.send_message(msg, recipient) return 'OK' if __name__ == "__main__": app.debug = True web_button = Button( type='web_url', title='My Button Image', payload='http://www.newton.ac.uk/files/covers/968361.jpg') postback_button = Button( type='postback', title='My Postback', payload="My defined payload string", ) bot.set_welcome("Welcome to PyBot!") bot.set_persistent_menu([ web_button, postback_button, ]) app.run(port=8000)