diff --git a/language/slackbot/demo_bot.js b/language/slackbot/demo_bot.js index 16486f0f9b..853c96da8d 100755 --- a/language/slackbot/demo_bot.js +++ b/language/slackbot/demo_bot.js @@ -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')); @@ -79,62 +85,55 @@ const TABLE_SQL = `CREATE TABLE if not exists entities ( ts integer );`; -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, ''); - +function startController () { // Create the table that will store entity information if it does not already // exist. db.run(TABLE_SQL); - controller.spawn({token: token}).startRTM(err => { - if (err) { - console.error('Failed to start controller!'); - console.error(err); - throw err; - } - }); + controller + .spawn({ token: token }) + .startRTM((err) => { + if (err) { + console.error('Failed to start controller!'); + console.error(err); + process.exit(1); + } + }); - return ( - controller - // If the bot gets a DM or mention with 'hello' or 'hi', it will reply. You - // can use this to sanity-check your app without needing to use the NL API. - .hears( - ['hello', 'hi'], - ['direct_message', 'direct_mention', 'mention'], - handleSimpleReply - ) - // If the bot gets a DM or mention including "top entities", it will reply with - // a list of the top N most frequent entities used in this channel, as derived - // by the NL API. - .hears( - ['top entities'], - ['direct_message', 'direct_mention', 'mention'], - handleEntitiesReply - ) - // For any posted message, the bot will send the text to the NL API for - // analysis. - .on('ambient', handleAmbientMessage) - .on('rtm_close', startBot) - ); + return controller + // If the bot gets a DM or mention with 'hello' or 'hi', it will reply. You + // can use this to sanity-check your app without needing to use the NL API. + .hears( + ['hello', 'hi'], + ['direct_message', 'direct_mention', 'mention'], + handleSimpleReply + ) + // If the bot gets a DM or mention including "top entities", it will reply with + // a list of the top N most frequent entities used in this channel, as derived + // by the NL API. + .hears( + ['top entities'], + ['direct_message', 'direct_mention', 'mention'], + handleEntitiesReply + ) + // For any posted message, the bot will send the text to the NL API for + // analysis. + .on('ambient', handleAmbientMessage) + .on('rtm_close', startBot); } -function startBot(bot) { +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}).startRTM(err => { - if (err) { - console.error('Failed to start controller!'); - console.error(err); - throw err; - } - }); + bot + .spawn({ token: token }) + .startRTM((err) => { + if (err) { + console.error('Failed to start controller!'); + console.error(err); + process.exit(1); + } + }); } function handleSimpleReply(bot, message) { @@ -168,72 +167,73 @@ function handleEntitiesReply(bot, message) { }); } -function analyzeEntities(text, ts) { +function analyzeEntities (text, ts) { // Instantiates a client - const language = Language(); + const client = new language.LanguageServiceClient(); // Instantiates a Document, representing the provided text const document = { // The document text, e.g. "Hello, world!" content: text, // The type of content to analyze - type: 'PLAIN_TEXT', + type: 'PLAIN_TEXT' }; // Detects entities in the document - return language.analyzeEntities({document: document}).then(results => { - const entities = results[0].entities; - entities.forEach(entity => { - const name = entity.name; - const type = entity.type; - const salience = entity.salience; - let wikiUrl = ''; - if (entity.metadata.wikipedia_url) { - wikiUrl = entity.metadata.wikipedia_url; - } - - // Uncomment this to see the entity info logged to console: - // console.log(`${name}, type: ${type}, w url: ${wikiUrl}, salience: ${salience}, ts: ${ts}`); - - db.run('INSERT INTO entities VALUES (?, ?, ?, ?, ?);', [ - name, - type, - salience, - wikiUrl, - Math.round(ts), - ]); + return client.analyzeEntities({ document: document }) + .then((results) => { + const entities = results[0].entities; + entities.forEach((entity) => { + const name = entity.name; + const type = entity.type; + const salience = entity.salience; + let wikiUrl = ''; + if (entity.metadata.wikipedia_url) { + wikiUrl = entity.metadata.wikipedia_url; + } + + // Uncomment this to see the entity info logged to console: + // console.log(`${name}, type: ${type}, w url: ${wikiUrl}, salience: ${salience}, ts: ${ts}`); + + db.run( + 'INSERT INTO entities VALUES (?, ?, ?, ?, ?);', + [name, type, salience, wikiUrl, Math.round(ts)] + ); + }); + + return entities; }); - - return entities; - }); } -function analyzeSentiment(text) { +function analyzeSentiment (text) { // Instantiates a client - const language = Language(); + const client = new language.LanguageServiceClient(); // Instantiates a Document, representing the provided text const document = { // The document text, e.g. "Hello, world!" content: text, // The type of content to analyze - type: 'PLAIN_TEXT', + type: 'PLAIN_TEXT' }; // Detects the 'sentiment' of some text using the NL API - return language.analyzeSentiment({document: document}).then(results => { - const sentiment = results[0]; - - // Uncomment the following lines to log the sentiment to the console: - // console.log(`Sentiment: ${sentiment}`) - // if (sentiment.score >= SENTIMENT_THRESHOLD) { - // console.log('Sentiment: positive.'); - // } else if (sentiment.score <= -SENTIMENT_THRESHOLD) { - // console.log('Sentiment: negative.'); - // } - - return sentiment; - }); + return client.analyzeSentiment({ document: document }) + .then((results) => { + 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) { + // console.log('Sentiment: negative.'); + // } + + return sentiment; + }); } function handleAmbientMessage(bot, message) { diff --git a/language/slackbot/package.json b/language/slackbot/package.json index e44564deb3..def5327315 100644 --- a/language/slackbot/package.json +++ b/language/slackbot/package.json @@ -21,14 +21,15 @@ "test": "repo-tools test run --cmd ava -- -T 20s --verbose system-test/*.test.js" }, "dependencies": { - "@google-cloud/language": "0.11.0", + "@google-cloud/language": "2.0.0", "botkit": "0.6.21", - "sqlite3": "3.1.13" + "sqlite3": "4.0.4" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.0.0", + "@google-cloud/nodejs-repo-tools": "3.0.0", "ava": "0.25.0", "proxyquire": "2.1.0", + "semistandard": "12.0.1", "sinon": "3.2.0" } }