繁体   English   中英

使用Microsoft Bot Framework V4主动向Microsoft Teams中的团队聊天发送消息

[英]Proactively send message to a Team chat in Microsoft Teams using Microsoft Bot Framework V4

我试图从我的Azure托管机器人向Microsoft Teams团队聊天主动发送一条特别消息。 我已成功将消息发送给用户与机器人进行的一对一聊天。

我已经看过该文档,其中详细说明了如何执行此操作,但是我认为这是Bot Framework版本3。它使用了Microsoft.Bot.Connector.Teams.Models掘金包,但版本4中不再支持该软件包。查找执行此操作的文档。

更具描述性:

我有一个机器人可以从我的Web应用程序接收带有警报数据的POST请求。 当漫游器收到这些POST请求之一时,它将消息转发给团队聊天。 此消息将在任何上下文之外发送。 我的转发到一对一聊天的代码可以在这里找到。

/// <summary>
    /// Forwards a message to a personal chat.
    /// The details of who to send the message to is included in the <paramref name="forwardContext"/>.
    /// </summary>
    /// <param name="forwardContext">JSON of the recipient details.</param>
    /// <param name="messageToSend">Text message to send to chat.</param>
    /// <returns>A <see cref="Task"/> representing the asynchronous messsage forward.</returns>
    private async Task ForwardMessageToPersonalChatAsync(JToken forwardContext, string messageToSend)
    {
        // Collect data from JSON input
        var restCmd = forwardContext;
        var toId = (string)restCmd["toId"];
        var toName = (string)restCmd["toName"];
        var fromId = (string)restCmd["fromId"];
        var fromName = (string)restCmd["fromName"];
        var channelId = (string)restCmd["channel"];
        var serviceURL = (string)restCmd["serviceURL"];
        var conversationId = (string)restCmd["conversation"];
        var tenant = (string)restCmd["tenant"];
        var cred_str = $@"toId: {toId}
        toName: {toName}
        fromId: {fromId}
        fromName: {fromName}
        channelId: {channelId}
        serviceURL: {serviceURL}
        conversationId: {conversationId}";
        this.logger.LogInformation(cred_str);
        this.logger.LogInformation($"Forwarding the following message to {toName}: {messageToSend}");

        var uri = new System.Uri(serviceURL);

        Dictionary<string, string> botCreds = this.GetBotCredentials();
        ConnectorClient connector = new ConnectorClient(uri, botCreds["App ID"], botCreds["App Password"]);
        var activity = new Activity
        {
            Type = ActivityTypes.Message,
            From = new ChannelAccount(fromId, fromName),
            Recipient = new ChannelAccount(toId, toName),
            Conversation = new ConversationAccount(false, "personal", conversationId),
            Text = messageToSend,
        };

        try
        {
            MicrosoftAppCredentials.TrustServiceUrl(serviceURL);
            await connector.Conversations.SendToConversationAsync(conversationId, activity);
        }
        catch (System.Exception ex)
        {
            this.logger.LogError(ex.ToString());
        }
    }

如何向团队聊天发送主动消息?

谢谢你的帮助。

团队尚未正式支持Bot Framework 4.0。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM