Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Utilities/googlechatspaceintegration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

# set up the environment to use google workspace environment
follow the below documentation for the set up:
link: https://developers.google.com/workspace/chat/api/guides/quickstart/nodejs

# set up webhook triggers
1. Go to script.google.com.
2. Go to "getting started" tab.
3. Choose the prebuilt "ChatApp" project.
4. Inside "onMessage" event listener replace your code.
5. The code inside the onMessage event listener sholud call your '/webhook'
endpoint which sends the message and thread name as request body.
6. Now deploy the project as add-on and copy the deploymentId.
7. Go to google cloud console, configure your google chat App.
8. In the connection settings choose "Apps script".
9. Paste the deploymentId.
10. Save the configuration.

# set up incoming webhooks
1. go to your google chats workspace.
2. Open space settings.
3. Go to Apps & Integrations.
4. Add the webhook and copy the url.
5. paste that webhook url in you .env file as follows:
WEBHOOK_URL = <your-webhook-url>

# To run the server
Go to root folder
1. install dependencies: npm install
2. to run server: node index.js
7 changes: 7 additions & 0 deletions Utilities/googlechatspaceintegration/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules

test/services/servicesMockData.js

json

.env
11 changes: 11 additions & 0 deletions Utilities/googlechatspaceintegration/src/config/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require("mongoose");

module.exports = async () => {
try {
mongoose.connect(process.env.MONGO_URL);
console.log("connected to DB");
}
catch(error) {
console.log("Error in connecting DB "+error);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { Router } = require('express');
const router = Router({ strict: true });
const services = require('../services/services');

router.post('/webhook', services.getAnswer);
router.post('/one-week-threads', services.pushOneWeekThreads);

module.exports = router;
16 changes: 16 additions & 0 deletions Utilities/googlechatspaceintegration/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express')
const dotenv = require('dotenv');
const configDB = require('./config/db');

const app = express()

dotenv.config();
configDB();

app.use(express.json())

app.use(require('./controllers/routes'));

app.listen(5000, ()=>{
console.log("Server running");
});
5 changes: 5 additions & 0 deletions Utilities/googlechatspaceintegration/src/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
testMatch: [
'<rootDir>/test/services/*.test.js'
]
}
49 changes: 49 additions & 0 deletions Utilities/googlechatspaceintegration/src/middlewares/authorize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const fs = require('fs').promises;
const path = require('path');
const process = require('process');
const { authenticate } = require('@google-cloud/local-auth');
const { auth } = require('google-auth-library');

const CREDENTIALS_PATH = path.join(process.cwd(), './json/credentials.json');

async function loadSavedCredentialsIfExist(TOKEN_PATH) {
try {
const content = await fs.readFile(TOKEN_PATH);
const credentials = JSON.parse(content);
return auth.fromJSON(credentials);
} catch (err) {
console.log(err);
return null;
}
}


async function saveCredentials(client, TOKEN_PATH) {
const content = await fs.readFile(CREDENTIALS_PATH);
const keys = JSON.parse(content);
const key = keys.installed || keys.web;
const payload = JSON.stringify({
type: 'authorized_user',
client_id: key.client_id,
client_secret: key.client_secret,
refresh_token: client.credentials.refresh_token,
});
await fs.writeFile(TOKEN_PATH, payload);
}

async function authorize(SCOPES,TOKEN_PATH) {
let client = await loadSavedCredentialsIfExist(TOKEN_PATH);
if (client) {
return client;
}
client = await authenticate({
scopes: SCOPES,
keyfilePath: CREDENTIALS_PATH,
});
if (client.credentials) {
await saveCredentials(client, TOKEN_PATH);
}
return client;
}

module.exports = authorize;
11 changes: 11 additions & 0 deletions Utilities/googlechatspaceintegration/src/models/thread.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require('mongoose');

const threadSchema = mongoose.Schema({
name: { type: String, unique: true },
messages: [String],
createTime: Number
});

const threadModel = mongoose.model('threads', threadSchema);

module.exports = threadModel;
Loading