Last active
May 1, 2022 14:39
-
-
Save bots-business/0635dfc5ee3b28328e93239a607d680c to your computer and use it in GitHub Desktop.
Use this code to connect Bots.Business BJS with Google App Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bots.Business 2020 | |
// Use this code to connect Bots.Business BJS with Google App Script | |
// BJS data; | |
var message, chat, bot, params, options, statistics, admins, owner, iteration_quota, | |
payment_plan, completed_commands_count, request, content, http_status, cookies, http_headers, user; | |
function debug(){ run() } | |
function run(data){ | |
var is_debug = false; | |
if(!data){ | |
data = loadData(); | |
is_debug = true; | |
} | |
data = JSON.parse(data); | |
setBJSData(data.data); | |
var result = {}; | |
try { | |
result = eval(data.code); | |
}catch (e){ | |
result.error = { name: e.name, message: e.message, stack: e.stack, code: data.code} | |
if(is_debug){ sendMail(data, result) } | |
} | |
if(is_debug&&(result=={})){ sendMail(data, result) } | |
callWebhook(result, data.webhookUrl); | |
} | |
//this is a function that fires when the webapp receives a POST request | |
function doPost(e){ | |
var data = e.postData.contents; | |
saveData(data); | |
run(data); | |
} | |
function setBJSData(data){ | |
message = data.message; | |
chat = data.chat; | |
bot = data.bot; | |
params = data.params; | |
options = data.options; | |
statistics = data.statistics; | |
admins = data.admins; | |
owner = data.owner; | |
iteration_quota = data.iteration_quota; | |
payment_plan = data.payment_plan; | |
completed_commands_count = data.completed_commands_count; | |
request = data.request; | |
content = data.content; | |
http_status = data.http_status; | |
cookies = data.cookies; | |
http_headers = data.http_header; | |
user = data.user; | |
} | |
function sendMail(data, result){ | |
if(!data.email){ return } | |
MailApp.sendEmail({ | |
to: data.email, | |
subject: "Google App Script Error", | |
htmlBody: "<b>code:</b>" + | |
"<br>" + JSON.stringify(data.code) + | |
"<br><br><b>result:</b>" + | |
"<br>" + JSON.stringify(result) | |
}); | |
} | |
function saveData(data){ | |
CacheService.getScriptCache().put("LastData", data); | |
} | |
function loadData(){ | |
return CacheService.getScriptCache().get("LastData"); | |
} | |
function doGet(e){ | |
var html = response.getContentText(); | |
return HtmlService.createHtmlOutput("<h1>Google App Sync: installed</h1>"); | |
} | |
function callWebhook(data, webhookUrl){ | |
var options = { | |
'method' : 'post', | |
'contentType': 'application/json', | |
'payload' : JSON.stringify(data) | |
}; | |
UrlFetchApp.fetch(webhookUrl, options); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment