Triggering an Api.ai event through a external server - javascript

It is needed to send an alert to the chat after a timer has started.
Scenario:
Remind me to call Bob in 5 minutes
OK, will remind you in 5 minutes
After this dialog fulfillment server will start a timer and when the time goes off, event is to be triggered.
But when using the event api in API.ai will not trigger a message to the chat window which was built using the JS api.
Is there a another way to achieve this.

This is very much possible. In the intents that you setup on API.ai, use #sys.time so that 'call me in 5 minutes' will return a time like 14:05:00. You can use this parameter to start a timer on your JS script and send the message when timer expires.

Related

Queue with delay or alternative

I have a chatbot application that needs to finish its session X minutes after the latest message from user. I run a job each X minutes which I verify if the last message has been sent X minutes ago. Do you guys have some suggestion for that problem without a job use? I think something as a queue with a X minutes delay or something like that. What is a better way to do that?
If your chatbot is running on clients with javascript enabled, you can set and clear and reset a window timer on each key stroke...basically if the time goes off after x minutes, that means no key has been pressed, at that time you can do whatever is needed to close the application.
Read more about window.setTimout here: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout

djs unmuting user in time (database)

I want to make a mute command where you can mute a user for a specific time.
I have used a setTimeout() function before but it isnt useful for a bigger time or if the bot stops and start again. So now I am saving the time to a database:
expires = new Date(new Date().getTime() + time).toLocaleString()
// -> 9.9.2021, 22:21:34
So now I want to unmute this user on 9.9.2021, 22:21:34. The problem is that I dont know how to give the bot a signal that the user has to be unmuted in time. So how to tell the bot when to call the function unmute(). For example if I get the data from the database when the bot is started and the data is: 9.9.2021, 22:21:34. Now the bot has to call a function at this date. How?
One common approach is to use the database as a message-queue. The bot would "poll" the database at regular intervals to pull relevant events and execute those that are ready. When an event is pulled, it is also deleted from the database.

Execute API after close the browser in Angular

i need to execute a Api if the browser closed, example break out,
closing the browser from the user, crash of pc, ext ext.
This function is in my component, i dont call this function anywhere,I think it should automatically activate.
#HostListener('window:beforeunload', ['$event'])
beforeunloadHandler(event) {
this.postService.unsetPost(this.postId).subscribe();
}
But dont work.
In my opinion the better idea is making the heartbeat api that sends requests every N seconds to notify server that the session is active and the user is online. On the server check every M minutes if there was no heartbeat requests for more than N seconds: if it is so - execute the API request(what you wanted to execute on crash).

How to call function in certain time interval

I need to run a certain API function, in a certain time period once the access token is granted, and check the status and inform the user if there any changes.
So far i did it by clicking a button on popup window of the firefox addon and calling the functions background js files, so i can receive the info from the API and notify the user. But i need to automate this process in background js files.
Are there any firefox inbuild JS API or any other way to achieve this auto API request calling from addon background files?
Yes, there is, the setInterval() function.
let inter = setInterval(() => {
console.log('HI!');
}, 1000);
To stop the timer, just call: clearInterval(inter);
More info about setInterval() here.

jquery/javascript setInterval

Currently I'm developing a user notification alert message function.
I managed to use setInterval to control my Ajax call (to check if there's any notification msg for the user). But my problem is that I only wanted the notification message only
appear once on the page (Now it displays multiple notification alert msg on the screen). I know that you can use setTimeout to make it only call once but I also needed the page to check if there's a new notification message alert in every 5 min.
Second question is it possible the first round calling the Ajax call instantly and then all other calls every 5 min? Because I wanted the system to check instantly once they logged into the system n then afterward every 5 min.
Here is my code
function getAjaxNotice() {
$.post("/async/getnotification", {},
function(response) {
var notice = $(response);
$("#notices").prepend(notice);
});
return false;
}
setInterval("getAjaxNotice()", 50000);
First of all, you should wrap your initialization code in an onLoad function:
$(document).ready(function() {
// Put all your code here
});
Making it appear once is easy, use .html() instead to set the content rather than add to it:
$("#notices").html(notice);
Third, as a style note, you should not pass a string to setInterval(). Rather, pass a function name:
setInterval( getAjaxNotice, 50000 );
Finally, to make it call the function now, and again after every 5 minutes, use:
// Set the timer
setInterval( getAjaxNotice, 50000 );
// Call it once now
getAjaxNotice();
Also note that 50000 is 50 seconds, not 5 minutes. 5 minutes would be 5 * 60 * 1000 = 300000.
For the first problem, you should be storing the return value from setInterval, and then calling clearInterval(myIntervalId) when you receive an alert.
For the second problem, you can call getAjaxNotice once during onload of the body, and then if no alerts are received, call setInterval at that point.
setInterval's time is in milliseconds.
5 minutes * 60 seconds * 1000 milliseconds = 300000ms
Also, I suggest you pass a function to setInterval not a string, so you can avoid the implicit use of eval.
setInterval(getAjaxNotice, 300000);
To call getAjaxNotice at the start of the page, put it in a ready block.
$(function(){
getAjaxNotice();
});
A couple of things...
setInterval("getAjaxNotice()", 50000);
Is not 5 minutes.
5 minutes = 300000 milliseconds.
and if you want it to run instantly and THEN do it every 5 minutes you can simply do
$(document).ready(function() {
getAjaxNotice();
function getAjaxNotice() {
$.post("/async/getnotification" ,
{},
function(response)
{
var notice = $(response);
$("#notices").prepend(notice);
});
return false;
}
setInterval( getAjaxNotice(), 300000 );
});
In your situation it sounds like you are dealing with a few problems. So using your current approach, you can initially make your ajax call and follow it up with a set timeout:
getAjaxNotice();
setTimeout( "getAjaxNotice()", 300000);
Secondly, ensuring the user received the message only once can be done easily if you have some type of "message confirmed" event. Assume your user could have browsers open on multiple computers, if you make the user click the message or click an ok button, or perform some action to acknowledge they received the message, you can fire off another ajax call to delete that message from the buffer on your server, yet still display it on all open browsers. The local browser would only display it once because you could prevent displaying it client side if the message is a duplicate (based on what ever criteria makes sense for your application)
However, you should look into long polling and COMET, http://en.wikipedia.org/wiki/Comet_(programming). Comet is a concept around pushing notifications to web browsers based on server side events, as opposed to web browsers constantly asking the server for changes.
Due to limitations in web frameworks and browsers, this was accomplished with a few technologies, but long-polling seems to be the most prevalent. HTML5 and websockets are trying to make some changes that could prevent polling all together, but its not readily available yet.
Long Polling, http://en.wikipedia.org/wiki/Push_technology, and COMET based architecture have been used by companies like meebo and facebook. Don't quote me on this but for some reason I'm inclined to believe facebook uses an Erlang based webserver to serve their chat messages. Erlang and NodeJs are just a couple of solutions you can use to build light weight web servers that work well with tons of long polling requests hitting your servers.
You should definitely go read up on all these things yourself as there is a wealth of information available. I have experimented with create a NodeJs server on Amazon EC2, as I'm traditionally a .NET job and don't feel IIS is the right solution for supporting an the long polling features of a .net application which uses long polling, and I have to say I like NodeJs alot. Plus the javascript language is much more familiar to me than my limited knowledge of Erlang.

Categories