-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathGoogleApp.js
152 lines (131 loc) · 3.29 KB
/
GoogleApp.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
var libPrefix = "GoogleAppLib_";
function setUrl(url){
Bot.setProperty(libPrefix + "AppUrl", url, "string");
}
function getUrl(){
return Bot.getProperty(libPrefix + "AppUrl");
}
function isWebhookLibInstalled(){
if(Libs.Webhooks){ return }
throwError("Please install Webhook Lib. It is required by GoogleApp Lib.")
}
function getWebhookUrl(isDebug){
var cmd = libPrefix + "onRun";
if(isDebug){
Bot.sendMessage("GoogleAppLib: Debug mode - ON");
cmd = libPrefix + "onDebugRun"
}
return Libs.Webhooks.getUrlFor({
command: cmd,
user_id: user.id
})
}
function throwError(title){
throw new Error("GoogleApp Lib error: " + title);
}
function isOptionsCorrect(options){
if(!options){
throwError("on run - need object param")
}
if(typeof(options)!="object"){
throwError("on run - param must be object")
}
if(!options.code){
throwError("on run - need passed code in params")
}
if(!options.code.name){
throwError("on run - code must be function with name")
}
}
function run(options){
isWebhookLibInstalled();
isOptionsCorrect(options)
var webhookUrl = getWebhookUrl(options.debug);
var func = options.code;
var url = getUrl() + "?hl=en";
if(options.debug){
Bot.sendMessage(
"GoogleAppLib: post data to [url](" + url + ")." +
"\n\nYou can open this link only on incognito mode without Google autorization"
);
}
HTTP.post( {
url: url,
// success: "" - no success
error: libPrefix + "onHttpError",
body: {
code: func + ";" + func.name + "()",
webhookUrl: webhookUrl,
email: options.email,
onRun: options.onRun,
// pass all BJS variables to Google App script
data: getData()
},
folow_redirects: true,
// headers: { "Content-Type": "text/plain;charset=utf-8" }
} )
}
function getData(){
return {
message: message,
user: user,
chat: chat,
bot: bot,
params: params,
options: options,
admins: admins,
owner: owner,
iteration_quota: iteration_quota,
payment_plan: payment_plan,
completed_commands_count: completed_commands_count,
request: request,
content: content,
http_status: http_status,
cookies: cookies,
http_headers: http_headers,
command: command,
BB_API_URL: BB_API_URL
}
}
function inspectError(json){
var error = json.error;
if(!error){ return }
Bot.sendMessage("Error on Google App script: " +
inspect(error.name) + "\n\n" + inspect(error.message) );
Bot.sendMessage("Code: " + inspect(error.code))
return true
}
function parseContent(){
if(typeof(content)=="object"){
return content
}
try{
return JSON.parse(content);
}catch(e){
throwError("Error on content parsing: " + content)
}
}
function doUserOnRun(data){
if(!data.onRun){ return }
if(data.onRun==""){ return }
Bot.run({ command: data.onRun, options: data.result })
}
function onRun(){
var json = parseContent();
doUserOnRun(json);
}
function onDebugRun(){
var json = parseContent();
if(inspectError(json.result)){ return }
doUserOnRun(json);
}
function onHttpError(){
throwError("app error. Please check app url and script installation.")
}
publish({
setUrl: setUrl,
run: run
})
on(libPrefix + "onRun", onRun);
on(libPrefix + "onDebugRun", onDebugRun)
on(libPrefix + "onHttpError", onHttpError)