使用团队的bot框架向群聊发送消息

时间:2020-05-01 04:44:32

标签: c# botframework microsoft-teams

我想主动向机器人添加到的群组发送消息(而不是团队,简单的群组聊天)

到目前为止,我正在做的是将对话ID保存(在内存中)在单例字典中 然后向所有对话发出通知。 这是API端点代码。

        public CINotificationsController(
            IBotFrameworkHttpAdapter adapter,
            IBot bot,
            IConfiguration config,
            ConcurrentDictionary<string, ConversationReference> conversationReferences,
            ILogger<CINotificationsController> logger)
        {
            _Adapter = adapter;
            _Bot = bot;
            _ConversationReferences = conversationReferences;
            _Logger = logger;
            _AppId = config.GetSection("MicrosoftAppId").Value;

        }

        [HttpPost]
        public async Task<IActionResult> PostAsync(CINotification notificationData)
        {
            _Logger.LogInformation($"Got CI notification, {JsonConvert.SerializeObject(notificationData)}");
            var jobName = notificationData.JobName;
            var culpritsEmails = notificationData.Commiter;

            foreach (var conv in _ConversationReferences.Values)
            {
                await ((BotAdapter)_Adapter).ContinueConversationAsync(_AppId, conv, GetBotCallBack(notificationData, conv), default);
            }

            return Ok(culpritsEmails);

        }

        private BotCallbackHandler GetBotCallBack(CINotification notificationData, ConversationReference conv)
        {
            return async (ctx, cts) =>
            {
                _Logger.LogDebug($"conversationId:[{conv.ActivityId}], serviceUrl:[{conv.ServiceUrl}]");
                var mention = new Mention();
                if (!string.IsNullOrEmpty(notificationData.Commiter))
                {
                    var membersByEmail = (await TeamsInfo.GetMembersAsync(ctx, cts)).ToDictionary(k => k.Email, v => v, StringComparer.OrdinalIgnoreCase);
                    _Logger.LogDebug($"members:[{string.Join(",", membersByEmail.Keys)}]");
                    if (membersByEmail.TryGetValue(notificationData.Commiter, out var teamMemeber))
                    {
                        mention.Mentioned = teamMemeber;
                        mention.Text = $"<at>{teamMemeber.Name}</at>";
                    }

                    _Logger.LogDebug($"got mentions: {mention.Text}");

                }

                var msgText = $"{mention.Text} {notificationData.Message}";
                _Logger.LogDebug($"Sending message text: {msgText}");
                var replyActivity = MessageFactory.Text(msgText);
                replyActivity.Entities.Add(mention);

                await ctx.SendActivityAsync(replyActivity);
            };
        }

当然,内存词典_ConversationReferences是一个坏主意,我正在寻找某种方法(API,这种形式是SDK)来列出该漫游器已添加到的所有组或所有已进行的对话。开始..

我是否必须将其存储在某些数据库中或者我有其他选择?

2 个答案:

答案 0 :(得分:1)

我认为不存在这样的API调用。例如,您可以从Graph API获得list of all apps installed into a team,但对于群聊或1-1聊天则没有这种东西。

但是,您-已经拥有了所有这些信息-只需将其存储在自己的身边,这些天就拥有如此多的良好存储选项(云计算或其他方法)。在那种情况下,它完全在您的控制之下。

答案 1 :(得分:0)

您可以使用此代码

    // Create or get existing chat conversation with user
    var response = client.Conversations.CreateOrGetDirectConversation(activity.Recipient, activity.From, activity.GetTenantId());

    // Construct the message to post to conversation
    Activity newActivity = new Activity()
    {
        Text = "Hello",
        Type = ActivityTypes.Message,
        Conversation = new ConversationAccount
        {
            Id = response.Id
        },
    };

// Post the message to chat conversation with user
await client.Conversations.SendToConversationAsync(newActivity, response.Id);

在这里

https://docs.microsoft.com/en-us/microsoftteams/platform/bots/how-to/conversations/send-proactive-messages?tabs=dotnet