是否可以像向用户发送消息一样向Bot Framework发送消息? [英] Is it possible to send a message to the Bot Framework as if it were from the user?

查看:66
本文介绍了是否可以像向用户发送消息一样向Bot Framework发送消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Direct Line 3.0和Microsoft Bot Framework,并要求网页将某些表单字段发送给bot,就像用户发送它们一样.例如,当用户按下Submit(提交)时,电子邮件,电话等字段将被发送到漫游器,就像用户通过以下方式发送给他们一样:电子邮件,电话等. 这是因为bot根据值是什么来重定向用户.该机器人使用C#并托管在Azure上.提交信息的逻辑应该使用JavaScript.

I'm using Direct Line 3.0 and the Microsoft Bot Framework and require the webpage to send some form fields to the bot as if the user sent them. For example when the user presses Submit, the fields email, phone etc are sent to the bot as if the user sent them like this: email, phone, etc. This is because the bot redirects the user depending on what the values are. The bot is in C# and is hosted on Azure. The logic for submitting the information should be in JavaScript.

启动是这样的:

<div id="chat" style="background-color:white;   
   width:250px;height:600px;"><div id="bot" />
<script src="https://cdn.botframework.com/botframework-
webchat/latest/botchat.js"></script></div></div>

并通过DirectLine脚本:

and through a DirectLine script:

<script>
    const botConnection = new BotChat.DirectLine({
        secret: 'secret',
    });

    BotChat.App({
        user: { id: 'You' },
        bot: { id: 'myId' },
        resize: 'detect',
        botConnection: botConnection
    }, document.getElementById("bot"));

</script>

我所需要的只是发送一个字符串,就像用户发送它一样.我似乎无法通过HTML操作来做到这一点.

All I need is to send one string as if the user sent it. I cannot do this with HTML manipulation it seems.

感谢任何指出我正确方向的人!

Thanks for anyone pointing me in the right direction!

推荐答案

使用网聊的反向渠道"功能,可以像用户一样"向机器人发送消息.

Sending a message to the bot "like the user would do" is possible using the "Backchannel" functionnality of the webchat.

Github网络聊天页面上的自述文件中有一个很好的用法示例: https ://github.com/Microsoft/BotFramework-WebChat#the-backchannel .

There is a good sample of use in the Readme file on Github webchat's page: https://github.com/Microsoft/BotFramework-WebChat#the-backchannel.

您必须使用先前创建的botConnection来发送如下活动:

You have to use your botConnection previously created to send an activity like the following:

botConnection.postActivity({
    from: { id: 'me' },
    name: 'buttonClicked',
    type: 'event',
    value: ''
});

然后将其捕获到您的机器人代码中,但检查活动"类型(在这种情况下为Event).

Then catch this on your bot code, but checking the Activity type which will be Event in this case.

您可以通过单击提供的样本中的按钮来查看他们如何抛出此postActivity:此处的样本:

You can have a look on how they throw this postActivity from a button click in the sample provided: samples here: https://github.com/Microsoft/BotFramework-WebChat/blob/master/samples/backchannel/index.html

或者在其他示例中我所做的(可在Github上获得,包括客户端网页和bot代码):bot的控制器如下所示:

Or in this other sample that I made (available on Github, both client web page and bot code): the bot's controller looks like the following:

[BotAuthentication]
public class MessagesController : ApiController
{
    /// <summary>
    /// POST: api/Messages
    /// Receive a message from a user and reply to it
    /// </summary>
    public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        // Process each activity
        if (activity.Type == ActivityTypes.Message)
        {
            await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
        }
        // Webchat: getting an "event" activity for our js code
        else if (activity.Type == ActivityTypes.Event && activity.ChannelId == "webchat")
        {
            var receivedEvent = activity.AsEventActivity();

            if ("localeSelectionEvent".Equals(receivedEvent.Name, StringComparison.InvariantCultureIgnoreCase))
            {
                await EchoLocaleAsync(activity, activity.Locale);
            }
        }
        // Sample for Skype: locale is provided in ContactRelationUpdate event
        else if (activity.Type == ActivityTypes.ContactRelationUpdate && activity.ChannelId == "skype")
        {
            await EchoLocaleAsync(activity, activity.Entities[0].Properties["locale"].ToString());
        }

        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

    private async Task EchoLocaleAsync(Activity activity, string inputLocale)
    {
        Activity reply = activity.CreateReply($"User locale is {inputLocale}, you should use this language for further treatment");
        var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
        await connector.Conversations.SendToConversationAsync(reply);
    }
}

这篇关于是否可以像向用户发送消息一样向Bot Framework发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆