Bot Framework Node.js ad hoc message TO A SPECIFIC USER

走远了吗. 提交于 2019-12-23 01:11:57

问题


I have been staring at this for hours and can't find a solution and that is even though by all suggestions it SHOULD be quite easy - https://docs.microsoft.com/en-us/bot-framework/nodejs/bot-builder-nodejs-proactive-messages.

I have created a simple code which will "register" the user and save their data in my cosmosDatabse on Azure. That works perfectly.

//ON "register" SAVE USER DATA AND SAY REGISTERED MESSAGE 
bot.dialog('adhocDialog', function(session, args) {

    var savedAddress = session.message.address;
    session.userData.savedAddress = savedAddress;
    //REGISTERED MESSAGE
    session.endDialog("*Congratulations! You are now registered in our network! (goldmedal)*");
})
.triggerAction({
    matches: /^register$/i
})
  • But how can I then access that specific user and send him a message if, say, a condition is met? (in fact on HTTP request)

I am fairly certain we have to write the conversation ID or user ID somewhere. The question is where?

function startProactiveDialog(address) {
    bot.beginDialog(address, "A notification!");
}
  • This is how simple I think it should be. But where do you specify the user then?

回答1:


You've saved the address of the user inside of your database by saving it to session.userData.savedAddress. When the event triggers, perform a query to your database that checks for the users that meet two criteria.

  1. They're registered to listen for the event
  2. Their address has been saved inside of the database.

In your case, you can save a property to the session.userData object, a property that lists which events they're listening for. If you just need to send a message to the user, then you can simply use bot.loadSession(savedAddress) to ping the user.


Edit:

So instead of looking specifically by user ID, you should send a query to your CosmosDB that looks for entries that have a "listen-to" Boolean-type flag corresponding to the event.

You're not worrying about the user ID at first, you're just retrieving all entries with a query that would (broadly speaking) look like this: SELECT * FROM BotState WHERE data LIKE 'listenForEvent=1.

So to setup your session.userData so that the above theoretical query would work, you would need to modify that snippet of code in your question to something like the following:

bot.dialog('adhocDialog', function(session, args) {
    var savedAddress = session.message.address;
    session.userData.savedAddress = savedAddress;
    session.userData.listenForEvent = 1 // Our property we're going to look for.

    session.endDialog("*Congratulations! You are now registered in our network! (goldmedal)*");
})
.triggerAction({
    matches: /^register$/i
})



回答2:


Actually, the savedAddress should be an instance of IAddress, and also, the function loadSession(address: IAddress, callback: (err: Error, session: Session) => void): void; and address(adr: IAddress): Message; under Message class all require IAddress as the parameter.

So first of all, you should save the entire address json object in cosmosDB for later using.

As botbuilder for Node.js is built on Restify or Express, you can build an addition route for your user to trigger and send proactive messages. The work flow could be following:

  1. Guide user to register & Save the user's address object with the account mapping in your DB
  2. Create a Route in Restify or Expressjs for trigger the proactive message:

    server.get('/api/CustomWebApi', (req, res, next) => {
       //find the user's address in your DB as `savedAddress`
        var msg = new builder.Message().address(savedAddress);
        msg.text('Hello, this is a notification');
        bot.send(msg);
        res.send('triggered');
        next();
      }
    );
    

    or if you want to leverage loadSession

    server.get('/api/CustomWebApi', function (req, res, next) {
        bot.loadSession(savedAddress, (err, session) => {
            if (!err) {
                session.send('Hello, this is a notification')
                session.endConversation();
            }
        })
        res.send('triggered');
        next();
    });
    



回答3:


I created a users.json file, to which I save all the users. It works the way I need it to. I guess database would be better, but I don't really have a clue where to begin with that. Database is a whole new chapter I have not encountered yet, so it doesn't make sense to work on it when the project needs are resolved.



来源:https://stackoverflow.com/questions/49176318/bot-framework-node-js-ad-hoc-message-to-a-specific-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!