def get_results_attachment(listings_data_items, more_url=None): tpl_elements = list() for listing_d in listings_data_items: buttons = list() if more_url: buttons.append( elements.WebUrlButton( title='View more', url=more_url, )) buttons.append( elements.WebUrlButton( title='View this listing', url=listing_d['listing_url'], )) element_kwargs = dict(title=Truncator(listing_d['title']).chars(44), subtitle=Truncator( listing_d['fmt_address']).chars(79), buttons=buttons) image_url = listing_d.get('image_url', None) if image_url: element_kwargs['image_url'] = image_url element = elements.Element(**element_kwargs) tpl_elements.append(element) template = templates.GenericTemplate(tpl_elements) return attachments.TemplateAttachment(template=template)
def test_generic_template(self): element = elements.Element( title='Classic White T-Shirt', image_url='http://petersapparel.parseapp.com/img/item100-thumb.png', subtitle='Soft white cotton t-shirt is back in style', buttons=[ elements.WebUrlButton( title='View Item', url= 'https://petersapparel.parseapp.com/view_item?item_id=100', ) ]) template = templates.GenericTemplate([element]) attachment = attachments.TemplateAttachment(template) message = messages.Message(attachment=attachment) request = messages.MessageRequest(self.recipient, message) # Assert we can serialise self.assertIsInstance(request.serialise(), str)
def sendFBMessage(chat_id,from_user,msgs,suggested_responses=[],msg_type=None,extras=[]): # TODO: if suggested_response, create object to handle choices # if suggested responses > 3 recipient = fbmessages.Recipient(recipient_id=from_user) if msg_type == 'text': if not suggested_responses: text_messages = msgs else: text_messages = msgs[:-1] for msg in text_messages: message = fbmessages.Message(text=msg) request = fbmessages.MessageRequest(recipient, message) fbmessenger.send(request) if suggested_responses: buttons = [] for suggested_response in suggested_responses: buttons.append(elements.PostbackButton( title=suggested_response, payload=suggested_response )) template = templates.ButtonTemplate( text=msgs[-1], buttons=buttons ) attachment = attachments.TemplateAttachment(template=template) message = fbmessages.Message(attachment=attachment) request = fbmessages.MessageRequest(recipient, message) fbmessenger.send(request) if msg_type == 'image': elmnts = [] for i, url in enumerate(msgs): buttons = [] buttons.append(elements.PostbackButton( title="I like this one", payload="I like number {}st".format(i+1) )) # WEBSITE DOWN # buttons.append(elements.WebUrlButton( # title="See on gofind site", # url="gofindfashion.com/?{}".format(url) # )) buttons.append(elements.PostbackButton( title="See more results", payload="See more like this" )) title = suggested_responses[i] element = elements.Element(title=title[:43],image_url=url,buttons=buttons) elmnts.append(element) template = templates.GenericTemplate(elements=elmnts) attachment = attachments.TemplateAttachment(template=template) message = fbmessages.Message(attachment=attachment) request = fbmessages.MessageRequest(recipient, message) fbmessenger.send(request) if msg_type == 'link': for i, url in enumerate(msgs): elmnts = [] buttons = [] buttons.append(elements.WebUrlButton( title="Go to store", url=url )) buttons.append(elements.PostbackButton( title="Search with this pic", payload="Search with this pic" )) buttons.append(elements.PostbackButton( title="See more results", payload="See more like this" )) title = suggested_responses[i] element = elements.Element(title=title[:43],image_url=extras[i],buttons=buttons) elmnts.append(element) template = templates.GenericTemplate(elements=elmnts) attachment = attachments.TemplateAttachment(template=template) message = fbmessages.Message(attachment=attachment) request = fbmessages.MessageRequest(recipient, message) fbmessenger.send(request)
def webhook(request): if request.GET: if request.GET['hub.verify_token'] == '': return HttpResponse(request.GET['hub.challenge']) else: return HttpResponse('Error, invalid token') try: output = json.loads(request.body.decode('utf-8')) print(output) try: for event in output['entry']: messaging = event['messaging'] for x in messaging: recipient_id = x['sender']['id'] recipient = messages.Recipient(recipient_id=recipient_id) if x.get('message'): # collect text if x['message'].get('text'): if x['message'].get('text').lower( ) == 'hello' or x['message'].get( 'text').lower() == 'hi': message_text = x['message']['text'] # ask if they want to buy something # message = messages.Message(text='Hello, do you wish to buy or sell a product?') # request = messages.MessageRequest(recipient, message) # messenger.send(request) postback_button = elements.PostbackButton( title='Buy', payload='BUY') postback_button1 = elements.PostbackButton( title='Sell', payload='SELL') template = templates.ButtonTemplate( text= 'Hello, do you wish to buy or sell a product?', buttons=[ postback_button, postback_button1 ]) attachment = attachments.TemplateAttachment( template=template) message = messages.Message( attachment=attachment) request = messages.MessageRequest( recipient, message) messenger.send(request) # elif x['message'].get('text').lower() == 'Page 1': else: my_elements = [] postback_button2 = elements.PostbackButton( title="Buy", payload='BUY_1') # my_elements.append(Element(title="prod title", subtitle="prod substitle", # image_url="https://cdn.shopify.com/s/files/1/0739/4315/products/laguna_profile_mayfair_fullres_4815ea8f-83a8-4e29-841f-c099841987de.png?v=1480399889", # buttons=[postback_button2])) # my_elements.append(Element(title="prod title", subtitle="prod substitle", # image_url="https://cdn.shopify.com/s/files/1/0739/4315/products/laguna_profile_mayfair_fullres_4815ea8f-83a8-4e29-841f-c099841987de.png?v=1480399889", # buttons=[postback_button2])) # my_elements.append(Element(title="prod title", subtitle="prod substitle", # image_url="https://cdn.shopify.com/s/files/1/0739/4315/products/laguna_profile_mayfair_fullres_4815ea8f-83a8-4e29-841f-c099841987de.png?v=1480399889", # buttons=[postback_button2])) # my_elements.append(Element(title="prod title", subtitle="prod substitle", # image_url="https://cdn.shopify.com/s/files/1/0739/4315/products/laguna_profile_mayfair_fullres_4815ea8f-83a8-4e29-841f-c099841987de.png?v=1480399889", # buttons=[postback_button2])) my_elements.append( Element( title="HP Pavilion 420", subtitle="KES 24000", image_url= "http://www.browsingphones.com/wp-content/uploads/2015/11/HP-Laptop-Price-In-Kenya-Specs-Jumia-300x225.jpg", buttons=[postback_button2])) my_elements.append( Element( title="Lenovo workbook 14 inch", subtitle="KES 30000", image_url= "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcThIxirSJSLuv9qpZCeyhp30itUZ-IrndcTyloWPSHEij2e3zjo2Q", buttons=[postback_button2])) my_elements.append( Element( title="HP Workbook 8200", subtitle="KED 27000", image_url= "https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQaz0E0LRQEwsNq1H24VKTn4Idxy0nnFDvHcyIuOzH9cQFJaG5E", buttons=[postback_button2])) my_elements.append( Element( title="HP Pavilion 328", subtitle="KES 45000", image_url= "https://i5.walmartimages.com/dfw/4ff9c6c9-2ac9/k2-_ed8b8f8d-e696-4a96-8ce9-d78246f10ed1.v1.jpg", buttons=[postback_button2])) template = templates.GenericTemplate( elements=my_elements) attachment = attachments.TemplateAttachment( template=template) message = messages.Message( attachment=attachment) request = messages.MessageRequest( recipient, message) messenger.send(request) if x['message'].get('attachments'): for att in x['message'].get('attachments'): pass elif x.get('postback').get('payload') == 'BUY': quick_reply = quick_replies.QuickReplyItem( content_type='text', title='LaptopMart', payload='page_1') quick_reply2 = quick_replies.QuickReplyItem( content_type='text', title='BikeMart', payload='page_2') replies = quick_replies.QuickReplies( quick_replies=[quick_reply, quick_reply2]) message = messages.Message( text='Where would you like to shop?', quick_replies=replies) request = messages.MessageRequest(recipient, message) messenger.send(request) pass elif x.get('postback').get( 'payload') == 'USER_DEFINED_PAYLOAD1': message = messages.Message(text='New Message') request = messages.MessageRequest(recipient, message) messenger.send(request) pass elif x.get('postback').get('payload') == 'BUY_1': my_elements = [] id = str(uuid.uuid4()) buy_response = buy('+254720000802', '+254720000800', id) if buy_response.status_code == 202: message = messages.Message( text='Transaction complete') request = messages.MessageRequest( recipient, message) messenger.send(request) template = templates.GenericTemplate( elements=my_elements) attachment = attachments.TemplateAttachment( template=template) message = messages.Message(attachment=attachment) request = messages.MessageRequest( recipient, message) messenger.send(request) else: print buy_response.status_code buy_response = buy('+254720000802', '+254720000800', id) quick_reply3 = quick_replies.QuickReplyItem( content_type='text', title=buy_response.status_code, payload='page_2') except Exception, e: print e.message except Exception, e: print e