chatbot在本地bot框架仿真器中可以正常显示欢迎消息,但在azure chatbot中不起作用

时间:2019-07-15 08:34:06

标签: c# asp.net-core-2.0 chatbot

我使用c#在Asp.net核心中创建了一个聊天机器人,当用户第一次打开聊天机器人时,我遇到有关欢迎消息的问题。当我使用Microsoft Bot框架模拟器时,它工作正常。但是当我在azure bot选项中创建bot服务时,它不是通过欢迎消息发送的。但是,是的,当您键入任何内容并按发送按钮之后,将显示一条欢迎消息。意思是,Azure bot收到任何消息,然后响应,但是当用户打开聊天bot面板时,我想先显示我的欢迎消息。知道解决方案是什么吗?

我使用了这段代码,它在本地bot框架仿真器中工作正常,但在azure bot中却无法正常工作。

namespace Microsoft.BotBuilderSamples
{
    public class DialogAndWelcomeBot<T> : DialogBot<T> where T : Dialog
    {
        public DialogAndWelcomeBot(ConversationState conversationState, UserState userState, T dialog, ILogger<DialogBot<T>> logger)
            : base(conversationState, userState, dialog, logger)
        {
        }

        protected override async Task OnMembersAddedAsync(
            IList<ChannelAccount> membersAdded,
            ITurnContext<IConversationUpdateActivity> turnContext,
            CancellationToken cancellationToken)
        {
            foreach (var member in membersAdded)
            {   
                if (member.Id != turnContext.Activity.Recipient.Id)
                {   
                    var replyOne = MessageFactory.Text($"Hi there! I'm bot. ");
                    await turnContext.SendActivityAsync(replyOne, cancellationToken);
                    var replyTwo = MessageFactory.Text($"How may i assist you?");
                    await turnContext.SendActivityAsync(replyTwo, cancellationToken);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以参考此代码。它会帮助你。

IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
if (iConversationUpdated != null)
{
    ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));

    foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
    {
        // if the bot is added, then
        if (member.Id == iConversationUpdated.Recipient.Id)
        {
            var reply = ((Activity)iConversationUpdated).CreateReply(
                $"Hello Bot");
            connector.Conversations.ReplyToActivityAsync(reply);
        }
    }
}