简体   繁体   中英

Bot Framework: Read the outgoing message of the Conversation

Is there a way to use the Conversation so that the IMessageActivity to be sent to the user is returned to the caller instead of sent back to the user uri?
What I want to accomplish is to handle myself the incoming and outgoing messages, and while I can create an activity to be supplied to the Conversation object I cannot read and use the IMessageActivity reply.

To be more specific I'm wondering if I can substitute

Conversation.SendAsync(IMessageActivity, Func<IDialog<object>>);

With something that will let me handle the reply.

Thank you.

The reply should be handled by the dialog passed to the SendAsync method.

In the dialog, you will usually have a method that receives the context and the message sent by the user. For example:

public async virtual Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
   var message = await result;
}

In the StartAsync method of the dialog you will have to set this method as the one that will be called once the user sends a message to the bot

public async Task StartAsync(IDialogContext context)
{
    context.Wait(this.MessageReceivedAsync);
}

Then, is up to you to decide what to do after receiving the message.

Do you want to use a PromptDialog , eg the Confirm one? You can, just instantiate the dialog, call it and get the result in the ResumeAfter<bool> method.

PromptDialog.Confirm(context, ResumeAfterPrompt, "prompt dialogue", "retry dialog", 3);

private async Task ResumeAfterPrompt(IDialogContext context, IAwaitable<bool> result)
{
    try
    {
        // try get user response
        bool response = await result;

        await context.PostAsync($"You said: {result}");
    }
    catch (TooManyAttemptsException)
    {
        // handle error
    }

   // wait for another message from the user. Could be the same method or a new one following the same signature.
   context.Wait(this.MessageReceivedAsync);
}

Do you want to use a FormFlow? You also, just instantiate the FormDialog and get the result in the ResumeAfter<T> method you define.

It's always the same pattern when working with multi dialogs (Prompts and Forms are dialogs!): call the dialog, get the result in the ResumeAfter method and wait for a new user message.

All this concepts are well explained and also demonstrated in the Multi-Dialogs sample . Go through the readme and the codebase and you will understand what I explained. That sample shows prompts, a FormFlow, custom dialogs, etc. Here there are a few more details.

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