How do I send a value from a Microsoft Bot to Javascript?

蓝咒 提交于 2019-12-13 17:22:01

问题


I need to send a string from the MessagesController in a Microsoft Framework Bot to a script written in JS. The string contains the value of a secret for a bot we are hosting which is inactive (not in use). Is there a way to send a string from an Azure hosted bot?

Edit: I have tried to create a method which returns a string but cannot retrieve it through a url (sitename.azurewebsites.net/api/messages/methodname), I cannot use Jsonresult or Actionresult as the bot isn't MVC.

More info: We have set up 5 bots which set themselves as being active (in a chat session) or inactive (not in a chat session). We know if a bot is active or not by the values set in a database. There is a stored procedure in the database which returns a direct line secret for an inactive bot. What I want to do is send this secret (string) from the database to the JavaScript so we can generate a bot dynamically. The problem is, we do not want to connect to the database from the JS on the front end, instead we want to get the secret from a bot (any bot). Therefore I would like to get a string value from the MessagesController into JavaScript from the hosted bot on azure.

Thanks

Edit2:

 public IHttpActionResult GetString()
        {
            return Ok("it worked");
        }

returns BotAuthenticator failed to authenticate incoming request!

回答1:


You can achieve the following:

I need to send a string from the MessagesController in a Microsoft Framework Bot to a script written in JS.

By using the backchannel capability of the webchat channel. Based on your query I assume your are using this channel here.

To get more information about the backchannel, have a look here. The idea is to send an event from your bot code, that will be received by the JS on the client side:

The client JavaScript also listens for a specific event from the bot. In their sample they show the ability to change a color on the client side, based on an event sent by the bot:

botConnection.activity$
    .filter(activity => activity.type === "event" && activity.name === "changeBackground")
    .subscribe(activity => changeBackgroundColor(activity.value))

You can use the following to send the secret you require and activate the right bot.



来源:https://stackoverflow.com/questions/49532572/how-do-i-send-a-value-from-a-microsoft-bot-to-javascript

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