简体   繁体   English

是否可以像从用户那里一样向Bot Framework发送消息?

[英]Is it possible to send a message to the Bot Framework as if it were from the user?

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. 我正在使用Direct Line 3.0和Microsoft Bot Framework,并且要求网页将某些表单字段发送给bot,就像用户将其发送一样。 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. 例如,当用户按下Submit(提交)时,电子邮件,电话等字段将被发送给bot,就像用户向其发送电子邮件,电话等那样。这是因为bot会根据值进行重定向,从而重定向用户。 The bot is in C# and is hosted on Azure. 该机器人使用C#并托管在Azure上。 The logic for submitting the information should be in JavaScript. 提交信息的逻辑应使用JavaScript。

Bot is initiated like this: Bot是这样启动的:

<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>

and through a DirectLine script: 并通过DirectLine脚本:

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

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

</script>

All I need is to send one string as if the user sent it. 我所需要的只是发送一个字符串,就像用户发送它一样。 I cannot do this with HTML manipulation it seems. 我似乎无法通过HTML操作来做到这一点。

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. 使用网聊的“反向通道”功能,可以“像用户一样”向机器人发送消息。

There is a good sample of use in the Readme file on Github webchat's page: https://github.com/Microsoft/BotFramework-WebChat#the-backchannel . Github网络聊天页面上的自述文件中有一个很好的用法示例: https : //github.com/Microsoft/BotFramework-WebChat#the-backchannel

You have to use your botConnection previously created to send an activity like the following: 您必须使用先前创建的botConnection来发送如下活动:

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

Then catch this on your bot code, but checking the Activity type which will be Event in this case. 然后在您的机器人代码中捕获该错误,但检查“活动”类型,在这种情况下将为“ Event

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 您可以通过单击提供的示例中的按钮来查看他们如何抛出此postActivity:示例: https : //github.com/Microsoft/BotFramework-WebChat/blob/master/samples/backchannel/index.html

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: 或在我制作的另一个示例中(可在Github上获得,包括客户端网页和bot代码):bot的控制器如下所示:

[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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM