简体   繁体   English

Bot Framework:方法在尝试等待值后退出

[英]Bot Framework: Method exits after attempting to await value

I seem to be having some issues with IAwaitable. 我似乎对IAwaitable有一些问题。 I have the following dialog 我有以下对话框

[Serializable]
public class SearchDialog : IDialog<object>
{
    private SpotifyClient _client;

    public SearchDialog()
    {
        _client = new SpotifyClient();
    }

    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("Please enter the name of the artist");
        context.Wait<string>(MessageReceived);
    }

    public async Task MessageReceived(IDialogContext context, IAwaitable<string> activity)
    {
        var message = await activity;

        var results = await _client.SearchForArtistAsync(message);

        if(results != null && results.Artists.Results.Any())
        {
            var artists = results.Artists.Results.Select(x => x.Name); // List<string>

            PromptDialog.Choice(context,
                ChoiceSelectAsync,
                artists,
                string.Empty,
                "Didn't get that",
                3,
                PromptStyle.PerLine);
        }
    }

    public async Task ChoiceSelectAsync(IDialogContext context, IAwaitable<string> choice)
    {
        var chosenArtist = await choice;

        await context.PostAsync($"You have chosen {chosenArtist}");

        context.Wait<string>(ChoiceSelectAsync);
    }
}

The problem is, the prompt dialog is never displayed. 问题是,提示对话框从不显示。 The MessageReceived method seems to tries to await activity, but then exits the method. MessageReceived方法似乎尝试等待活动,但随后退出该方法。 Why would it not await the result if i'm using the await keyword? 如果我使用await关键字,为什么不等待结果呢? Any help would be greatly appreciated. 任何帮助将不胜感激。

The signature of the MessageReceived method is wrong. MessageReceived方法的签名错误。 It should be an IAwaitable<MessageActivity> instead of IAwaitable<string> 它应该是IAwaitable<MessageActivity>而不是IAwaitable<string>

public async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> activity)

After doing that, you need to also update the context.Wait call to context.Wait(MessageReceived); 之后,您还需要更新context.Wait调用context.Wait(MessageReceived);

Then, to access to the text sent by the user just check the Text property of the awaited message: 然后,要访问用户发送的文本,只需检查等待消息的Text属性:

  var message = await activity;

  var results = await _client.SearchForArtistAsync(message.Text);

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

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