如何使用BotFramework SDK v4上的Bot向特定的频道和特定的用户发送主动消息

时间:2019-07-04 05:29:05

标签: c# .net-core botframework microsoft-teams skype-for-business

单独使用漫游器向Microsoft Teams&Skype发送通知消息(自适应卡)。

我实现了批准工作流程,我正在尝试将批准通知发送到以下渠道: -Skype -队伍

根据用户可以在工作流应用程序的通知设置中设置的通知首选项。

我创建了一个主动机器人,将自适应卡和触发器发送给团队。但是我不知道机器人如何将通知发送到所需的频道。

我的代码如下:

ProActiveBot

public class ProActiveBot : ActivityHandler
    {
        private ConcurrentDictionary<string, ConversationReference> _conversationReferences;
        public ProActiveBot(ConcurrentDictionary<string, ConversationReference> conversationReferences)
        {
            _conversationReferences = conversationReferences;
        }
        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> context, CancellationToken cancellationToken)
        {
            ConversationReference conversationReference = context.Activity.GetConversationReference();
            _conversationReferences.AddOrUpdate(conversationReference.User.Id, conversationReference, (key, newValue) => conversationReference);
            await context.SendActivityAsync(MessageFactory.Text($"Sent: {context.Activity.Text}"), cancellationToken);
        }
        protected override Task OnConversationUpdateActivityAsync(ITurnContext<IConversationUpdateActivity> context, CancellationToken cancellationToken)
        {
            ConversationReference conversationReference = context.Activity.GetConversationReference();
            _conversationReferences.AddOrUpdate(conversationReference.User.Id, conversationReference, (key, newValue) => conversationReference);
            return base.OnConversationUpdateActivityAsync(context, cancellationToken);
        }
    }

通知控制器

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

        public TriggerController(IBotFrameworkHttpAdapter adapter, ICredentialProvider credentials, ConcurrentDictionary<string, ConversationReference> conversationReferences)
        {
            _adapter = adapter;
            _conversationReferences = conversationReferences;
            _appId = ((SimpleCredentialProvider)credentials).AppId;
        }

        public async Task<IActionResult> Get()
        {
            foreach (var conversationReference in _conversationReferences.Values)
            {
                await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));
            }
            return new ContentResult()
            {
                Content = @"<html></body><h1>Sent</h1></body></html>",
                ContentType = "text/html",
                StatusCode = (int)HttpStatusCode.OK,
            };
        }



private async Task BotCallback(ITurnContext context, CancellationToken cancellationToken)
    {
        await context.SendActivityAsync(MessageFactory.Attachment(new Attachment()
        {
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = JsonConvert.DeserializeObject(System.IO.File.ReadAllText(Path.Combine(".", "Cards", "notification.json"))),
        }), cancellationToken);
    }
}

有人可以帮我理解我如何选择一个特定的频道,以在BotFramework上向其发送通知。

0 个答案:

没有答案