Skip to content

Latest commit

 

History

History
336 lines (213 loc) · 15.6 KB

README.md

File metadata and controls

336 lines (213 loc) · 15.6 KB

Revolutionising Communication Through Google Actions - Powered By Rocket Chat


Index


Billing Enabled Compulsory

Required for running this action This action uses Firebase Cloud Functions to make an HTTP request to a non-Google service. The free Firebase Spark Plan only allows outbound network calls to Google services. If you plan to run the sample, you will need to temporarily upgrade to a Firebase plan that allows for outbound networking, such as the Blaze Plan, also called Pay as you go.


Setup Instructions

Prerequisites

  1. Node.js (> v8.10)
  2. Firebase CLI
  3. Rocket Chat Server updated to Release 1.0.0-rc3 or later

Configuration

Actions Console

  1. From the Actions on Google Console, add a new project > Create Project > under More options > Conversational
  2. From the left navigation menu under Build > Actions > Add Your First Action > BUILD (this will bring you to the Dialogflow console) > Select language and time zone > CREATE.
  3. In the Dialogflow console, go to Settings ⚙ > Export and Import > Restore from zip using the agent.zip in this sample's directory.

Firebase Deployment

  1. Download and install Node.js

  2. Set up and initialize the Firebase CLI. If the following command fails with an EACCES error, you may need to change npm permissions.

    npm install -g firebase-tools

  3. Authenticate the firebase tool with your Google account:

    firebase login

  4. Clone repository to your local machine.

    git clone https://github.com/PrajvalRaval/google-action-rocketchat.git

  • Then, change directory to google-action-rocketchat.

    cd google-action-rocketchat

  1. Initialize Firebase

    firebase init

  2. You'll be asked to select which Firebase CLI features you want to setup for your Actions project. Choose Functions then press Enter to confirm and continue.

  3. Associate the firebase tool with your Actions project by selecting it using the arrow keys to navigate the projects list.

  • Note: You can skip this step by selecting [don't setup a default project], but then you will need to do this association later using the command firebase use --project.
  1. After choosing the project, the firebase tool will start the Functions setup asking you what language you want to use. Select JavaScript using the arrow keys and press Enter to continue.

  2. For Do you want to use ESLint to catch probable bugs and enforce style? type N and press enter.

  3. For File functions/package.json already exists. Overwrite? type N and press enter.

  4. For File functions/index.js already exists. Overwrite? type N and press enter.

  5. Get the project dependencies by typing Y to the prompt:

  • Do you want to install dependencies with npm now?.

  • Once the setup is completed, you'll see an output similar to the following:

    ✔ Firebase initialization complete!

  1. Install the actions-on-google dependency by using following commands,

    cd functions

    npm install actions-on-google

  2. Get the fulfillment dependencies and deploy the fulfillment function:

    npm install

    In Order to deploy our config we need to set some temporary Firebase Environment Variables by running the following command:

    firebase functions:config:set envariables.server_url="temp_url" envariables.oauth_service_name="temp_oauth" envariables.clientid="temp_clientid"
    

    Then run,

    firebase deploy --only functions

  3. The deployment takes a few minutes. Once completed, you'll see output similar to the following. You'll need the Function URL to enter in Dialogflow.

✔  Deploy complete!

Project Console: https://console.firebase.google.com/project/myprojectname-ab123/overview
Function URL (factsAboutGoogle): https://us-central1-myprojectname-ab123.cloudfunctions.net/dialogflowFirebaseFulfillment

  1. In the Dialogflow console's navigation menu, click Fulfillment, toggle the Webhook button to ENABLED , and replace the url in the URL field with your Function URL that was returned after the deploy command > SAVE..

dialogflow-deploy-fulfillment-00

  1. From the left navigation menu, click Integrations > Integration Settings under Google Assistant > Enable Auto-preview changes. (If not already enabled)

Enabling Billing

  1. Go to your Firebase Console and select your project from list.

  2. In the bottom side of left menu you will see an Upgrade button and select Blaze Plan from the list.

Screenshot 2019-05-16 at 7 42 52 PM

Configuring Account Linking

  1. Login to your Action Developer Console and select Rocket Chat project on the list.

  2. Go to Settings ⚙ -> Project Setting and Copy Project ID and paste it somewere safe as we will need it later.

  3. Now click on Advance Options on Bottom left of the screen and select Account Linking from the list.

  4. In Account creation,

  • Select No, I only want to allow account creation on my website

  • Click Next.

  1. In Linking type, Select OAuth from the list and Set Authorisation code as grant type. Click Next.

  2. Now we need to fill up the Authorization URI, Access Token URI, Client ID, Client Secret which we will generate on our rocket chat server.

  • Note You need to be admin of the server to proceed with the further steps.
  1. In a new tab go to your Server -> Three Dot Menu -> Administration.

Go to Server -> Administration

  1. Click on OAuth Apps.

Click on OAuth Apps

  1. Click on New Application on top right. Now we need to give it an Application Name and a Redirect URI.

  2. For Application Name use "rcgoogleaction". This can be anything else as well.

  • For the Redirect URI, copy https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID and paste it in the Redirect URI field. (Paste the Project Id we copied earlier here)
  1. You'll see it automatically generating Client ID, Client Secret, Authorization URL, and Access Token URL. Now copy these from the oauth app page and paste it in the Client ID, Client Secret, Authorization URL and Access Token URL fields in the Client information on Google Action Console Page. Click Next.

  2. Copy Client ID and paste it somewhere safe as we will be using it for setting up Firebase Environent Variables.

  3. In Configure your client (optional) section,

  • Tick mark(✔) Google to transmit clientID and secret via HTTP basic auth header

  • Click Next.

  1. In Testing instructions,
  • Provide an Email Id and Password as per instructions

  • Click on Save.

  1. We are done on setting our OAuth App which will give us the access token to use for logging in. But for that we need to also enable custom oauth login for our server which we will do in the next steps.

  2. Go to your Server -> Three Dot Menu -> Administration. Scroll down on your left and select OAuth and on top right click on Add custom OAuth.

Add custom OAuth

  1. Give a unique name in lower case for the custom oauth. For example enter "googleaction". Click on Send. Copy and paste this name somewhere safe as we will be using it for setting up Firebase Environent Variables.

  2. You will now be provided a few fields some of which will be prefilled. We only need to change a few.

  • Change the Enable to true.

  • In the URL field enter https://yourserverurl/api/v1

  1. Finally at the bottom switch Merge users to true. We don't need to make any other changes here.

  2. Click on Save Changes on top.

  3. Go to your console add your Server URL,OAuth Service Name and Client ID to the following command and run,

    firebase functions:config:set envariables.server_url="https://YOUR.SERVER.chat" envariables.oauth_service_name="YOUR_CUSTOM_OAUTH_NAME" envariables.clientid="YOUR_CLIENT_ID"
    

Then run,

 firebase deploy --only functions
  1. Finally We Are Done !

AWS Lambda Deployment

The fulfillment code can also be deployed in AWS as a lambda function as an alternative to using Firebase.

  1. Navigate to the AWS Lambda console.

  2. Create a new function with the following settings.

    • Author From Scratch
    • Runtime: Node.js 12.x
    • Permissions: Create a new role with basic Lambda Permissions
  3. Add the following Environment Variables

OAUTH_SERVICE_NAME => Name of the Custom OAuth service created in Rocket Chat OAuth Settings
SERVER_URL => Rocket Chat server url (https://YOUR.SERVER.chat)
  1. Add an API Gateway trigger with the following settings.

    • Create an API
    • API Type: HTTP API
    • Security: Open
  2. Copy the following value somewhere.

    • Click on API Gateway > Details > API endpoint
  3. Navigate to Dialogflow > Fulfillment section: https://dialogflow.cloud.google.com/#/agent/project_id/fulfillment

    • Paste the API endpoint in the Webhook URL field.
  4. To upload the backend code in Lambda function

    • Navigate to ./functions directory in the project repository
    • Select all files and compress into a zip file.
    • In the Functions Code section of AWS Lambda Function
    • Actions > Upload a .zip file
    • Select the compressed zip file
    • Click Save.

Running this Action

  • You can test your Action on any Google Assistant-enabled device on which the Assistant is signed into the same account used to create this project. Just say or type, “OK Google, talk to my test app”.
  • You can also use the Actions on Google Console simulator to test most features and preview on-device behavior.

Development

Local Development Setup

The project can be run on local development for debugging. The steps are listed below.

  1. Create an environment file inside the functions folder.
    • ./functions/.env
  2. Add the following variables
SERVER_URL=<your server url>
OAUTH_SERVICE_NAME=<custom OAuth service name>
  1. Start the server locally by running npm run local
  2. Create a tunnel using ngrok ngrok http 3000
  3. Navigate to Dialogflow > Fulfillment section: https://dialogflow.cloud.google.com/#/agent/project_id/fulfillment
    • Paste the URL generated by ngrok in the Webhook URL field.

Files

  1. ./functions/index.js

    • Add new handlers for intents, modify intent logic to customize the skill.
  2. ./functions/helperFunctions.js

    • Enhance the functionality of the source code by adding functions that are required for sending requests and various other logics.
  3. ./functions/config.js

    • Imports Firebase config into our code.
  4. ./functions/apiEndpoints.js

    • REST API endpoint URLs.
  5. ./functions/locales/*.json

    • Contains responses for different locales. If you are planning to contribute to your locale you will need to change response strings here.

i18n

By default we support development for EN,PT and HI locales and are included in our Dialogflow agent. But we do have developed a base locale resource file for every supported Google Action locale, for developers worldwide to develop this action in their own language. If you are interested in contributing to your locale please follow this steps.

  1. Go to your Dialogflow Console

  2. In Dialogflow console click on + (Below Settings ⚙ Sign) > Select Additional Language > Your Locale after that click on SAVE

  3. Now click on Screenshot 2019-05-20 at 8 02 57 PM and select Your Locale from the list.

  4. You will need to add Training phrases one by one to every intent. Select Intents > select one intent from the list > Training phrases. Add some training phrases that include every Parameter required as headers data to send a POST request to the server, in the utterance and make sure it is native speaker friendly.

  5. While creating Training Phrases, make sure that you give composite entities parameters to the utterances. E.g. If your creating an utterance with username and channelname, select @sys.any as their entity type. Refer to EN locale training phrases for more depth understanding.

  6. For Backend setup, go to ./functions/index.js and add your locale name to i18n.configure > locales array.

    i18n.configure({
    locales: ['en-US', 'en-GB', 'en-AU', 'en-CA', 'en-IN', 'pt-BR', 'hi-IN'],
    directory: __dirname + '/locales',
    defaultLocale: 'en-US',
    objectNotation : true
    });
    

    NOTE: It is same name as in locales folder.

  7. Locale File: ./functions/locales/*.json ,It is highly likely that your locale may be behind because of new function additions, so we highly suggest to make sure that every object in your locale is up-to-date with our English locale file. If not make sure to add those JSON blocks to your locale before deployment. To do that simply copy and paste missing blocks from English locale file and simply translate the response strings into your own language.

We have developed those base locales using a translation software and translations of response may not be always accurate so feel free to update those response strings correctly as per your native language.

For more details on i18n please check out Fulfillment Localization and I18n-node


References & Issues


Our Alexa Skill

Are you an Alexa Developer, We also have an Alexa Repository for you to contribute on. Any form of help is appreciated.