从主动消息中启动对话框 [英] Starting A Dialog From From A Proactive Message
本文介绍了从主动消息中启动对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Bot Framework的新手,所以很抱歉,如果这是基本知识,但我正尝试向用户发送主动消息,以开始对话.我正在使用以下示例:
I'm new to the Bot Framework so I'm sorry if this is basic, but I'm trying to send a Proactive message to the user start a conversation. I'm using the below sample:
这很好用,但是我想做的是从这一点开始对话,而不是仅仅发送一些文本给用户.这可能吗?这是样本中的代码
This works perfectly but what I'd like to do is start a dialog from this point instead of just sending back some text to the user. Is this possible? Here is the code from the sample
[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);
}
}
推荐答案
我最终弄明白了-这是我所做的:
I ended up figuring this out - here is what I did:
在我的NotifyController中,我像这样开始对话
In my NotifyController, I start the conversation like this
[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中启动对话框:
Then in the BotCallback I start the dialog:
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);
}
}
这篇关于从主动消息中启动对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文