From c2a5eabe3125ee9aad9f6a09dc85cd9a918d3f58 Mon Sep 17 00:00:00 2001 From: Amy Unruh Date: Sun, 11 Nov 2018 14:04:37 -0800 Subject: [PATCH 1/2] example update to reflect package and api changes --- language/slackbot/demo_bot.js | 30 +++++++++++++++--------------- language/slackbot/package.json | 14 +++++++------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/language/slackbot/demo_bot.js b/language/slackbot/demo_bot.js index 6f8c5242e1..13b573b490 100755 --- a/language/slackbot/demo_bot.js +++ b/language/slackbot/demo_bot.js @@ -44,11 +44,17 @@ See the README.md in this directory for more information about setup and usage. const Botkit = require('botkit'); const fs = require('fs'); -const Language = require('@google-cloud/language'); +const language = require('@google-cloud/language'); const path = require('path'); const sqlite3 = require('sqlite3').verbose(); -const controller = Botkit.slackbot({ debug: false }); +if (!process.env.SLACK_TOKEN_PATH) { + throw new Error('Please set the SLACK_TOKEN_PATH environment variable!'); +} + +var token = fs.readFileSync(process.env.SLACK_TOKEN_PATH, { encoding: 'utf8' }); +token = token.replace(/\s/g, ''); +const controller = new Botkit.slackbot({clientSigningSecret: token}); // create our database if it does not already exist. const db = new sqlite3.cached.Database(path.join(__dirname, './slackDB.db')); @@ -80,12 +86,6 @@ const TABLE_SQL = `CREATE TABLE if not exists entities ( );`; function startController () { - if (!process.env.SLACK_TOKEN_PATH) { - throw new Error('Please set the SLACK_TOKEN_PATH environment variable!'); - } - - let token = fs.readFileSync(process.env.SLACK_TOKEN_PATH, { encoding: 'utf8' }); - token = token.replace(/\s/g, ''); // Create the table that will store entity information if it does not already // exist. @@ -125,8 +125,6 @@ function startController () { function startBot (bot, cerr) { console.error('RTM closed'); - let token = fs.readFileSync(process.env.SLACK_TOKEN_PATH, { encoding: 'utf8' }); - token = token.replace(/\s/g, ''); bot .spawn({ token: token }) @@ -170,7 +168,7 @@ function handleEntitiesReply (bot, message) { function analyzeEntities (text, ts) { // Instantiates a client - const language = Language(); + const client = new language.LanguageServiceClient(); // Instantiates a Document, representing the provided text const document = { @@ -181,7 +179,7 @@ function analyzeEntities (text, ts) { }; // Detects entities in the document - return language.analyzeEntities({ document: document }) + return client.analyzeEntities({ document: document }) .then((results) => { const entities = results[0].entities; entities.forEach((entity) => { @@ -208,7 +206,7 @@ function analyzeEntities (text, ts) { function analyzeSentiment (text) { // Instantiates a client - const language = Language(); + const client = new language.LanguageServiceClient(); // Instantiates a Document, representing the provided text const document = { @@ -219,12 +217,14 @@ function analyzeSentiment (text) { }; // Detects the 'sentiment' of some text using the NL API - return language.analyzeSentiment({ document: document }) + return client.analyzeSentiment({ document: document }) .then((results) => { - const sentiment = results[0]; + const sentiment = results[0].documentSentiment; // Uncomment the following lines to log the sentiment to the console: // console.log(`Sentiment: ${sentiment}`) + // console.log(`Sentiment score: ${sentiment.score}`) + // console.log(`Magnitude: ${sentiment.magnitude}`); // if (sentiment.score >= SENTIMENT_THRESHOLD) { // console.log('Sentiment: positive.'); // } else if (sentiment.score <= -SENTIMENT_THRESHOLD) { diff --git a/language/slackbot/package.json b/language/slackbot/package.json index af58f30b61..d6014db0c5 100644 --- a/language/slackbot/package.json +++ b/language/slackbot/package.json @@ -15,7 +15,7 @@ }, "main": "demo_bot.js", "engines": { - "node": ">=4.3.2" + "node": ">=8" }, "scripts": { "lint": "semistandard '**/*.js'", @@ -23,15 +23,15 @@ "test": "repo-tools test run --cmd ava -- -T 20s --verbose system-test/*.test.js" }, "dependencies": { - "@google-cloud/language": "0.11.0", - "botkit": "0.5.7", - "sqlite3": "3.1.9" + "@google-cloud/language": "^2.0.0", + "botkit": "^0.6.20", + "sqlite3": "^4.0.4" }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "^3.0.0", - "ava": "0.25.0", - "proxyquire": "1.8.0", + "ava": "^0.25.0", + "proxyquire": "^1.8.0", "semistandard": "^12.0.1", - "sinon": "3.2.0" + "sinon": "^3.2.0" } } From 1547a0237382d4fbe1d1d630e12709985fc25551 Mon Sep 17 00:00:00 2001 From: Franziska Hinkelmann Date: Sun, 11 Nov 2018 20:08:09 -0500 Subject: [PATCH 2/2] Use linter Ran the linter on the sample with `npm run lin -- --fix`. --- language/slackbot/demo_bot.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/language/slackbot/demo_bot.js b/language/slackbot/demo_bot.js index 13b573b490..f21ded086f 100755 --- a/language/slackbot/demo_bot.js +++ b/language/slackbot/demo_bot.js @@ -42,7 +42,7 @@ See the README.md in this directory for more information about setup and usage. 'use strict'; -const Botkit = require('botkit'); +const Slackbot = require('botkit').slackbot; const fs = require('fs'); const language = require('@google-cloud/language'); const path = require('path'); @@ -54,7 +54,7 @@ if (!process.env.SLACK_TOKEN_PATH) { var token = fs.readFileSync(process.env.SLACK_TOKEN_PATH, { encoding: 'utf8' }); token = token.replace(/\s/g, ''); -const controller = new Botkit.slackbot({clientSigningSecret: token}); +const controller = new Slackbot({clientSigningSecret: token}); // create our database if it does not already exist. const db = new sqlite3.cached.Database(path.join(__dirname, './slackDB.db')); @@ -86,7 +86,6 @@ const TABLE_SQL = `CREATE TABLE if not exists entities ( );`; function startController () { - // Create the table that will store entity information if it does not already // exist. db.run(TABLE_SQL);