Bots.Business - Help
  • Welcome
  • Getting started
  • Create bot from Google Table
  • App
    • Reset or Update Your Password
  • Commands
    • Answer
    • Aliases
    • Keyboard
    • Groups
    • Wait for answer
    • Auto Retry (AR)
  • Coding: BJS
    • Variables
    • Bot functions
    • Message broadcasting and editing
    • User functions
    • Properties
    • Always running commands
    • Error command: "!"
    • Lists
      • Migration from properties to list
    • Api functions
    • BB Admin functions
    • Admin Panel
    • Send HTTP request
    • Web App
    • Caching
    • Inline Bot
    • BJS Security
    • Good coding practices
    • Top errors
  • Git
    • Import bot from Git repository
    • Export bot to Git repository
    • Repository structure
    • File: bot.json
    • Automatic importing on Git push
  • Iterations. How to reduce theys?
  • Limitations
  • Cloud
  • Reports
  • Deep Linking - pass any params on Bot starting
  • How to link chat account with BB account?
  • BB Inspection
  • Protected bot
  • VS Code
  • How to...
  • Smart Bot
    • Overview
    • Lang File
    • SmartBot
    • SmartTasker
    • Amount Dialog
  • Libs
    • What it is - Libs?
    • Libs development
    • RefferalLib
    • ResourcesLib
    • Random
    • MembershipChecker (MCL)
    • Cooldown Lib
    • CurrencyConverter
    • Lang
    • TopBoardLib
    • QiwiPayments
    • Coinbase (CB)
    • CoinPayments (CP)
    • OxaPay
    • CryptoJS
    • CurrencyQuote
    • GoogleApp
    • GoogleTableSync
    • Guard
    • Webhooks lib
    • DateTimeFormat Lib
  • Store
    • BB Point Bot
    • Welcome bot
    • Help bot
    • SRB Demo Keyboard Tools
Powered by GitBook
On this page
  • Getting started
  • Easy setup
  • Detail setup
  • Using
  • Permissions
  • Granting access rights via manifest file
  • Debugging
  • Links

Was this helpful?

  1. Libs

GoogleApp

PreviousCurrencyQuoteNextGoogleTableSync

Last updated 2 years ago

Was this helpful?

Use this lib to connect BJS with

Getting started

Easy setup

  1. Install GoogleAppLib and WebhooksLib to your bot

Detail setup

1. Create new App Script project

2. Add Code.gs

You will need to contact your Google Apps administrator, or else use a Gmail account.)

3. Deploy as web app

Now click Deploy. You may be asked to review permissions now. Project version - always "New".

Cloick in Deploy button. You will have Public App url.

4. Public App URL

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.

5. Add permissions

6. Install GoogleAppLib and WebhooksLib to bot

Go to App > Libs and install GoogleAppLib and WebhooksLib

7. Create setup command:

/setup

// replace with your URL, obtained in step 4
Libs.GoogleApp.setUrl("https://script.google.com/macros/*******");

Using

Use any Google App script in BJS now

function GACode(){
   // Google App Script code here
   // Please note: this function is runs on GA not BB
   // ...
}

// BJS
Libs.GoogleApp.run({
  code: GACode, // Function with Google App code
  onRun: "onRun", // Optional. This command will be executed after run
  email: "my@email.com" // Optional. Email for errors,
  // debug: true // For debug. Default is false
});

Example for command /task

function GACode(){
  // translation from English to France
  var trans = LanguageApp.translate(params, 'en', 'fr');

  // make Google Calendar event
  var event = CalendarApp.getDefaultCalendar()
    .createEventFromDescription('Lunch with ' + user.first_name + ', Friday at 1PM');
  
  // send Email
  MailApp.sendEmail({
    to: "help@example.com",
    subject: "hello from bot " + bot.name,
    htmlBody: "<h1>Hello!</h1>How are you?<br>" +
        "We have message to bot: " + message
  });

  // ...
  // Use all power of Google App Script!
  
  // return result as JSON
  return { event: event, trans: trans }
}

Libs.GoogleApp.run({
  code: GACode,
  onRun: "onRun",
  // email: "test@example.com" // email for errors
  // debug: true // default false
});

You can turn on debug flag with true

command onRun

Bot.sendMessage(inspect(options))

GACode - it is isolated function with Google App code. It can not have BJS code like Bot.sendMessage and etc. Only GA code!

let myVar = "will not works";

options.myVar = "will be works";

function GACode(){
   // Google App Script code here
   // Please note: this function is runs on GA not BB
   // ...
   
   myVar = 5; // Error! myVar is not defined in GA only in BB side
   
   // this will be works:
   let myVar;
   myVar = 5;
   
   Bot.sendMessage("ok")  // Error! It is BJS not GA code!
   
   let botName = bot.name; // Will be worked
   let myVar = options.myVar; // Will be worked
}

Permissions

You need set permissions for Google App Script

Run Libs.GoogleApp.run()in first time. You can have like such error:

Error on Google App script: "Exception"

If you have such error you need set access rights.

Granting access rights via manifest file

  1. Open the script project.

  2. At the left, click Project Settings settings.

  3. Select the Show "appsscript.json" manifest file in editor checkbox:

At the left, click Editor code.

At the left, click the appsscript.json file.

Locate the top-level field labeled oauthScopes. If it's not present, you can add it.

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:

{
  "timeZone": "Asia/Tokyo",
  "dependencies": {
  },
  "webapp": {
    "access": "ANYONE_ANONYMOUS",
    "executeAs": "USER_DEPLOYING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": ["https://www.googleapis.com/auth/script.send_mail",
                  "https://www.googleapis.com/auth/script.external_request",
                  "https://www.googleapis.com/auth/spreadsheets"],
  "runtimeVersion": "V8"
}
  1. Save the manifest file using Ctrl+S or the Save file icon in the menu bar.

Debugging

Run Libs.GoogleAppLib.runin first time. Then:

  • select "debug" function on Tab

  • press "Debug" 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

Copy this to your Google account

as web app

Try to open web app via app

Create command

After this step you can this Lib and .

This is detail setup description. Please try to use before it.

Go to and create new project by button:

Paste the script from into the script code editor and hit Save.

You need to add

Then you can code

But you can use and pass data with for Bot.run method.

"The script does not have permission to perform that action. Required permissions: ( || || )"

Full help available . From that help:

", " - is mandatory scope

Publish your app again (see )

go to Google App Script Editor (See )

Also you can open web app by url (see ) in incognito mode. And look for any errors. For example we have permission error here:

home page

Google App Script - good examples for inspiration

Stack Overflow - ask your questions on SO

- Check out the Apps Script videos on YouTube

- The reference documentation provided in this section describes the various Apps Script services and the Apps Script manifest file structure.

table
https://script.google.com
above
https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/calendar.readonly
https://www.google.com/calendar/feeds
here
https://www.googleapis.com/auth/script.send_mail
https://www.googleapis.com/auth/script.external_request
Google App script
examples
answers
Videos
Reference
Deploy
Public App url
setup
use
debug
easy setup
permissions
debug
step 3
step 2
step 5
Google App Script
variables
options