简体   繁体   中英

Azure Bot template - BotApplication

I downloaded Azure bot template. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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