Dialogue tip with the Microsoft botframework

Hi,

Admittedly, dealing with dialogues within the bot framework isn’t an easy task. We often have to struggle to make that damn thing do what we want it to do. I recently had to include a potentially rich answer during one of my dialogues.  In short, the answer to a given question could contain a video but could as well be a basic text.

Moreover, this is only one step of the whole dialogue.  As I had to fight a little bit to get it work, I thought it might be worthwhile to make a short blog post about it.  So here is the code:

[Serializable]
public class VideoAnswer : IDialog<IMessageActivity>
{
    string _link = string.Empty;
    string _text = string.Empty;
    public VideoAnswer(string text,string link)
    {
        _link = link;
        _text = text;
    }
    public async Task StartAsync(IDialogContext context)
    {

        var reply = context.MakeMessage();
        reply.Text = _text;
        if (!string.IsNullOrEmpty(_link))
        {
            VideoCard card = new VideoCard();
            card.Text = "We found a video that should help you";
            List<MediaUrl> medias = new List<MediaUrl>();
            medias.Add(new MediaUrl
            {
                Url = _link
            });
            card.Media = medias;
            reply.Attachments.Add(card.ToAttachment());
        }
        await context.PostAsync(reply);
        context.Done(reply);
    }
}

[Serializable]
public class KbDialog : IDialog<object>
{
    string _path = null;
    Guid _source = Guid.Empty;
    public KbDialog(string path,Guid source)
    { 
        _path = path;
        _source = source;
    }
    public async Task StartAsync(IDialogContext context)
    {
        context.Call(Chain.From(() =>
            Chain.Return(SPUtils.GetSharePointAnswerToQuestion(_path, _source)))
            .ContinueWith<SharePointKB, IMessageActivity>(async (ctx, answ) =>
            {
                var a = await answ;
                return new VideoAnswer(
                    a.Answer,
                    a.VideoLink);
            })
            .ContinueWith<IMessageActivity, bool>(async (ctx, act) =>
            {
                return new PromptDialog.PromptConfirm(
                    Config.Cfg.DidItHelpPrompt, Config.Cfg.DidItHelpPromptInvalidAnswer, 100);
            }
            ), OnQAComplete);
    }

    private async Task OnQAComplete(IDialogContext context, IAwaitable<bool> result)
    {
        //do your stuff
        context.Done(this);
    }
}

The idea of the above code is to get some answer from SharePoint when a user asks a question. My KB may contain a video or not. So here in the KbDialog, which is itself part of a root dialog, I start getting the answer from SharePoint and I pass it to the next step on the form of a SharePointKb object. The next step’s output is an IMessageActivity which is implemented inside of the VideoAnswer class, itself implementing the IDialog interface. Within that separate dialogue, I reply either with text-only, either with text & video but always on the form of a IMessageActivity.

Happy Bot Coding!

Advertisements

About Stephane Eyskens

Office 365, Azure PaaS and SharePoint platform expert
This entry was posted in Azure and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s