When is a conversation started on Facebook? - NodeJS SDK - node.js

When a facebook messenger user start a conversation I want to get the data like name, photo, etc ... and send to my API.
But I'm doubtful when I should do this.
It would be in the:
bot.on('conversationUpdate', (session) => {
// On here??
})

Facebook doesn't support the conversationUpdate event. It won't send events when you start a conversation, however you could solve your issue in two different ways.
Create a custom middleware that checks if you already have the userData from Facebook and retrieve it when you don't have it yet.
Create a custom middleware that translates Facebook's callbacks to intents
I already created those for NodeJS, which you can use in your bot. BotBuilder-FacebookExtensions
If you need more guidance, you can read the blogs I wrote about this:
How to retrieve User Data from Facebook /
How to process Facebook Messenger callbacks

Related

Handle Dialogflow requests from multiple users through ngrok

I'm working on a website for school, using Dialogflow, ReactJs and NodeJs. I am pretty new to doing this and I am not certain whether my implementation will work for my use case.
My website involves a game, which the server-side needs game state information from. I am sending requests from React to NodeJs as follows:
axios({
method: 'post',
url: '/game_state',
data: {'current_player': current_player}
// ... (more data is sent in reality)
}
);
My post requests are then handled by NodeJs as follows:
app.post('/game_state',express.json(),(event)=>{
current_player = event.body.current_player;
// ... (more data)
});
This data can then be accessed by a Dialogflow request. For example, if the user asks: "Who's turn is it?", an intent with webhook fulfillment can then be triggered. The code for that:
function turn(agent){
current_player == 1 ? agent.add("Your turn") : agent.add("My turn");
}
As I said before, I am not very experienced with developing websites. I want to be able to use my implementation with multiple users at the same time, so one user's game states shouldn't influence those of another user.
So far, I've been using (the free version of) ngrok to test my website. My implementation works perfectly fine if only one user is accessing the link. However, when I open the website twice on the same computer (using the ngrok-generated link) and interact with e.g. the turn intent, I notice that the turn response will be the same for both even if for one session, it's the player's turn and for the other, it's the chatbot's turn.
Most likely, the post request is being overwritten given that the same link is used. I would like to find a solution that makes it possible to use the same link but have the server keep the game state information separate for each user. I feel like this might be possible with a user ID, but I'm stumped on how to implement that.
I know something like this may be an option: app.get("/game_state/:id", ...), but I am not sure how to also get this information to be available for the Dialogflow part of the implementation. Dialogflow calls the server itself, so it doesn't pass this user ID with it. Somehow, I'd need to distinguish it there as well.
Any suggestions would be greatly appreciated!
When you make Dialogflow detected intent API call at that time you are creating a session path
const sessionPath = await sessionClient.projectAgentSessionPath(PROJECTID, sessionId);
If you pass session Id = 123456789A so for this Id Dialogflow managed all context and parameter on Id 123456789A
session Id = USER
when you get the webhook call to the server you also get this session Id or we also called UserId like
let userId = agent.session.split('/')[4]
you can find more information here
dialogflow session

Best way to fetch emails from an email account and perform an async function with NodeJS

What would be the best non-block way to fetch emails from my account (Im using GMAIL, can use any, the service doesn't matter) and depending on the subject/body it runs an AXIO/CHEERIO or Puppeteer scraper, so async would be the best.
If you are asking for a 'way' to do it, I'll assume that you are asking how can you do it.
You can use the Axios package.
It's promised-base.
A basic example:
axios.get('/')
.then(data=> //do something)
.catch(err => //handle it)
But you'll have to read into the gmail api for details if you really want to interact with gmail that way.
I can only answer till here as the question does not provide any more specific information.

Intercept user intent with `routing` middleware

I'm trying to intercept messages to send to my analytics server.
I'm looking to send it some data such as: messages,intent,resulted dialog.
I tried to use the routing middleware of universalBot in this fashion:
bot.on('routing',(session)=>{
console.log(session.intent);
console.log(session);
})
But it seems like the intent is undefined, and the only way I manage to see it is through triggerAction => onFindAction function.
I can use this method but it seems like a hack, is there a good way to intercept the intent data with middleware?
I'm using the Node.js SDK with botbuilder-apiai

Getting Agent Info from API.ai fulfilment request

Is there a way that I can get the API.ai agent information from the fulfillment request?
I am trying to capture any unique parameter from Node.js code that can be passed from Agent. This will be utilized for proceeding with the logic for multiple agents using a single code base.
For Amazon Alexa I could get the Skill Application Id from the session. Is there something similar in API.ai?
If you want to get agent id you can make something like this (using express framework):
app.post(`/:agent_id/webhook`, (req, res) => {
const apiai_agent_id = req.params.agent_id;
// webhook code...
});
And on fulfillment page in this case for each agent you have to specify it's own webhook URL like
https://mywebhooks.com/55982e7c-db17-47ee-92bb-176476228942/webhook
(you can get agent id for webhook URL from browser's address bar)
You can use the API.AI's sessionId or intentId which will be unique across agents. The sessionId and intentId* is sent with every fulfillment webhook request. The of the JSON sent are similar to this:
{
"id": "1a4b6209-51ec-47a1-a797-2e2f71926ac8",
"sessionId": "1503343146047"
...
}
but will contain other elements as well.
*intentId may include additional numeric identifier after the intent ID if you are using slot filling
Source: https://api.ai/docs/fulfillment#request

facebook node js logout

using module https://github.com/amachang/facebook-node-sdk
i don't find a method to diconnect from facebook and clear data.
i tried to use Facebook.prototype.clearAllPersistentData() but it doesn't work
In other words when i diconnect from facebook and i log in with facebook in my app
the app don't ask me to connect with facebook another time
the problem i still get with the method loginRequired() data.
is there a method for logout ?
If you use a LoginButton, you can try use: (facebook-android-sdk:4.5.0)
fbLogin.setLoginBehavior(LoginBehavior.WEB_ONLY);
insted
fbLogin.setLoginBehavior(LoginBehavior.NATIVE_WITH_FALLBACK); // default
when configuring LoginButton object.

Resources