Search in sources :

Example 1 with Attachment

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");
}
Also used : MessengerCallback(co.aurasphere.botmill.fb.model.incoming.MessengerCallback) ListTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.ListTemplatePayload) Attachment(co.aurasphere.botmill.fb.model.base.Attachment) EchoMessage(co.aurasphere.botmill.fb.model.incoming.callback.EchoMessage) MessageEnvelope(co.aurasphere.botmill.fb.model.incoming.MessageEnvelope) Test(org.junit.Test)

Example 2 with Attachment

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;
}
Also used : AttachmentType(co.aurasphere.botmill.fb.model.base.AttachmentType) JsonElement(com.google.gson.JsonElement) Attachment(co.aurasphere.botmill.fb.model.base.Attachment) Payload(co.aurasphere.botmill.fb.model.base.Payload) UrlPayload(co.aurasphere.botmill.fb.model.outcoming.payload.UrlPayload) QuickReplyLocationPayload(co.aurasphere.botmill.fb.model.base.QuickReplyLocationPayload) AirlineCheckinTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineCheckinTemplatePayload) AirlineFlightUpdateTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineFlightUpdateTemplatePayload) GenericTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.GenericTemplatePayload) ReceiptTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.ReceiptTemplatePayload) AirlineBoardingPassTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineBoardingPassTemplatePayload) ListTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.ListTemplatePayload) ButtonTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.ButtonTemplatePayload) AirlineItineraryTemplatePayload(co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineItineraryTemplatePayload) PayloadType(co.aurasphere.botmill.fb.model.outcoming.payload.PayloadType)

Example 3 with 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);
}
Also used : FbBotMillMessageResponse(co.aurasphere.botmill.fb.model.outcoming.message.FbBotMillMessageResponse) AttachmentMessage(co.aurasphere.botmill.fb.model.outcoming.message.AttachmentMessage) Attachment(co.aurasphere.botmill.fb.model.base.Attachment) AttachmentPayload(co.aurasphere.botmill.fb.model.outcoming.payload.AttachmentPayload)

Aggregations

Attachment (co.aurasphere.botmill.fb.model.base.Attachment)3 ListTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.ListTemplatePayload)2 AttachmentType (co.aurasphere.botmill.fb.model.base.AttachmentType)1 Payload (co.aurasphere.botmill.fb.model.base.Payload)1 QuickReplyLocationPayload (co.aurasphere.botmill.fb.model.base.QuickReplyLocationPayload)1 MessageEnvelope (co.aurasphere.botmill.fb.model.incoming.MessageEnvelope)1 MessengerCallback (co.aurasphere.botmill.fb.model.incoming.MessengerCallback)1 EchoMessage (co.aurasphere.botmill.fb.model.incoming.callback.EchoMessage)1 AttachmentMessage (co.aurasphere.botmill.fb.model.outcoming.message.AttachmentMessage)1 FbBotMillMessageResponse (co.aurasphere.botmill.fb.model.outcoming.message.FbBotMillMessageResponse)1 AttachmentPayload (co.aurasphere.botmill.fb.model.outcoming.payload.AttachmentPayload)1 PayloadType (co.aurasphere.botmill.fb.model.outcoming.payload.PayloadType)1 UrlPayload (co.aurasphere.botmill.fb.model.outcoming.payload.UrlPayload)1 ButtonTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.ButtonTemplatePayload)1 GenericTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.GenericTemplatePayload)1 ReceiptTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.ReceiptTemplatePayload)1 AirlineBoardingPassTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineBoardingPassTemplatePayload)1 AirlineCheckinTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineCheckinTemplatePayload)1 AirlineFlightUpdateTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineFlightUpdateTemplatePayload)1 AirlineItineraryTemplatePayload (co.aurasphere.botmill.fb.model.outcoming.payload.template.airline.AirlineItineraryTemplatePayload)1