how to move to other section in script - bots

I'm building a bot using gupshup.io using the scripting way ... but handling some things in default.js file as mentioned in the docs
I'm trying in a handler function to check if the event.message equals specific string to go to another section in the script
can anybody please help ?
thanks a lot

So to achieve that you can create a child state to go another section and just set options.next_state to that state. I mean suppose you have a script like this
[main]
inputParser:Welcome to New Bot.
thisFlow:
This is a output of this flow.
callAnotherFlow:
:call default.anotherFlow
[anotherFlow]
This is another flow.[[Wow, No]]
Wow
Thanks
No
Oh!
So in case if the message is 'another flow' you want the second flow to begin. So in the input parser you can create something like.
module.exports.main = {
inputParser: (options, event, context, callback)=>{
if(event.message.toLowerCase() === "another flow"){
options.next_state = 'callAnotherFlow';
}else{
options.next_state = 'thisFlow';
}
callback(options, event, context);
}
}
I think this is what you are looking for.

Related

Azure logic app use a variable inside inline_code action

I have a logic app that looks something like this:
In the js code action, I just wanted to check if the name of the file (that had triggered the workflow) match some pattern. So my Inline code action has something like:
var input = workflowContext.trigger.outputs.headers.x-ms-file-name;
if(input.match(/^([0-9]){3}_Hello/))
return true;
else
return false;
However, it seems that the action can't get the file name because the x-ms-file-name is splitted by a '-'.
So, I tried to solve this problem by creating a variable called fileName in an the action before it
and then use it in the inline code action. But I don't know how to call the variable inside inline code action.
What should I write here:
var input = ????;
if(input.match(/^([0-9]){3}_Hello/))
return true;
else
return false;
Any suggestion to how to solve this problem?
Note (I am not sure if it is relevant but in case it helps): I am using the standard logic app so I amp supposed to not have/use Integration account
It seems that I can just simply write the following in the inline action:
var input = workflowContext.trigger.outputs.headers['x-ms-file-name'];
if(input.match(/^([0-9]){3}_Hello/))
return true;
else
return false;

Ending conversation

My bot (using MS BotFramework) is supposed to be hearing the conversation stream. If someone mentions 'chatbot' it should say 'Here I am!', otherwise stays quiet. It seems to be very simple and maybe it is but I am having a hard time trying to implementing it. Here is what I have:
bot.add('/', function(session) {
if (someoneSaidChatbot) {
session('Here I am!")
} else {
// session.reset(), maybe? No!
// session.endDialog() then? Uh...nope.
// nothing? Hmmm. negative
}
});
So, nothing works. If I leave there the bot just hangs and it stops listening to the stream or answering commands.
Any thoughts?
This code ends a dialog when someone types "chatbot" as part of the utterance. Is this what you are looking for?
bot.add('/', function (session) {
if (session.message.text.search("chatbot") >= 0) {
session.endDialog("Here I am");
}
});
I'd like to suggest use endConversationAction() to register a Bots Global Actions
bot.endConversationAction(
'enddialog', //dialog Id
'Here I am', //message
{ matches: /^.*chatbot/i } //match pattern
);
since this is global action, anytime when bot heard "Chatbot", it will say "Here I am" , if there are some dialog in stack, your proposed solution might not work.
It may also depend on which channel you're using. Some channels don't offer the ability for the Bot to listen to all of the messages in the conversation.

Server state and users in a Meteor app, how to create state?

I have a Meteor application where I use RiveScript, a chatbot module for Node. The module can save some aspects of user input. My issue is that when I run the module on server, the state is not saved for one user, but for all users. How would I go about creating a state for each user?
One method would be to create a new bot for each user like so:
let RiveScript = require('rivescript');
let users = {};
Meteor.onConnection(connection => {
users[connection.id] = new RiveScript({utf8: true});
users[connection.id].bot.loadDirectory('directory/',
() => {
users[connection.id].bot.sortReplies();
}
);
connection.onClose(() => {
delete users[connection.id]
})
});
However, in terms of memory management this can cause an issue. Are there any commonly used patterns in this regard?
I am not familiar with Meteor nor RiveScript, so I hope I'm answering your question.
If you want to keep a certain state for each conversation, e.g.
Chat #1 : the bot is waiting for the user to answer a particular question
Chat #2 : the bot is waiting for a command
...
Why don't you use a simple array mapping a conversation identifier (or in your case connection.id, I'm guessing) to its current state?
Example:
// Just to simulate an enumeration
var States = {WAITING: 0, WAITING_XXX_CONFIRMATION: 1, ...}
var state = [];
Meteor.onConnection(connection => {
state[connection.id] = States.WAITING;
});
// Somewhere else, e.g. in response to a command
if (state[connection.id] == WAITING_XXX_CONFIRMATION && input === 'yes') {
// do something
// reset the state
set[connection.id] = WAITING;
}
You could then keep track of the state of every conversation, and act accordingly. You could also wrap the state management inside an object to make it more reusable and nice to use (e.g. StateManager, to be able to make calls such as StateManager.of(chatId).set(newState).
Hope this helps.

NodeJS: Run module method within sandbox

I need one simple thing:
var Base = function(module){
this.outsideMethod = function(arg1)
{
// run method in new context - sandbox
return vm.runInNewContext(module.insideMethod, arg1);
}
}
is something like this possible in nodejs? thx very much
If the insideMethod function does not call or use functions/vlues from outside the context it shall run in, yes.
You can convert any function in Javascript to a string.
Doing vm.runInNewContext('('+module.insideMethod+')('+JSON.stringify(arg1)+"); could be what you want.

RequireJS and Globals

I have a number of event handlers in my page that were accessing global functions (functions defined in Script tags on the page). For instance:
<button id="ClearText" onclick="cleartb()">Clear Text Box</button>
That cleartb() function simply sits on the page:
<script>
function cleartb()
{
vm.essayText('');
return;
}
</script>
Now, vm is my page's view model (but for this question, all that matters is that it was simply a global variable available to the entire page) and I use functions and values it exposes in several event handlers, alert messages, etc.
The problem is that I've moved the definition of vm into a RequireJS AMD module called vm.js:
define(["knockout", "jquery"], function (ko, $) {
var essayText = 'Hello World!';
...
return {
essayText: essayText
}
});
When my onlick event handler runs or I refer to vm in any manner, I get a "vm undefined" error (as expected).
Question 1:
How can I give my page access to the vm variable defined in an AMD module especially if I don't want to "pollute" the global namespace? Is there a best-practice here?
Question 2:
Ultimately, I don't even want cleartb() on the page because it really is a view-model-specific operation. Although I think I can figure out what to do once I have the (an?) answer to Question 1, I would be interested to know how best to move the cleartb function into the vm AMD module so that I still can call it from my onlick event handler.
Note that I want values and function still to be exposed from a vm variable so that I can continue to use vm.cleartb() or inspect the value of vm.essayText() (it's a KO observable). (In other words, I don't want to solve the problem with a cleartb(vm) solution.)
Thank you for any help!
<script>
function cleartb()
{
vm.essayText('');
return;
}
alert(window.cleartb);
</script>
Actually, this way is already pollute the global window variable. So I think your first requirement don't make sense. And then you can do this way:
define(["knockout", "jquery"], function (ko, $) {
var essayText = 'Hello World!', varToBeExported;
...
window.varToBeExported = {
'cleartb': cleartb
};
return {
essayText: essayText
}
});
But if unnecessary, you should using requireJs way - require(['your moudle'],.... .

Resources