Skip to content

Instantly share code, notes, and snippets.

@mattcam
Created August 17, 2018 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattcam/c5fa676eca9ba2b37cef0416d2900b92 to your computer and use it in GitHub Desktop.
Save mattcam/c5fa676eca9ba2b37cef0416d2900b92 to your computer and use it in GitHub Desktop.
inf-chat-bot
'use strict'
var _ = require('underscore');
var fs = require('fs');
var yaml = require('js-yaml');
var builder = require('botbuilder');
var restify = require('restify');
const {Wit} = require('node-wit');
var Log = require('log'), log = new Log('info');
require('dotenv').load()
const client = new Wit({
accessToken: process.env.WIT_SERVER_ACCESS_TOKEN
});
var intents = yaml.safeLoad(fs.readFileSync('training/intents.yml', 'utf8'));
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
var bot = new builder.UniversalBot(connector, function (session) {});
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, '/');
}
});
}
});
bot.on('conversationUpdate', function (message) {
if (message.membersAdded[0].id === message.address.bot.id) {
var reply = new builder.Message()
.address(message.address)
.textFormat('markdown')
.text(intents.interactions[intents.app.start].response.text);
bot.send(reply);
}
});
bot.recognizer({
recognize: function (context, done) {
var intent = { score: 1.0, intent: intents.app.default };
client.message(context.message.text, {})
.then((data) => {
if (Object.keys(data.entities).length) {
intent = { score: 1.0, intent: data.entities.intent[0].value };
}
done(null, intent);
})
.catch(console.error);
}
});
_.each(_.keys(intents.interactions), function(interaction) {
bot.dialog(interaction, function (session) {
if ('response' in intents.interactions[interaction]) {
if ('text' in intents.interactions[interaction].response) {
var message = new builder.Message(session)
.text(intents.interactions[interaction].response.text)
.textFormat("markdown");
session.send(message);
}
}
session.endDialog();
}).triggerAction({ matches: interaction });
})
app:
start: helpDialog
default: questionsDialog
interactions:
finishDialog:
response:
text:
- Nice talking to you and hope that the information helped you, bye for now.
helpDialog:
response:
text:
- Hello, I can help you with using influencerTips to get actionable suggestions to help you engage with your audience on social media.<br/><br/>Do you require help with getting started? suggestions? upgrading? registering for a conference call? something else?
questionsDialog:
response:
text:
- Thanks. Questions that I cannot provide help with are reviewed and followed up by the influencerTips team. Type 'help' for useful information. Feel free to share your comments about how we can improve this service.
registerDialog:
response:
text:
- Join influencerTips for 15 minute show and tell conference call tutorial about how to get found on social media using hashtags. This is a great opportunity to learn and ask questions about digital marketing. Enter your email for meeting invite.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment