简体   繁体   English

Azure Bot模板-BotApplication

[英]Azure Bot template - BotApplication

I downloaded Azure bot template. 我下载了Azure机器人模板。 right now my bot is working and replying an echo of what i said and how many characters it had when I run it with the bot framework emulator. 现在,我的机器人正在工作,并回复了我所说的内容以及使用机器人框架模拟器运行它时有多少个字符的回声。 but i want my bot to start a conversation. 但我希望我的机器人开始对话。 how do i do that? 我怎么做? i want the bot to say hello first regardless to a user input. 我希望机器人无论用户输入如何先打招呼。 the method "post async" only prints the message to the chat after input was received from the user. “异步后发布”方法仅在收到用户输入后才将消息打印到聊天中。

code: 码:

namespace BotApplication1.Dialogs
{
    [Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            Microsoft.Bot.Builder.Dialogs.Internals.IBotToUser($"Hi! Please type in a name of a public figure!");//compile time error
            context.PostAsync($"Hello user");//prints the "hello user" only after user input
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var activity = await result as Activity;

            // Calculate something for us to return
            int length = (activity.Text ?? string.Empty).Length;

            // Return our reply to the user
            await context.PostAsync($"You sent {activity.Text} which was {length} characters");

            context.Wait(MessageReceivedAsync);
        }
    }
}

any help? 有什么帮助吗? if not here then where to ask???? 如果不在这里,那么在哪里问??? please! 请!

thank you 谢谢

Hadas 哈达斯

You should add the welcome message in the conversationUpdate in the MessagesController not in the RootDialog. 你应该在添加的欢迎信息conversationUpdateMessagesController在RootDialog没有。

If your bot receives a conversationUpdate activity indicating that a user has joined the conversation, you may choose to have it respond by sending a welcome message to that user. 如果您的机器人收到指示用户已加入对话的对话更新活动,则可以选择通过向该用户发送欢迎消息来使其响应。

Demo code. 演示代码。

if (message.Type == ActivityTypes.ConversationUpdate)
    {
        // Handle conversation state changes, like members being added and removed
        // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
        // Not available in all channels

        // Note: Add introduction here:
        IConversationUpdateActivity update = message;
        var client = new ConnectorClient(new Uri(message.ServiceUrl), new MicrosoftAppCredentials());
        if (update.MembersAdded != null && update.MembersAdded.Any())
        {
            foreach (var newMember in update.MembersAdded)
            {
                if (newMember.Id != message.Recipient.Id)
                {
                    var reply = message.CreateReply();
                    reply.Text = $"Welcome {newMember.Name}!";
                    client.Conversations.ReplyToActivityAsync(reply);
                }
            }
        }
    }

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

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