Skip to content

Commit

Permalink
refactor: Use google's application default credentials instead of usi…
Browse files Browse the repository at this point in the history
…ng multiple env vars

- This is the first step to reuse service accounts with BigQuery in the future
  • Loading branch information
MrOrz committed Apr 27, 2023
1 parent 36de776 commit abb5251
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
6 changes: 3 additions & 3 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ REVIEW_REPLY_BUFFER={"seconds":0,"minutes":0,"hours":-12,"days":0}
# LINE add friend url, used for the message to guide user from LINE Notify to cofacts
LINE_FRIEND_URL=https://line.me/R/ti/p/@cofacts

# Google service
GOOGLE_APPLICATION_CREDENTIALS=<path-to-service-account-credential>

# Dialogflow
DAILOGFLOW_CLIENT_EMAIL=
DAILOGFLOW_PRIVATE_KEY=
DAILOGFLOW_PROJECT_ID=
DAILOGFLOW_LANGUAGE=
DAILOGFLOW_ENV=

Expand Down
29 changes: 15 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,26 @@ $ npm run notify
$ node build/scripts/scanRepliesAndNotify.js
```

### Dialogflow
### Google cloud services

We use dialogflow to detect if user is chatting with bot.
If userinput matches one of dialogflow intents, we can directly return predefined responses in that intent.
rumors-line-bot uses Google cloud services that is authenticated and authorized using Google Cloud
service accounts and [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials).

To use Dialogflow,
Please create a service account under the project, download its key and use `GOOGLE_APPLICATION_CREDENTIALS` env var to
provide the path to your downloaded service account key. See [documentation](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key) for detail.

1. You should [create a project](https://cloud.google.com/dialogflow/es/docs/quick/setup#project) and take note of the project ID then [enable api](https://cloud.google.com/dialogflow/es/docs/quick/setup#api).
2. [Build an agent](https://cloud.google.com/dialogflow/es/docs/quick/build-agent).
3. You will get a JSON file after [seting up authentication](https://cloud.google.com/dialogflow/es/docs/quick/setup#auth), copy `client_email` and `private_key` in the file.
4. sets in `.env` file
#### Dialogflow

```
DAILOGFLOW_CLIENT_EMAIL=<paste client_email get from step3 here>
DAILOGFLOW_PRIVATE_KEY=<paste private_key get from step3 here>
DAILOGFLOW_PROJECT_ID=<paste project_id get from step1 here>
```
We use Dialogflow to detect if user is chatting with bot.

Optional env variables:
If user input matches one of dialogflow intents, we can directly return predefined responses in that intent.

To use Dialogflow, please do the following setup:

1. Please ensure your [GCP project](https://cloud.google.com/dialogflow/es/docs/quick/setup#project) has [enabled Dialogflow api](https://cloud.google.com/dialogflow/es/docs/quick/setup#api).
2. [Build an agent](https://cloud.google.com/dialogflow/es/docs/quick/build-agent) connected to the GCP project.
3. Please ensure the service account has `dialogflow.sessions.detectIntent` permission.
4. Set these env variables (optional):
- `DAILOGFLOW_LANGUAGE` : Empty to agent's default language, or you can specify a [language](https://cloud.google.com/dialogflow/es/docs/reference/language).
- `DAILOGFLOW_ENV` : Default to draft agent, or you can create different [versions](https://cloud.google.com/dialogflow/es/docs/agents-versions).

Expand Down
29 changes: 18 additions & 11 deletions src/lib/detectDialogflowIntent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
import dialogflow from '@google-cloud/dialogflow';
import crypto from 'crypto';

const projectId = process.env.DAILOGFLOW_PROJECT_ID;
const credentials = {
client_email: process.env.DAILOGFLOW_CLIENT_EMAIL,
// https://stackoverflow.com/questions/39492587/escaping-issue-with-firebase-privatekey-as-a-heroku-config-variable/41044630#41044630
private_key: (process.env.DAILOGFLOW_PRIVATE_KEY || '').replace(/\\n/g, '\n'),
};
// const projectId = process.env.DAILOGFLOW_PROJECT_ID;
// const credentials = {
// client_email: process.env.DAILOGFLOW_CLIENT_EMAIL,
// // https://stackoverflow.com/questions/39492587/escaping-issue-with-firebase-privatekey-as-a-heroku-config-variable/41044630#41044630
// private_key: (process.env.DAILOGFLOW_PRIVATE_KEY || '').replace(/\\n/g, '\n'),
// };

// https://googleapis.dev/nodejs/dialogflow/latest/v2beta1.SessionsClient.html
const sessionClient = new dialogflow.SessionsClient({ credentials });
const sessionClient = new dialogflow.SessionsClient(/* { credentials } */);
let projectId = null;
sessionClient
.getProjectId()
.then((id) => {
if (!id) return;

projectId = id;
console.log(`[Dialogflow] Connected to project ID = ${id}`);
})
.catch((e) => ('[Dialogflow]', e));

export default async function (input) {
if (!projectId || !credentials.client_email || !credentials.private_key) {
console.log(
'[Dialogflow] Skip detecting intent, one of env variables not set.'
);
if (!projectId) {
return;
}
// https://cloud.google.com/dialogflow/es/docs/api-overview#sessions
Expand Down

0 comments on commit abb5251

Please sign in to comment.