-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.gs
47 lines (38 loc) · 1.21 KB
/
Code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// The token of your Telegram bot
var TOKEN = "<< Your bot token >>";
// Name of the Telegram channel which bot is added into
var CHANNEL = "@<< Your Channel >>";
// Name of the Gmail Label which need to search for
var LABEL = "<< Your Label >>"
function processEmails() {
// Search unread emails with specified label
var search = "label:" + LABEL + " is:unread";
Logger.log('Search: ' + search);
var threads = GmailApp.search(search, 0, 10);
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
Logger.log('Number of emails found: ' + messages.length);
for (var j = 0; j < messages.length; j++) {
var m = messages[j];
if (!m.isInTrash() && m.isUnread()) {
if (m.getAttachments().length > 0) {
// Get the first attachment (image jpeg file)
// and send to telegram bot
sendPhoto(m.getAttachments()[0].copyBlob());
}
m.markRead();
}
}
}
}
function sendPhoto(photo) {
var payload = {
'chat_id': CHANNEL,
'photo': photo
};
var options = {
'method': 'post',
'payload': payload
};
UrlFetchApp.fetch("https://api.telegram.org/bot" + TOKEN + "/sendPhoto", options);
}