use of co.aurasphere.botmill.fb.model.base.Attachment in project fb-botmill by BotMill.
the class EchoCallbackTest method testEchoWithListTemplate.
/**
* Tests an {@link EchoMessage} containing a {@link ListTemplatePayload}.
*/
@Test
public void testEchoWithListTemplate() {
// Tests the echo with a List Template.
String echoListTemplatejson = "{\"object\":\"page\",\"entry\":[{\"id\":\"1423903854504468\",\"time\":1483578465379,\"messaging\":[{\"sender\":{\"id\":\"****\"},\"recipient\":{\"id\":\"****\"},\"timestamp\":1483578464765,\"message\":{\"is_echo\":true,\"app_id\":****,\"mid\":\"mid.1483578464765:ed1fe23f35\",\"seq\":785209,\"attachments\":[{\"title\":\"Title1, Title2, Title3, Title4\",\"url\":\"https://www.facebook.com/commerce/update/\",\"type\":\"template\",\"payload\":{\"template_type\":\"list\",\"sharable\":false,\"elements\":[{\"title\":\"Title1\",\"item_url\":\"http://www.alvinjayreyes.com/\",\"image_url\":\"http://blog.domainmonkee.com/wp-content/uploads/2014/05/url.jpg\",\"subtitle\":\"This is a sample sub title for your product\",\"buttons\":[{\"type\":\"postback\",\"title\":\"View\",\"payload\":\"view\"}],\"default_action\":{\"type\":\"web_url\",\"url\":\"http://www.alvinjayreyes.com/\"}},{\"title\":\"Title2\",\"item_url\":\"http://www.alvinjayreyes.com/\",\"image_url\":\"http://blog.domainmonkee.com/wp-content/uploads/2014/05/url.jpg\",\"subtitle\":\"This is a sample sub title for your product\",\"buttons\":[{\"type\":\"postback\",\"title\":\"View\",\"payload\":\"view\"}],\"default_action\":{\"type\":\"web_url\",\"url\":\"http://www.alvinjayreyes.com/\"}},{\"title\":\"Title3\",\"item_url\":\"http://www.alvinjayreyes.com/\",\"image_url\":\"http://blog.domainmonkee.com/wp-content/uploads/2014/05/url.jpg\",\"subtitle\":\"This is a sample sub title for your product\",\"buttons\":[{\"type\":\"postback\",\"title\":\"View\",\"payload\":\"view\"}],\"default_action\":{\"type\":\"web_url\",\"url\":\"http://www.alvinjayreyes.com/\"}},{\"title\":\"Title4\",\"item_url\":\"http://www.alvinjayreyes.com/\",\"image_url\":\"http://blog.domainmonkee.com/wp-content/uploads/2014/05/url.jpg\",\"subtitle\":\"This is a sample sub title for your product\",\"buttons\":[{\"type\":\"postback\",\"title\":\"View\",\"payload\":\"view\"}],\"default_action\":{\"type\":\"web_url\",\"url\":\"http://www.alvinjayreyes.com/\"}}],\"buttons\":[{\"type\":\"postback\",\"title\":\"Just Sample\",\"payload\":\"Yes\"}]}}]}}]}]}";
MessengerCallback callback = FbBotMillJsonUtils.fromJson(echoListTemplatejson, MessengerCallback.class);
MessageEnvelope messageEnvelope = checkCallbackWellFormed(callback);
EchoMessage echoMessage = super.assertInstanceOf(messageEnvelope.getMessage(), EchoMessage.class);
// Checks that individual fields are correctly parsed.
Assert.assertTrue(echoMessage.isEcho());
Assert.assertEquals("****", echoMessage.getAppId());
Assert.assertEquals("mid.1483578464765:ed1fe23f35", echoMessage.getMid());
Assert.assertEquals("785209", echoMessage.getSeq());
super.assertListOfSize(1, echoMessage.getAttachments());
// Checks the attachment.
Attachment attachment = echoMessage.getAttachments().get(0);
Assert.assertEquals("Title1, Title2, Title3, Title4", attachment.getTitle());
Assert.assertEquals("https://www.facebook.com/commerce/update/", attachment.getUrl());
Assert.assertEquals(AttachmentType.TEMPLATE, attachment.getType());
// Checks the payload.
ListTemplatePayload payload = super.assertInstanceOf(attachment.getPayload(), ListTemplatePayload.class);
Assert.assertEquals(PayloadType.LIST, payload.getTemplateType());
super.assertListOfSize(4, payload.getElements());
for (int i = 0; i < 4; i++) {
checkListTemplateElements(payload.getElements().get(i), i + 1);
}
// Checks the buttons.
super.assertListOfSize(1, payload.getButtons());
checkPostbackButtonWellFormed(payload.getButtons().get(0), "Just Sample", "Yes");
}
use of co.aurasphere.botmill.fb.model.base.Attachment in project fb-botmill by BotMill.
the class AttachmentDeserializer method deserialize.
/*
* (non-Javadoc)
*
* @see
* com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement,
* java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
*/
public Attachment deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Attachment attachment = delegateGson.fromJson(json, Attachment.class);
AttachmentType type = attachment.getType();
Class<? extends Payload> payloadClass = null;
JsonElement payloadJson = json.getAsJsonObject().get("payload");
switch(type) {
case AUDIO:
case FILE:
case IMAGE:
case VIDEO:
payloadClass = UrlPayload.class;
break;
case LOCATION:
payloadClass = QuickReplyLocationPayload.class;
break;
case FALLBACK:
// nothing.
break;
case TEMPLATE:
// In case of a template I need to check which one to instantiate.
String payloadTypeString = payloadJson.getAsJsonObject().get("template_type").getAsString();
PayloadType templateType = PayloadType.valueOf(payloadTypeString.toUpperCase());
switch(templateType) {
case AIRLINE_BOARDINGPASS:
payloadClass = AirlineBoardingPassTemplatePayload.class;
break;
case AIRLINE_CHECKIN:
payloadClass = AirlineCheckinTemplatePayload.class;
break;
case AIRLINE_ITINERARY:
payloadClass = AirlineItineraryTemplatePayload.class;
break;
case AIRLINE_UPDATE:
payloadClass = AirlineFlightUpdateTemplatePayload.class;
break;
case BUTTON:
payloadClass = ButtonTemplatePayload.class;
break;
case GENERIC:
payloadClass = GenericTemplatePayload.class;
break;
case LIST:
payloadClass = ListTemplatePayload.class;
break;
case RECEIPT:
payloadClass = ReceiptTemplatePayload.class;
break;
}
break;
}
Payload payload = context.deserialize(payloadJson, payloadClass);
attachment.setPayload(payload);
return attachment;
}
use of co.aurasphere.botmill.fb.model.base.Attachment in project fb-botmill by BotMill.
the class UploadApi method uploadAttachment.
/**
* Method to upload an attachment to Facebook's server in order to use it
* later. Requires the pages_messaging permission.
*
* @param attachmentType
* the type of attachment to upload to Facebook. Please notice
* that currently Facebook supports only image, audio, video and
* file attachments.
* @param attachmentUrl
* the URL of the attachment to upload to Facebook.
* @return nonexpiring ID for the attachment.
*/
public static UploadAttachmentResponse uploadAttachment(AttachmentType attachmentType, String attachmentUrl) {
AttachmentPayload payload = new AttachmentPayload(attachmentUrl, true);
Attachment attachment = new Attachment(attachmentType, payload);
AttachmentMessage message = new AttachmentMessage(attachment);
FbBotMillMessageResponse toSend = new FbBotMillMessageResponse(null, message);
return FbBotMillNetworkController.postUploadAttachment(toSend);
}
Aggregations