GoogleApp
Use this lib to connect BJS with Google App Script​

Getting started

1. Go to https://script.google.com and create new project by button:
2. Paste the script from above into the script code editor and hit Save.
3. From the Publish menu, select Deploy as web app… 4. Choose to execute the app as yourself, and allow Anyone, even anonymous to execute the script. (Note, depending on your Google Apps instance, this option may not be available. You will need to contact your Google Apps administrator, or else use a Gmail account.) Now click Deploy. You may be asked to review permissions now. Project version - always "New".
5. The URL that you get will be the webhook that you need use in this Lib. You can test this webhook in your browser first by pasting it. Note that depending on your Google Apps instance, you may need to adjust the URL to make it work.
6. Install GoogleAppLib and WebhooksLib to bot
7. Create setup command:
/setup
1
// replace with your URL, obtained in step 4
2
Libs.GoogleApp.setUrl("https://script.google.com/macros/*******");
Copied!

Using

Use any Google App script in BJS now
1
function GACode(){
2
// Google App Script code here
3
// Please note: this function is runs on GA not BB
4
// ...
5
}
6
​
7
// BJS
8
Libs.GoogleApp.run({
9
code: GACode, // Function with Google App code
10
onRun: "onRun", // Optional. This command will be executed after run
11
email: "[email protected]" // Optional. Email for errors,
12
// debug: true // For debug. Default is false
13
});
Copied!
Example for command /task
1
function GACode(){
2
// translation from English to France
3
var trans = LanguageApp.translate(params, 'en', 'fr');
4
​
5
// make Google Calendar event
6
var event = CalendarApp.getDefaultCalendar()
7
.createEventFromDescription('Lunch with ' + user.first_name + ', Friday at 1PM');
8
9
// send Email
10
MailApp.sendEmail({
12
subject: "hello from bot " + bot.name,
13
htmlBody: "<h1>Hello!</h1>How are you?<br>" +
14
"We have message to bot: " + message
15
});
16
​
17
// ...
18
// Use all power of Google App Script!
19
20
// return result as JSON
21
return { event: event, trans: trans }
22
}
23
​
24
Libs.GoogleApp.run({
25
func: GACode,
26
onRun: "onRun",
27
// email: "[email protected]" // email for errors
28
// debug: true // default false
29
});
Copied!
command onRun
1
Bot.sendMessage(inspect(options))
Copied!
GACode - it is isolated function with Google App code. It can not have BJS code like Bot.sendMessage and etc. Only GA code!
But you can use variables and pass data with options for Bot.run method.
1
let myVar;
2
​
3
function GACode(){
4
// Google App Script code here
5
// Please note: this function is runs on GA not BB
6
// ...
7
8
myVar = 5; // Error! myVar is not defined in GA only in BB side
9
10
// this will be works:
11
let myVar;
12
myVar = 5;
13
14
Bot.sendMessage("ok") // Error! It is BJS not GA code!
15
16
let botName = bot.name; // Will be worked
17
let myData = options.myData; // Will be worked if you pass
18
// options.myData with Bot.run(
19
// { command: "/thisCmd", options: { myData: "test" } })
20
}
Copied!
​

Permissions

You need set permissions for Google App Script
Run Libs.GoogleAppLib.runin first time. You can have like such error:
Error on Google App script: "Exception"
"The script does not have permission to perform that action. Required permissions: (https://www.googleapis.com/auth/calendar || https://www.googleapis.com/auth/calendar.readonly || https://www.google.com/calendar/feeds)"
If you have such error you need set access rights.

Granting access rights via manifest file

Full help available here. From that help:
You can explicitly set the scopes your script project uses by editing its manifest file. The manifest field oauthScopes is an array of all scopes used by the project. To set your project's scopes, do the following:
  1. 1.
    Open the script project (script from step 2) in the Apps Script editor.
  2. 2.
    In the menu, select File > Project properties.
  3. 3.
    Select the Scopes tab.
  4. 4.
    Review the scopes your script currently requires and determine what changes need to be made. Click Cancel when finished.
  5. 5.
    If the manifest file appsscript.json isn't visible in the left nav bar, select the View > Show manifest file menu item.
  6. 6.
    Select the appsscript.json file in the left nav to open it.
  7. 7.
    Locate the top-level field labeled oauthScopes. If it is not present, you can add it.
  8. 8.
    The oauthScopes field specifies an array of strings. To set the scopes your project uses, replace the contents of this array with the scopes you want it to use. For example:
    1
    {
    2
    "timeZone": "Asia/Tokyo",
    3
    "dependencies": {
    4
    },
    5
    "webapp": {
    6
    "access": "ANYONE_ANONYMOUS",
    7
    "executeAs": "USER_DEPLOYING"
    8
    },
    9
    "exceptionLogging": "STACKDRIVER",
    10
    "oauthScopes": ["https://www.googleapis.com/auth/script.send_mail",
    11
    "https://www.googleapis.com/auth/script.external_request",
    12
    "https://www.googleapis.com/auth/spreadsheets"],
    13
    "runtimeVersion": "V8"
    14
    }
    Copied!
  9. 10.
    Save the manifest file using Ctrl+S or the Save file icon in the menu bar.
  10. 11.
    Publish your app again (see step 3)
​

Debugging

Run Libs.GoogleAppLib.runin first time. Then:
  • go to Google App Script Editor (See step 2)
  • select "Debug" function on Tab
  • press "Run" button:
Google app is runs. Bot will sent execution result to you. Also you can receive email with error description.
You can use "debug" function anytime for debugging

Links

​Google App script home page
Google App Script examples - good examples for inspiration
Stack Overflow answers - ask your questions on SO
​Videos - Check out the Apps Script videos on YouTube
​Reference - The reference documentation provided in this section describes the various Apps Script services and the Apps Script manifest file structure.
Last modified 1yr ago