Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
feat: validate required files and add timestamp to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ismarslomic committed Jun 9, 2022
1 parent 66c5d50 commit 45afa3b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
30 changes: 26 additions & 4 deletions assistant.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
"use strict";

const GoogleAssistant = require("google-assistant");
const fs = require("fs");

/** See https://github.com/endoplasmic/google-assistant for more info **/
const config = {
auth: {
keyFilePath: process.env.CLIENT_SECRET
|| "/usr/src/config/client_secret.json",
// where you want the tokens to be saved
// will create the directory if not already there
savedTokensPath: process.env.TOKEN || "/usr/src/config/tokens.json"
},
conversation: {
lang: process.env.LANGUAGE || 'en-GB', // defaults to en-GB, but try other ones, it's fun!
showDebugInfo: true, // default is false, bug good for testing AoG things,
isNew: true,
},
};

function Assistant() {
this.validateFiles = () => {
if (!checkFileExistsSync(config.auth.keyFilePath)) {
throw Error(
`Client Secret file at path '${config.auth.keyFilePath}' does not exist.`)
}

if (!checkFileExistsSync(config.auth.savedTokensPath)) {
throw Error(
`Tokens file at path '${config.auth.savedTokensPath}' does not exist.`)
}
}

this.cast = async (message) => {
const assistant = new GoogleAssistant(config.auth);
config.conversation.textQuery = `Broadcast ${message}`;

console.log("Sending message:", config.conversation.textQuery);
console.log(`Sending message (${config.conversation.lang}):`,
config.conversation.textQuery);

return new Promise((resolve, reject) => {
assistant
Expand Down Expand Up @@ -57,6 +67,18 @@ function Assistant() {
});
})
}

const checkFileExistsSync = filepath => {
let exists = true;

try {
fs.accessSync(filepath, fs.constants.F_OK);
} catch (e) {
exists = false;
}

return exists;
};
}

module.exports = Assistant;
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
"use strict";

require('log-timestamp');
const express = require("express");
const Assistant = require("./assistant");

const server = express()
const port = 8085;
const assistant = new Assistant();

// Validate required files
assistant.validateFiles()

server.use(express.json())

// Respond only on POST /broadcast route
Expand Down
47 changes: 40 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"homepage": "https://github.com/ismarslomic/google-assistant-broadcast#readme",
"dependencies": {
"express": "^4.18.1",
"google-assistant": "^0.7.0"
"google-assistant": "^0.7.0",
"log-timestamp": "^0.3.0"
}
}

0 comments on commit 45afa3b

Please sign in to comment.