Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

![Logo](images/Logo_alpha.png)

Receive customized email notifications to alert you about incoming birthdays or
other events of your Google contacts.
Receive customized email and Telegram notifications to alert you about
incoming birthdays or other events of your Google contacts.

Have you ever wondered why on Earth would Google Calendar provide a calendar to
remind you of your contact birthdays, but without letting you set up
Expand Down Expand Up @@ -212,8 +212,10 @@ permissions:
Obviously this script needs your authorization to send you the email
notifications. It won't send any other email to anyone.
- **Connect to an external service**
This permission is needed to check for updates and to load the profile images
of your contacts.
This permission is needed to
- check for updates
- load the profile images of your contacts
- send notifications to the Telegram Bot API (if enabled)

## Contributing

Expand Down
48 changes: 48 additions & 0 deletions code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ var settings = {
*/
lang: 'en'
},
telegram: {
/*
* TELEGRAM BOT NOTIFICATIONS
*
* If you want to receive notifications via Telegram, set 'enabled' to true.
* You will need a Bot Token from @BotFather and your Chat ID.
*/
enabled: false,
botToken: 'YOUR_BOT_TOKEN',
chatId: 'YOUR_CHAT_ID'
},
notifications: {
/*
* HOUR OF THE NOTIFICATION
Expand Down Expand Up @@ -2519,8 +2530,14 @@ function main (forceDate) {
});

log.add('Email sent.', Priority.INFO);

// Send Telegram notification if enabled.
log.add('Sending Telegram notification...', Priority.INFO);
var telegramMessage = '<b>' + emailData.subject + '</b>\n\n' + emailData.body;
sendTelegram(telegramMessage);
}


// Send the log if the debug options say so.
log.sendEmail(settings.user.notificationEmail, settings.user.emailSenderName);
}
Expand Down Expand Up @@ -2616,3 +2633,34 @@ function dateWithTimezone (year, month, day, hour, minute, second, timezoneId) {
date = new Date(date.getTime() - offset * 60000);
return date;
}

/**
* Send a notification message to a Telegram bot.
*
* @param {string} text - The message text to send.
*/
function sendTelegram (text) {
if (!settings.telegram.enabled || settings.telegram.botToken === 'YOUR_BOT_TOKEN') {
return;
}

var url = 'https://api.telegram.org/bot' + settings.telegram.botToken + '/sendMessage'
var payload = {
'chat_id': settings.telegram.chatId,
'text': text,
'parse_mode': 'HTML'
}

var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(payload),
'muteHttpExceptions': true
}

try {
UrlFetchApp.fetch(url, options);
} catch (e) {
log.add('Telegram Error: ' + e.message, Priority.WARNING);
}
}
1 change: 1 addition & 0 deletions docs/install-and-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ These are the names of the variables:
- `settings.notifications.maxPhonesCount`
- `settings.notifications.indentSize`
- `settings.notifications.compactGrouping`
- `settings.telegram`

### Debugging options

Expand Down