Skip to content

Instantly share code, notes, and snippets.

@bots-business
Last active May 1, 2022 14:39
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 bots-business/0635dfc5ee3b28328e93239a607d680c to your computer and use it in GitHub Desktop.
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
// 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