Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
33 changes: 16 additions & 17 deletions language/slackbot/demo_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,19 @@ 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 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 Slackbot({clientSigningSecret: token});

// create our database if it does not already exist.
const db = new sqlite3.cached.Database(path.join(__dirname, './slackDB.db'));
Expand Down Expand Up @@ -80,13 +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.
db.run(TABLE_SQL);
Expand Down Expand Up @@ -125,8 +124,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 })
Expand Down Expand Up @@ -170,7 +167,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 = {
Expand All @@ -181,7 +178,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) => {
Expand All @@ -208,7 +205,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 = {
Expand All @@ -219,12 +216,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) {
Expand Down
14 changes: 7 additions & 7 deletions language/slackbot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
},
"main": "demo_bot.js",
"engines": {
"node": ">=4.3.2"
"node": ">=8"
},
"scripts": {
"lint": "semistandard '**/*.js'",
"pretest": "npm run lint",
"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"
}
}