Send latest YouTube video to Telegram channels with a button linking to the video. Here is the full Documentation for this AppScript.
-
The
sendLatestYoutubeVideo()
function is defined. This function fetches the latest video uploaded or scheduled on a specific YouTube channel and sends a message about it to a Telegram chat. -
The API key for the YouTube Data API and the channel ID for the channel you want to fetch videos from are defined as variables.
-
The URL for the API request is constructed using the API key, channel ID, and the maximum number of videos to return (in this case, 1).
-
The API request is sent using the
UrlFetchApp.fetch()
method and the response is stored in theresponse
variable. -
The response is parsed as a JSON object using
JSON.parse()
and stored in thedata
variable. -
The code checks if any videos were found in the channel. If not, it logs a message and exits the function.
-
The code checks if the latest activity is a video upload or scheduled video. If not, it logs a message and exits the function.
-
The title and video ID of the latest video are extracted from the API response and stored in the
videoTitle
andvideoId
variables, respectively. -
The code checks if a video ID was found in the response. If not, it logs a message and exits the function.
-
The URL of the latest video is constructed using the video ID.
-
The Telegram bot token and chat IDs are defined as variables.
-
The URL for sending messages to Telegram is constructed using the bot token.
-
A button is created using the
inline_keyboard
property and the URL of the latest video. -
The message to be sent to Telegram is constructed using the video title and URL, and the button is included as
reply_markup
. -
A loop is started to send the message to each chat ID specified in the
TELEGRAM_CHAT_IDS
array. -
The
chat_id
,text
, andpayload
properties for each chat ID are set, and the message is sent using theUrlFetchApp.fetch()
method. -
If there is an error sending the message, a log message is created. If the message is sent successfully, a log message is created.
That's it! This code fetches the latest video from a specific YouTube channel and sends a message about it to one or more Telegram chats. If there are no videos in the channel or the latest activity is not a video upload or scheduled video, the code logs a message and exits the function.