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_button_template(self): web_button = elements.WebUrlButton( title='Show website', url='https://petersapparel.parseapp.com') postback_button = elements.PostbackButton( title='Start chatting', payload='USER_DEFINED_PAYLOAD') template = templates.ButtonTemplate( text='What do you want to do next?', buttons=[web_button, postback_button]) attachment = attachments.TemplateAttachment(template=template) message = messages.Message(attachment=attachment) request = messages.MessageRequest(self.recipient, message) self.assertEquals( request.serialise(), '{"message": {"attachment": {"type": "template", "payload": {"template_type": "button", "text": "What do you want to do next?", "buttons": [{"url": "https://petersapparel.parseapp.com", "type": "web_url", "title": "Show website"}, {"type": "postback", "payload": "USER_DEFINED_PAYLOAD", "title": "Start chatting"}]}}}, "recipient": {"id": "123"}}' )
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)
"key": "msisdn", "value": recipient_msisdn }] } response = requests.post(url=url, headers=headers, data=json.dumps(payload)) return response ACCESS_TOKEN = "" messenger = MessengerClient(access_token=ACCESS_TOKEN) # Send button template web_button = elements.WebUrlButton(title='Show website', url='https://petersapparel.parseapp.com') postback_button = elements.PostbackButton(title='Start chatting', payload='USER_DEFINED_PAYLOAD') template = templates.ButtonTemplate(text='What do you want to do next?', buttons=[web_button, postback_button]) attachment = attachments.TemplateAttachment(template=template) # # Send button template 2 # web_button2 = elements.WebUrlButton( # title="I'm a buyer", # url='https://petersapparel.parseapp.com' # ) # postback_button2 = elements.PostbackButton( # title='I have something to sell', # payload='USER_DEFINED_PAYLOAD'