Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slackbot example update to reflect package and api changes #879

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
merge fix
  • Loading branch information
amygdala committed Dec 14, 2018
commit 0b4251fc944de2e07d4370aa34906ec28b81e751
110 changes: 54 additions & 56 deletions language/slackbot/demo_bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,53 +86,54 @@ 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);

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');

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) {
Expand Down Expand Up @@ -166,7 +167,7 @@ function handleEntitiesReply(bot, message) {
});
}

function analyzeEntities(text, ts) {
function analyzeEntities (text, ts) {
// Instantiates a client
const client = new language.LanguageServiceClient();

Expand All @@ -175,7 +176,7 @@ function analyzeEntities(text, ts) {
// 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
Expand All @@ -191,23 +192,20 @@ function analyzeEntities(text, ts) {
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}`);
// 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),
]);
});
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 client = new language.LanguageServiceClient();

Expand All @@ -216,7 +214,7 @@ function analyzeSentiment(text) {
// 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
Expand All @@ -234,8 +232,8 @@ function analyzeSentiment(text) {
// console.log('Sentiment: negative.');
// }

return sentiment;
});
return sentiment;
});
}

function handleAmbientMessage(bot, message) {
Expand Down
18 changes: 9 additions & 9 deletions language/slackbot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
},
"main": "demo_bot.js",
"engines": {
"node": ">=8"
"node": ">=8.0.0"
},
"scripts": {
"test": "repo-tools test run --cmd ava -- -T 20s --verbose system-test/*.test.js"
},
"dependencies": {
"@google-cloud/language": "^2.0.0",
"botkit": "^0.6.20",
"sqlite3": "^4.0.4"
"@google-cloud/language": "2.0.0",
"botkit": "0.6.21",
"sqlite3": "4.0.4"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"ava": "^0.25.0",
"proxyquire": "^1.8.0",
"semistandard": "^12.0.1",
"sinon": "^3.2.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"
}
}
You are viewing a condensed version of this merge commit. You can view the full changes here.