001// Generated by delombok at Fri Oct 04 16:05:59 CEST 2024 002/* 003 * Copyright (c) 2010-2024 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; 031 032/** 033 * Represents the media template element that is used with the url as defined 034 * <a href="https://developers.facebook.com/docs/messenger-platform/send-messages/template/media#url">here</a> 035 * 036 * Allowed urls are: 037 * <ul> 038 * <li>https://business.facebook.com/<PAGE_NAME>/videos/<NUMERIC_ID></li> 039 * <li>https://www.facebook.com/<USERNAME>/videos/<NUMERIC_ID>/</li> 040 * <li>https://business.facebook.com/<PAGE_NAME>/photos/<NUMERIC_ID></li> 041 * <li>https://www.facebook.com/photo.php?fbid=<NUMERIC_ID></li> 042 * </ul> 043 */ 044public class MediaTemplateUrlElement extends AbstractFacebookType implements MediaAttachment.MediaTemplateElement { 045 @Facebook("media_type") 046 private String mediaType; 047 @Facebook 048 private String url; 049 @Facebook 050 private List<AbstractButton> buttons; 051 052 public MediaTemplateUrlElement(String url) { 053 if (!validUrl(url)) { 054 throw new IllegalArgumentException("The given URL is not valid"); 055 } 056 this.url = url; 057 if (url.contains("/videos/")) { 058 this.mediaType = MediaAttachment.MediaType.VIDEO.name().toLowerCase(); 059 } else { 060 this.mediaType = MediaAttachment.MediaType.IMAGE.name().toLowerCase(); 061 } 062 } 063 064 @Override 065 public void addButton(AbstractButton button) { 066 if (buttons == null) { 067 buttons = new ArrayList<>(); 068 } 069 buttons.add(button); 070 } 071 072 private boolean validUrl(String url) { 073 if (url == null) { 074 throw new IllegalArgumentException("The media URL may not be null"); 075 } 076 if (url.startsWith("https://business.facebook.com/")) { 077 return true; 078 } 079 return url.startsWith("https://www.facebook.com/"); 080 } 081 082 @java.lang.SuppressWarnings("all") 083 public String getMediaType() { 084 return this.mediaType; 085 } 086 087 @java.lang.SuppressWarnings("all") 088 public String getUrl() { 089 return this.url; 090 } 091 092 @java.lang.SuppressWarnings("all") 093 public List<AbstractButton> getButtons() { 094 return this.buttons; 095 } 096}