【问题标题】:Starting A Dialog From From A Proactive Message从主动消息开始对话
【发布时间】:2019-05-30 02:53:15
【问题描述】:

我是 Bot Framework 的新手,所以如果这是基本的,我很抱歉,但我正在尝试向用户发送主动消息以开始对话。我正在使用以下示例:

https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/16.proactive-messages

这很好用,但我想做的是从这一点开始一个对话框,而不是仅仅向用户发送一些文本。这可能吗?这是示例中的代码

[Route("api/notify")]
[ApiController]
public class NotifyController : ControllerBase
{
    private readonly IBotFrameworkHttpAdapter _adapter;
    private readonly string _appId;
    private readonly ConcurrentDictionary<string, ConversationReference> _conversationReferences;

    private readonly BotState _userState;
    private readonly BotState _conversationState;

    public NotifyController(IBotFrameworkHttpAdapter adapter,
        ICredentialProvider credentials,
        ConcurrentDictionary<string, ConversationReference> conversationReferences,
        ConversationState conversationState,
        UserState userState
        )
    {
        _adapter = adapter;
        _conversationReferences = conversationReferences;
        _appId = ((SimpleCredentialProvider)credentials).AppId;

        // If the channel is the Emulator, and authentication is not in use,
        // the AppId will be null.  We generate a random AppId for this case only.
        // This is not required for production, since the AppId will have a value.
        if (string.IsNullOrEmpty(_appId))
        {
            _appId = Guid.NewGuid().ToString(); //if no AppId, use a random Guid
        }


        _conversationState = conversationState;
        _userState = userState;
    }

    [HttpGet("{number}")]
    public async Task<IActionResult> Get(string number)
    {
        foreach (var conversationReference in _conversationReferences.Values)
        {
            await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));
        }

        // Let the caller know proactive messages have been sent
        return new ContentResult()
        {
            Content = "<html><body><h1>Proactive messages have been sent.</h1></body></html>",
            ContentType = "text/html",
            StatusCode = (int)HttpStatusCode.OK,
        };
    }

    private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        //This works from the sample:
        await turnContext.SendActivityAsync("Starting proactive message bot call back");

        //However I would like to do something like this (pseudo code):
        //var MyDialog = new ConfirmAppointmentDialog();
        //await turnContext.StartDialog(MyDialog);
    }
}

【问题讨论】:

  • 您希望发送主动消息以响应什么?也就是说,是什么触发了它?
  • 你还在做这个吗?
  • 抱歉,我仍在努力解决这个问题 - 但我最终解决了这个问题。一个 Azure 函数将调用我的 NotifyController,它有一个方法,现在可以通过对话框开始对话。我会为其他人发布解决方案。非常感谢。
  • this._Dialog at dialogSet.Add(this._Dialog);我无法将 _Dialog 视为依赖项或函数输入,建议使用。 @瑞安

标签: c# botframework


【解决方案1】:

我最终弄明白了——这就是我所做的:

在我的 NotifyController 中,我像这样开始对话

  [HttpGet("{number}")]
        public async Task<IActionResult> Get(string number)
        {

            //For Twillio Channel
            MicrosoftAppCredentials.TrustServiceUrl("https://sms.botframework.com/");

            var NewConversation = new ConversationReference
            {
                User = new ChannelAccount { Id = $"+1{number}" },
                Bot = new ChannelAccount { Id = "+1YOURPHONENUMBERHERE" },
                Conversation = new ConversationAccount { Id = $"+1{number}" },
                ChannelId = "sms",
                ServiceUrl = "https://sms.botframework.com/"
            };

            try
            {
                BotAdapter ba = (BotAdapter)_HttpAdapter;
                await ((BotAdapter)_HttpAdapter).ContinueConversationAsync(_AppId, NewConversation, BotCallback, default(CancellationToken));
            }
            catch (Exception ex)
            {
                this._Logger.LogError(ex.Message);
            }


            // Let the caller know proactive messages have been sent
            return new ContentResult()
            {
                Content = "<html><body><h1>Proactive messages have been sent.</h1></body></html>",
                ContentType = "text/html",
                StatusCode = (int)HttpStatusCode.OK,
            };
        }

然后在 BotCallback 中我启动对话框:

private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
        {
            try
            {
                var conversationStateAccessors = _ConversationState.CreateProperty<DialogState>(nameof(DialogState));

                var dialogSet = new DialogSet(conversationStateAccessors);
                dialogSet.Add(this._Dialog);

                var dialogContext = await dialogSet.CreateContextAsync(turnContext, cancellationToken);
                var results = await dialogContext.ContinueDialogAsync(cancellationToken);
                if (results.Status == DialogTurnStatus.Empty)
                {
                    await dialogContext.BeginDialogAsync(_Dialog.Id, null, cancellationToken);
                    await _ConversationState.SaveChangesAsync(dialogContext.Context, false, cancellationToken);
                }
                else
                    await turnContext.SendActivityAsync("Starting proactive message bot call back");
            }
            catch (Exception ex)
            {
                this._Logger.LogError(ex.Message);
            }
        }

【讨论】:

  • 你也可以为nodejs提供这个吗?
  • @Ryan 你能告诉你从哪里得到这个._Dialog吗?我想为我的应用程序实现类似的解决方案,但没有得到 this._Dialog。请帮助我。
  • @nil - 不幸的是,我没有这个确切的示例代码可以分享,但你应该能够做的就是创建一个新的对话框实例。我在下面有类似的东西:` //接下来,创建一个新对话框并运行它 var Dialog = new AppointmentCheckInMainDialog(); dialogSet.Add(对话框);等待 dialogContext.BeginDialogAsync(Dialog.Id, null, cancelToken); ` 我希望这有帮助,如果没有,请告诉我。谢谢
  • 在我的例子中,我在启动类中使用了 DI 注入的对话框:services.AddSingleton();
  • 感谢@Ryan,您的回答对我帮助很大。在我当前使用的机器人框架(v4.14)上,不再需要此行:MicrosoftAppCredentials.TrustServiceUrl("https://sms.botframework.com/"); 最后,我不断收到 401 错误,但这是由于没有使用正确的源电话号码。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
相关资源
最近更新 更多