001// Generated by delombok at Sun Sep 26 16:30:34 CEST 2021
002/*
003 * Copyright (c) 2010-2021 Mark Allen, Norbert Bartels.
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023package com.restfb.types.send.media;
024
025import java.util.ArrayList;
026import java.util.List;
027import com.restfb.Facebook;
028import com.restfb.types.AbstractFacebookType;
029import com.restfb.types.send.AbstractButton;
030import com.restfb.types.send.MediaAttachment;
031import com.restfb.types.send.WebButton;
032
033/**
034 * Represents the media template element that is used with the url as defined
035 * <a href="https://developers.facebook.com/docs/messenger-platform/send-messages/template/media#url">here</a>
036 *
037 * Allowed urls are:
038 * <ul>
039 * <li>https://business.facebook.com/<PAGE_NAME>/videos/<NUMERIC_ID></li>
040 * <li>https://www.facebook.com/<USERNAME>/videos/<NUMERIC_ID>/</li>
041 * <li>https://business.facebook.com/<PAGE_NAME>/photos/<NUMERIC_ID></li>
042 * <li>https://www.facebook.com/photo.php?fbid=<NUMERIC_ID></li>
043 * </ul>
044 */
045public class MediaTemplateUrlElement extends AbstractFacebookType implements MediaAttachment.MediaTemplateElement {
046  @Facebook("media_type")
047  private String mediaType;
048  @Facebook
049  private String url;
050  @Facebook
051  private List<AbstractButton> buttons;
052
053  public MediaTemplateUrlElement(String url) {
054    if (!validUrl(url)) {
055      throw new IllegalArgumentException("The given URL is not valid");
056    }
057    this.url = url;
058    if (url.contains("/videos/")) {
059      this.mediaType = MediaAttachment.MediaType.VIDEO.name().toLowerCase();
060    } else {
061      this.mediaType = MediaAttachment.MediaType.IMAGE.name().toLowerCase();
062    }
063  }
064
065  @Override
066  public void addButton(AbstractButton button) {
067    if (buttons == null) {
068      buttons = new ArrayList<>();
069    }
070    buttons.add(button);
071  }
072
073  private boolean validUrl(String url) {
074    if (url == null) {
075      throw new IllegalArgumentException("The media URL may not be null");
076    }
077    if (url.startsWith("https://business.facebook.com/")) {
078      return true;
079    }
080    return url.startsWith("https://www.facebook.com/");
081  }
082
083  @java.lang.SuppressWarnings("all")
084  public String getMediaType() {
085    return this.mediaType;
086  }
087
088  @java.lang.SuppressWarnings("all")
089  public String getUrl() {
090    return this.url;
091  }
092
093  @java.lang.SuppressWarnings("all")
094  public List<AbstractButton> getButtons() {
095    return this.buttons;
096  }
097}