MS Bot Framework pass the values from server code (C# ) to front end code (JavaScript)

元气小坏坏 提交于 2020-03-25 16:02:13

问题


I've been using MS bot framework - Chat bot in my project and I'm using QnAMaker in the back end for my chat bot, which is running in direct line bot channel.

On page load using JavaScript front end code, i'm sending some values say ABC to the bot (c# code - server hosted) via createStore method, after asking the question the bot will start querying it in the qnamaker using ABC as strict filters.

After sometime, In one some scenario, i will give input to BOT and change the values as XYZ. up to this everything is fine.

Now, I want to get the values XYZ and update that in my front end JavaScript Code.

In other words, i want to reverse the createstore method. or creating a communication from server side to client side

Is there any way to achieve my requirement.


回答1:


You could leverage the same store in order to receive any activity from the bot (previously named the backchannel approach). For example, you could send an activity from your bot of the event type combined with a name and a value.

Bot (JavaScript)

await context.sendActivity({
    type: ActivityTypes.Event,
    name: 'sample:backchannel',
    value: 'XYZ'
});

Bot (C#)

await turnContext.SendActivityAsync(
    new Activity { 
        Type = ActivityTypes.Event, 
        Name = "sample:backchannel",
        Value = "XYZ"
    }
);

WebChat

const store = window.WebChat.createStore(
    {},
    ({ dispatch }) => next => action => {
        if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
            const { activity } = action.payload;

            if (activity.type === 'event' && activity.name === 'sample:backchannel') {
                alert(activity.value); // Do whatever you want in your front-end
            }
        }

        return next(action);
    }
);

View full sample in BotFramework-Webchat documentation



来源:https://stackoverflow.com/questions/60616474/ms-bot-framework-pass-the-values-from-server-code-c-to-front-end-code-javas

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