The*_*bot 2 c# asp.net-core botframework
我正在尝试在 bot 框架 v4 上实现主动消息,它可以工作,但只能使用 BotCallback 函数上的字符串,我需要传递自定义文本,但 ContinueConversationAsync 似乎不允许它
public async Task<bool> SendProactiveMessage(MensajeExterno mensajeExterno)
{
var referenciaDeConversacion = ObtenerReferenciaConversacion(mensajeExterno.ConversationId);
var continuationActivity = referenciaDeConversacion.GetContinuationActivity();
if (referenciaDeConversacion == null) return false;
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, referenciaDeConversacion, BotCallback, default(CancellationToken));
return true;
}
private ConversationReference ObtenerReferenciaConversacion(string conversationId)
{
return new ConversationReferenceModulo().ObtenerConversationReference(conversationId);
}
public MensajeroDefaultModulo(IBotFrameworkHttpAdapter adapter, IConfiguration configuration)
{
_adapter = adapter;
_appId = configuration["MicrosoftAppId"];
if (string.IsNullOrEmpty(_appId))
{
_appId = Guid.NewGuid().ToString(); //if no AppId, use a random Guid
}
}
private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
{
var activity = turnContext.Activity;
await turnContext.SendActivityAsync("proactive hello", cancellationToken: cancellationToken);
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 LINQ 方法对 BotCallback 进行包装。但我个人不理解没有任何 EventArgs 的代表的想法。
[HttpPost]
public async Task<IActionResult> Post([FromBody] string messageText)
{
foreach (var conversationReference in _conversationReferences.Values)
{
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, async (context, token) => await BotCallback(messageText, context, token), default(CancellationToken));
}
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(string message, ITurnContext turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync(message, cancellationToken: cancellationToken);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
690 次 |
| 最近记录: |