rasa is up and running. working just fine on command line but not in Jabber. It is recognizing the events bye , hi , greet etc.
**but not recognizing joke.**
**Also how to respond utter_messages in response to jabber bot.**
bot.js
const Botkit = require('../lib/Botkit.js');
const xml = require('@xmpp/xml');
var rasa = require('../lib/middleware/middleware.js')({rasa_uri: 'http://localhost:5000', rasa_project:'default'});
var controller = Botkit.jabberbot({
json_file_store: './jabberbot/'
});
controller.middleware.receive.use(rasa.receive);
var bot = controller.spawn({
client: {
jid: 'username',
password: 'password',
host: "host",
port: 5222
}
});
function ExtractMentionJids(message) {
let direct_mention_reg = /href="xmpp:\s?(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+)\s?"/ig;
let email_reg = /\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+/i;
let match = message.stanza.toString().match(direct_mention_reg);
let mention_jids = [];
if (match) {
for (let i = 0; i < match.length; i++) {
let jid_match = match[i].match(email_reg);
if (jid_match) {
let jid = jid_match[0];
if (jid != bot.client_jid) {
mention_jids.push(jid);
}
}
}
}
return mention_jids;
}
function toUTCDateTimeString(date) {
var yyyy = date.getUTCFullYear();
var mm = date.getUTCMonth() < 9 ? "0" + (date.getUTCMonth() + 1) : (date.getUTCMonth() + 1); // getMonth() is zero-based
var dd = date.getUTCDate() < 10 ? "0" + date.getUTCDate() : date.getUTCDate();
var hh = date.getUTCHours() < 10 ? "0" + date.getUTCHours() : date.getUTCHours();
var min = date.getUTCMinutes() < 10 ? "0" + date.getUTCMinutes() : date.getUTCMinutes();
var ss = date.getUTCSeconds() < 10 ? "0" + date.getUTCSeconds() : date.getUTCSeconds();
return "".concat(yyyy).concat('-').concat(mm).concat('-').concat(dd).concat('T').concat(hh).concat(':').concat(min).concat(':').concat(ss);
};
controller.hears(['greet'],'direct_message, message_received, direct_mention,mention', rasa.hears, function(bot, message) {
//bot.reply(message, rasa.send);
//console.log("This is a greet message.");
console.log(message);
bot.reply(message);
});
controller.hears(['joke'],'direct_message, message_received, direct_mention,mention', rasa.hears, function(bot, message) {
//bot.reply(message, rasa.send);
console.log("This is a joke.");
bot.reply(message, message.response);
});
controller.hears(['goodbye'],'direct_message, message_received, direct_mention,mention', rasa.hears, function(bot, message) {
//bot.reply(message, rasa.send);
console.log("This is a goodbye message.");
bot.reply(message, message.response);
});
JabberBot.js
var Botkit = require(__dirname + '/CoreBot.js');
const Stanza = require('node-xmpp-client').Stanza;
const GroupManager = require('./JabberGroupManager.js');
var rasa = require('./middleware/middleware.js')({rasa_uri: 'http://localhost:5000', rasa_project:'default'});
function JabberBot(configuration) {
// Create a core botkit bot
var controller = Botkit(configuration || {});
function toUTCDateTimeString(date) {
var yyyy = date.getUTCFullYear();
var mm = date.getUTCMonth() < 9 ? '0' + (date.getUTCMonth() + 1) : (date.getUTCMonth() + 1); // getMonth() is zero-based
var dd = date.getUTCDate() < 10 ? '0' + date.getUTCDate() : date.getUTCDate();
var hh = date.getUTCHours() < 10 ? '0' + date.getUTCHours() : date.getUTCHours();
var min = date.getUTCMinutes() < 10 ? '0' + date.getUTCMinutes() : date.getUTCMinutes();
var ss = date.getUTCSeconds() < 10 ? '0' + date.getUTCSeconds() : date.getUTCSeconds();
return ''.concat(yyyy).concat('-').concat(mm).concat('-').concat(dd).concat('T').concat(hh).concat(':').concat(min).concat(':').concat(ss);
}
controller.middleware.receive.use(rasa.receive);
controller.middleware.send.use(rasa.send);
controller.changeEars(function (patterns, message) {
return rasa.hears(patterns, message);
});
controller.middleware.format.use(function(bot, message, platform_message, next) {
// clone the incoming message
for (var k in message) {
platform_message[k] = message[k];
}
next();
});
/*
* customize the bot definition, which will be used when new connections
* spawn!
*/
controller.defineBot(function(botkit, config) {
var xmpp = require('simple-xmpp');
var bot = {
type: 'xmpp',
botkit: botkit,
config: config || {},
client_jid: config.client.jid,
utterances: botkit.utterances,
};
GroupManager(config, xmpp, bot, controller);
function request_roster() {
let roster_stanza = new Stanza('iq', { 'from': config.client, 'type': 'get'});
roster_stanza.c('query', { xmlns: 'jabber:iq:roster'});
xmpp.conn.send(roster_stanza);
}
xmpp.on('online', function(data) {
console.log(toUTCDateTimeString(new Date()) + ':Connected with JID: ' + data.jid.user);
console.log('Yes, I\'m connected!');
request_roster();
/*
* send whitespace to keep the connection alive
* and prevent timeouts
*/
setInterval(function() {
xmpp.conn.send(' ');
}, 1800000);
});
xmpp.on('close', function() {
console.log(toUTCDateTimeString(new Date()) + ':connection has been closed!');
process.exit();
});
xmpp.on('error', function(err) {
console.log(toUTCDateTimeString(new Date()) + ':' + err);
process.exit();
});
xmpp.on('subscribe', function(from) {
xmpp.acceptSubscription(from);
console.log(toUTCDateTimeString(new Date()) + ':accept subscribe from:' + from);
controller.trigger('subscribe', [bot, from]);
});
xmpp.on('unsubscribe', function(from) {
console.log(toUTCDateTimeString(new Date()) + ':accept unsubscribe from:' + from);
xmpp.acceptUnsubscription(from);
});
function findBotJid(jid) {
return jid === bot.client_jid;
}
function matchBotJid(jid_left, jid_right) {
return jid_left.toLowerCase() === jid_right.toLowerCase();
}
function IsBotMentioned(message) {
let mention_jids = extractMentionJids(message);
if (mention_jids.find(findBotJid)) {
return true;
}
return false;
}
function extractMentionJids(message) {
let direct_mention_reg = /href="xmpp:\s?(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+)\s?"/ig;
let email_reg = /\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+/i;
let match = message.stanza.toString().match(direct_mention_reg);
let mention_jids = [];
if (match) {
for (let i = 0; i < match.length; i++) {
let jid_match = match[i].match(email_reg);
if (jid_match) {
let jid = jid_match[0];
mention_jids.push(jid);
}
}
}
return mention_jids;
}
controller.on('message_received', function(bot, message) {
if (message.group == false) {
if (matchBotJid(message.user, bot.client_jid)) {
controller.trigger('self_message', [bot, message]);
return false;
} else {
controller.trigger('direct_message', [bot, message]);
return false;
}
} else {
if (IsBotMentioned(message)) {
if (matchBotJid(bot.client_jid, message.from_jid)) {
controller.trigger('self_message', [bot, message]);
} else {
controller.trigger('direct_mention', [bot, message]);
}
return false;
}
}
});
xmpp.on('stanza', function(stanza) {
if (stanza.is('message')) {
if (stanza.attrs.type == 'chat') {
var body = stanza.getChild('body');
if (body) {
var message = body.getText();
var from = stanza.attrs.from;
var xmpp_message = {};
xmpp_message.user = from;
xmpp_message.text = message;
xmpp_message.group = false;
xmpp_message.stanza = stanza;
xmpp_message.channel = 'chat',
controller.ingest(bot, xmpp_message, null);
}
} else if (stanza.attrs.type == 'groupchat') {
var body = stanza.getChild('body');
if (body) {
let message = body.getText();
let from = stanza.attrs.from;
let from_split = from.split('/');
let conference = from_split[0];
let from_jid = null;
if (from_split.length > 1)
from_jid = from_split[1];
if (!from_jid)
return;
let history_reg = /xmlns="http:\/\/www.jabber.com\/protocol\/muc#history"/i;
if (history_reg.test(stanza.toString()))
return false;
var xmpp_message = {};
xmpp_message.user = conference;
xmpp_message.text = message;
xmpp_message.group = true;
xmpp_message.channel = 'groupchat';
xmpp_message.from_jid = from_jid;
xmpp_message.stanza = stanza;
controller.ingest(bot, xmpp_message, null);
}
}
}
});
bot.startConversation = function(message, cb) {
botkit.startConversation(this, message, cb);
};
bot.createConversation = function(message, cb) {
botkit.createConversation(this, message, cb);
};
bot.send = function(message, cb) {
if (message.stanza) {
xmpp.conn.send(message.stanza);
} else {
xmpp.send(message.user, message.text, message.group);
}
if (cb) {
cb();
}
};
bot.reply = function(src, resp, cb) {
var msg = {};
if (typeof (resp) == 'string') {
msg.text = resp;
} else {
msg = resp;
}
msg.user = src.user;
msg.channel = src.channel;
msg.group = src.group;
bot.say(msg, cb);
};
bot.findConversation = function(message, cb) {
botkit.debug('CUSTOM FIND CONVO', message.user, message.channel);
for (var t = 0; t < botkit.tasks.length; t++) {
for (var c = 0; c < botkit.tasks[t].convos.length; c++) {
if (
botkit.tasks[t].convos[c].isActive() &&
botkit.tasks[t].convos[c].source_message.user == message.user &&
botkit.tasks[t].convos[c].source_message.channel == message.channel
) {
botkit.debug('FOUND EXISTING CONVO!');
cb(botkit.tasks[t].convos[c]);
return;
}
}
}
cb();
};
xmpp.connect(config.client);
return bot;
});
controller.startTicking();
return controller;
}
module.exports = JabberBot;
domain.yml
intents:
- greet
- goodbye
- thanks
- deny
- joke
- name
entities:
- name
slots:
name:
type: text
actions:
- utter_name
- utter_thanks
- utter_greet
- utter_goodbye
- action_joke
templates:
utter_name:
- text: "Hey there! Tell me your name."
utter_greet:
- text: "Nice to you meet you {name}. How can I help?"
utter_goodbye:
- text: "Talk to you later!"
utter_thanks:
- text: "My pleasure."
nlu_data.md
<!--- Make sure to update this training data file with more training examples from http://forum.rasa.com/t/rasa-starter-pack/704 -->
## intent:goodbye <!--- The label of the intent -->
- Bye <!--- Training examples for intent 'bye'-->
- Goodbye
- See you later
- Bye bot
- Goodbye friend
- bye
- bye for now
- catch you later
- gotta go
- See you
- goodnight
- have a nice day
- i'm off
- see you later alligator
- we'll speak soon
## intent:greet
- Hi
- Hey
- Hi bot
- Hey bot
- Hello
- Good morning
- hi again
- hi folks
- hi Mister
- hi pal!
- hi there
- greetings
- hello everybody
- hello is anybody there
- hello robot
## intent:thanks
- Thanks
- Thank you
- Thank you so much
- Thanks bot
- Thanks for that
- cheers
- cheers bro
- ok thanks!
- perfect thank you
- thanks a bunch for everything
- thanks for the help
- thanks a lot
- amazing, thanks
- cool, thanks
- cool thank you
## intent:affirm
- yes
- yes sure
- absolutely
- for sure
- yes yes yes
- definitely
## intent:name
- My name is [Juste](name) <!--- Square brackets contain the value of entity while the text in parentheses is a a label of the entity -->
- I am [Josh](name)
- I'm [Lucy](name)
- People call me [Greg](name)
- It's [David](name)
- Usually people call me [Amy](name)
- My name is [John](name)
- You can call me [Sam](name)
- Please call me [Linda](name)
- Name name is [Tom](name)
- I am [Richard](name)
- I'm [Tracy](name)
- Call me [Sally](name)
- I am [Philipp](name)
- I am [Charlie](name)
- I am [Charlie](name)
- I am [Ben](name)
- Call me [Susan](name)
- [Lucy](name)
- [Peter](name)
- [Mark](name)
- [Joseph](name)
- [Tan](name)
- [Pete](name)
- [Elon](name)
- [Penny](name)
- name is [Andrew](name)
- I [Lora](name)
- [Stan](name) is my name
- [Susan](name) is the name
- [Ross](name) is my first name
- [Bing](name) is my last name
- Few call me as [Angelina](name)
- Some call me [Julia](name)
- Everyone calls me [Laura](name)
- I am [Ganesh](name)
- My name is [Mike](name)
- just call me [Monika](name)
- Few call [Dan](name)
- You can always call me [Suraj](name)
- Some will call me [Andrew](name)
- My name is [Ajay](name)
- I call [Ding](name)
- I'm [Partia](name)
- Please call me [Leo](name)
- name is [Pari](name)
- name [Sanjay](name)
## intent:joke
- Can you tell me a joke?
- I would like to hear a joke
- Tell me a joke
- A joke please
- Tell me a joke please
- I would like to hear a joke
- I would loke to hear a joke, please
- Can you tell jokes?
- Please tell me a joke
- I need to hear a joke
- joke
stories.md
## story_greet <!--- The name of the story. It is not mandatory, but useful for debugging. -->
* greet <!--- User input expressed as intent. In this case it represents users message 'Hello'. -->
- utter_name <!--- The response of the chatbot expressed as an action. In this case it represents chatbot's response 'Hello, how can I help?' -->
## story_goodbye
* goodbye
- utter_goodbye
## story_thanks
* thanks
- utter_thanks
## story_name
* name{"name":"Sam"}
- utter_greet
## story_joke_01
* joke
- action_joke
## story_joke_02
* greet
- utter_name
* name{"name":"Lucy"} <!--- User response with an entity. In this case it represents user message 'My name is Lucy.' -->
- utter_greet
* joke
- action_joke
* thanks
- utter_thanks
* goodbye
- utter_goodbye
endpoints.yml
action_endpoint:
url: "http://localhost:5055/webhook/"
rasa is up and running. working just fine on command line but not in Jabber. It is recognizing the events bye , hi , greet etc.
**but not** recognizing joke.
Also how to respond utter_messages in response to jabber bot.