botanalytics-ghome
Version:
Conversational analytics & engagement tool for chatbots
100 lines (76 loc) • 3.5 kB
JavaScript
;
const util = require('util');
const ViberBot = require('viber-bot').Bot;
const BotEvents = require('viber-bot').Events;
const TextMessage = require('viber-bot').Message.Text;
const Botanalytics = require('botanalytics').Viber(process.env.BOTANALYTICS_TOKEN, {
debug: true
});
const winston = require('winston');
const toYAML = require('winston-console-formatter');
var request = require('request');
function createLogger() {
const logger = new winston.Logger({
level: "debug" // We recommend using the debug level for development
});
logger.add(winston.transports.Console, toYAML.config());
return logger;
}
function say(response, message) {
response.send(new TextMessage(message));
}
function checkUrlAvailability(botResponse, urlToCheck) {
if (urlToCheck === '') {
say(botResponse, 'I need a URL to check');
return;
}
say(botResponse, 'One second...Let me check!');
var url = urlToCheck.replace(/^http:\/\//, '');
request('http://isup.me/' + url, function(error, requestResponse, body) {
if (error || requestResponse.statusCode !== 200) {
say(botResponse, 'Something is wrong with isup.me.');
return;
}
if (!error && requestResponse.statusCode === 200) {
if (body.search('is up') !== -1) {
say(botResponse, 'Hooray! ' + urlToCheck + '. looks good to me.');
} else if (body.search('Huh') !== -1) {
say(botResponse, 'Hmmmmm ' + urlToCheck + '. does not look like a website to me. Typo? please follow the format `test.com`');
} else if (body.search('down from here') !== -1) {
say(botResponse, 'Oh no! ' + urlToCheck + '. is broken.');
} else {
say(botResponse, 'Snap...Something is wrong with isup.me.');
}
}
})
}
const logger = createLogger();
if (!process.env.VIBER_PUBLIC_ACCOUNT_ACCESS_TOKEN_KEY) {
logger.debug('Could not find the Viber Public Account access token key in your environment variable. Please make sure you followed readme guide.');
return;
}
// Creating the bot with access token, name and avatar
const bot = new ViberBot(logger, {
authToken: process.env.VIBER_PUBLIC_ACCOUNT_ACCESS_TOKEN_KEY, // Learn how to get your access token at developers.viber.com
name: "Is It Up",
avatar: "https://raw.githubusercontent.com/devrelv/drop/master/151-icon.png" // Just a placeholder avatar to display the user
});
Botanalytics.attach(bot, (err) => {
console.log('Callback error: ' + util.inspect(err));
});
// The user will get those messages on first registration
bot.on(BotEvents.SUBSCRIBED, (message, response) => {
say(response, `Hi there ${response.userProfile.name}. I am ${bot.name}! Feel free to ask me if a web site is down for everyone or just you. Just send me a name of a website and I'll do the rest!`);
});
bot.on(BotEvents.SUBSCRIBED, (message, response) => {
// This sample bot can answer only text messages, let's make sure the user is aware of that.
if (!(message instanceof TextMessage)) {
say(response, `Sorry. I can only understand text messages.`);
}
});
bot.onTextMessage(/./, (message, response) => {
checkUrlAvailability(response, message.text);
});
const http = require('http');
const port = process.env.PORT || 8080;
http.createServer(bot.middleware()).listen(port, () => bot.setWebhook('https://yourwebhookurl.com'));