Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save omarmiatello/50372b4b6b6302e84c8c97c31213f5dd to your computer and use it in GitHub Desktop.
Save omarmiatello/50372b4b6b6302e84c8c97c31213f5dd to your computer and use it in GitHub Desktop.
Notification system! Add button in Google Presentation for share a single slide in a Telegram channel.

Setup

  • Open Presentation doc
  • Copy this script
  • Click File > Edit property and set script property TELEGRAM_TOKEN and TELEGRAM_CHAT_ID
  • Click Resource > Advanced Google Service and enable Slide API (also on API Console)
  • Save script and refresh the presentation
  • Done! 😀
function onOpen() {
var ui = SlidesApp.getUi();
ui.createMenu('GDG Tools')
.addSubMenu(ui.createMenu('Share this slide on "GDG Italia"')
.addItem('Telegram (favorite)', 'shareItalyTelegram')
.addItem('Slack', 'shareItalySlack'))
.addSubMenu(ui.createMenu('Share this slide on "GDG Spain"')
.addItem('Telegram', 'shareSpainTelegram')
.addItem('Slack (favorite)', 'shareSpainSlack'))
.addToUi();
}
// Menu functions
function shareItalyTelegram() {
var slideInfo = getCurrentSlide();
var properties = PropertiesService.getScriptProperties();
var tToken = properties.getProperty('ITALY_TELEGRAM_TOKEN');
var chatId = properties.getProperty('ITALY_TELEGRAM_CHAT_ID');
sendTelegramPhoto(tToken, chatId, slideInfo['thumbnail'], slideInfo['url']);
}
function shareItalySlack() {
var slideInfo = getCurrentSlide();
var properties = PropertiesService.getScriptProperties();
var webhook = properties.getProperty('ITALY_SLACK_WEBHOOK');
var channel = properties.getProperty('ITALY_SLACK_CHANNEL');
sendSlackPhoto(webhook, channel, slideInfo['thumbnail'], slideInfo['url']);
}
function shareSpainTelegram() {
var slideInfo = getCurrentSlide();
var properties = PropertiesService.getScriptProperties();
var tToken = properties.getProperty('SPAIN_TELEGRAM_TOKEN');
var chatId = properties.getProperty('SPAIN_TELEGRAM_CHAT_ID');
sendTelegramPhoto(tToken, chatId, slideInfo['thumbnail'], slideInfo['url']);
}
function shareSpainSlack() {
var slideInfo = getCurrentSlide();
var properties = PropertiesService.getScriptProperties();
var webhook = properties.getProperty('SPAIN_SLACK_WEBHOOK');
var channel = properties.getProperty('SPAIN_SLACK_CHANNEL');
sendSlackPhoto(webhook, channel, slideInfo['thumbnail'], slideInfo['url']);
}
// Utility
function getCurrentSlide() {
var presentation = SlidesApp.getActivePresentation();
var presentationId = presentation.getId();
var pageId = presentation.getSelection().getCurrentPage().getObjectId();
return {
'url': presentation.getUrl() + '#slide=id.' + pageId,
'thumbnail': Slides.Presentations.Pages.getThumbnail(presentationId, pageId).contentUrl
}
}
function sendTelegramPhoto(telegramToken, chatId, imageUrl, caption) {
return UrlFetchApp.fetch('https://api.telegram.org/bot' + telegramToken + '/sendPhoto', {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify({
'chat_id': chatId,
'photo': imageUrl,
'caption': caption
})
});
}
function sendSlackPhoto(webhook, channel, imageUrl, caption) {
return UrlFetchApp.fetch(webhook, {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify({
'channel': channel,
'text': caption,
'attachments': [{
'fallback': 'Slide',
'image_url': imageUrl
}]
})
});
}
// by Jackl - Omar Miatello
function onOpen() {
SlidesApp.getUi().createMenu('Strumenti GDG')
.addItem('Pubblica su Telegram questa slide!', 'slideOnTelegram')
.addToUi();
}
function slideOnTelegram() {
var properties = PropertiesService.getScriptProperties(); // alternative: PropertiesService.getUserProperties();
var telegramToken = properties.getProperty('TELEGRAM_TOKEN');
var chatId = properties.getProperty('TELEGRAM_CHAT_ID');
var presentation = SlidesApp.getActivePresentation();
var presentationId = presentation.getId();
var pageId = presentation.getSelection().getCurrentPage().getObjectId();
var thumbnail = Slides.Presentations.Pages.getThumbnail(presentationId, pageId);
UrlFetchApp.fetch('https://api.telegram.org/bot' + telegramToken + '/sendPhoto', {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify({
'chat_id': chatId,
'photo': thumbnail.contentUrl,
'caption': 'URL: ' + presentation.getUrl() + '#slide=id.' + pageId
})
});
}
// by Jackl - Omar Miatello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment