首页 > 解决方案 > 向用户发送主动消息的技能机器人

问题描述

  1. 我开发了一个向用户发送主动消息的机器人
  2. 我还创建了触发技能的机器人。我试图写一些东西,技能机器人/对话能够将通过 webhook 收到的主动消息发送给用户并继续现有的技能对话。我不能完全理解。如果有人可以在那里帮助我。

我使用此处的示例创建了一个简单的技能机器人,它保存ConversationReference当前活动并调用服务OnMessageActivityReceived()

// Save ConversationReference
var conversationReference = activity.GetConversationReference();
_conversationReferences.AddOrUpdate(conversationReference.User.Id, conversationReference, (key, newValue) => conversationReference);

// Calling external service
HttpResponseMessage responsemMsg = await client.PostAsync(RequestURI, stringContent);
if (responsemMsg.IsSuccessStatusCode)
{
    var apiResponse = await responsemMsg.Content.ReadAsStringAsync();

    //Post the API response to bot again
    await turnContext.SendActivityAsync(MessageFactory.Text($"Message Sent {turnContext.Activity.Text}"), cancellationToken);

}

被调用的服务最终会发出一个事件,该事件调用NotifyController我的技能机器人中的一个动作。它尝试使用 获取ConverstaionReference并发送活动TurnContext


//I am using the Skill Bot Id for _appId parameter
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, async (context, token) =>
 {
     await context.SendActivityAsync(proMsg.eventName);
     await context.SendActivityAsync(proMsg.context.ToString());
  }, new System.Threading.CancellationToken());

此 SendActivity 失败,来自 Skill Bot 的 OnTurnError 处理异常。我不确定我哪里出错了。我是 Bot 框架学习的新手。

标签: botframework

解决方案


我的问题通过使用带有 ClaimsIdentity 的 ContinueConversation() 重载并为受众、appid 等设置正确的声明得到解决。这基本上是身份验证问题。

public virtual Task ContinueConversationAsync(ClaimsIdentity claimsIdentity, ConversationReference reference, string audience, BotCallbackHandler callback, CancellationToken cancellationToken);

推荐阅读