Skip to content

Instantly share code, notes, and snippets.

@celorodovalho
Created April 26, 2018 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celorodovalho/05faa21c55add1d9997ebf8205f9df72 to your computer and use it in GitHub Desktop.
Save celorodovalho/05faa21c55add1d9997ebf8205f9df72 to your computer and use it in GitHub Desktop.
Google Script for PHPDFBOT
function phpDfBot() {
try {
var threads = GmailApp.search("(list:leonardoti@googlegroups.com OR list:clubinfobsb@googlegroups.com OR to:vagas@noreply.github.com OR to:clubinfobsb@googlegroups.com OR to:leonardoti@googlegroups.com) AND (subject:((desenvolvedor OR programador OR developer OR analista OR php OR web OR arquiteto OR dba OR suporte)) OR (desenvolvedor OR programador OR developer OR analista OR php OR web OR arquiteto)) is:unread", 0, 25);
Logger.log(threads.length);
for (var key in threads) {
var thread = threads[key];
var messages = thread.getMessages();
var label = GmailApp.getUserLabelByName("Não lidas");
var pattern = /<img.*src="([^"]*)"[^>]*>/;
var payload = {};
for (var index in messages) {
var message = messages[index];
var attachments = message.getAttachments();
payload[thread.getId()] = {
subject: message.getSubject()
};
var texto = message.getPlainBody()
.split('You are receiving this because you are subscribed to this thread')[0]
.split('Você recebeu esta mensagem porque está inscrito para o Google')[0]
.split('Você está recebendo esta mensagem porque')[0];
var html = message.getBody();
var matches = pattern.exec(html);
var image = null;
var url = '';
if(matches) {
url = matches[1];
var urlPattern = /^https*\:\/\/.*$/;
if(!urlPattern.exec(url)) {
url = '';
}
}
if (texto && texto.length > 200) {
payload[thread.getId()].message = texto.replace(/^(:)/gi, '').trim();
}
if (url.length) {
payload[thread.getId()].image = url;
}
if (attachments.length) {
for (var chave in attachments) {
var blob = attachments[chave].copyBlob();
payload[thread.getId()].image = Utilities.base64Encode(blob.getBytes());
}
}
}
sendMessage(payload);
thread.markRead();
thread.moveToArchive();
thread.removeLabel(label);
}
} catch (e) {
Logger.log(e.message);
}
}
function sendMessage(payload) {
var url = 'BOT_URL_SEND_MESSAGE';
var options =
{
"method": "POST",
"contentType" : "application/json",
"payload": JSON.stringify(payload),
"followRedirects": true,
"muteHttpExceptions": true
};
if (Object.keys(payload).length) {
var result = UrlFetchApp.fetch(url, options);
Logger.log(result.getResponseCode());
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(params.results);
}
}
}
function getCrawler()
{
var url = 'BOT_URL_CRON_CRAWLER';
var options =
{
"method": "GET",
"followRedirects": true,
"muteHttpExceptions": true
};
var result = UrlFetchApp.fetch(url, options);
Logger.log(result.getResponseCode());
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(params.results);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment