c# - 使用自定义 API 机器人无法使用 Bot 框架在 Microsoft Teams channel 中发布操作卡

标签 c# bots botframework microsoft-teams

当我调用我的 API 时,我必须从 bot 发送可操作卡片,然后该 API 将通过 Microsoft 团队 channel 中的 Bot 推送操作卡片,我将在其中传递 channel ID 和服务 URL

目前,我能够使用自定义 API 将简单消息成功发送到 Microsoft 团队 channel ,即用于发送简单消息。 但是在发送 Action 卡时它给出了异常(exception),例如,

{“事件导致多个 skype 事件”}

    public async Task<HttpResponseMessage> PostClause(ClauseRequest clauseRequest)
    {
       try
         {

            var channelId = "19:cf4306bb3aff49969b87420.......1@thread.skype";
            var serviceURL = "https://smba.trafficmanager.net/apac-client-ss.msg/";
            var connector = new ConnectorClient(new Uri(serviceURL));
            var channelData = new Dictionary<string, string>();
            channelData["teamsChannelId"] = channelId;
            IMessageActivity newMessage = Activity.CreateMessageActivity();
            newMessage.Type = ActivityTypes.Message;
            newMessage.Text = "Hello channel.";

            newMessage.Locale = "en-Us";
            var attachment = GetHeroCard();
            newMessage.Attachments = new List<Attachment>();
            newMessage.Attachments.Add(attachment);

            newMessage.SuggestedActions = new SuggestedActions()
            {
                Actions = new List<CardAction>()
                    {
                        new CardAction(){ Title = "Approve", Type=ActionTypes.ImBack, Value="Approve" },
                        new CardAction(){ Title = "Decline", Type=ActionTypes.ImBack, Value="Decline" }
                       // new CardAction(){ Title = "View in Google", Type=ActionTypes.OpenUrl, Value="https://www.google.co.in" }
                    }
            };

            ConversationParameters conversationParams = new ConversationParameters(
                isGroup: true,
                bot: null,
                members: null,
                topicName: "Test Conversation",
                activity: (Activity)newMessage,
                channelData: channelData);
            MicrosoftAppCredentials.TrustServiceUrl(serviceURL, DateTime.MaxValue);
            await connector.Conversations.CreateConversationAsync(conversationParams);
          }
          catch (Exception ex)
          {
           throw ex;
          }
    }


    private static Attachment GetHeroCard()
    {

        List<CardAction> cardButtons = new List<CardAction>();

        CardAction plButton = new CardAction()
        {
            Value = $"https://www.google.co.in",
            Type = "openUrl",
            Title = "View in Google"
        };

        cardButtons.Add(plButton);

        var heroCard = new HeroCard
        {
            Title = "BotFramework Hero Card",
            Subtitle = "Your bots — wherever your users are talking",
            Text = "Build and connect intelligent bots to interact with your users naturally wherever they are, from text/sms to Skype, Slack, Office 365 mail and other popular services.",
            Images = new List<CardImage> { new CardImage("https://sec.ch9.ms/ch9/7ff5/e07cfef0-aa3b-40bb-9baa-7c9ef8ff7ff5/buildreactionbotframework_960.jpg") },
            Buttons = cardButtons
        };

        return heroCard.ToAttachment();
    }

最佳答案

如文档中所述:

Teams 不支持 SuggestedActions

所以我更新了我的代码它现在可以工作了:)

    public async Task<HttpResponseMessage> PostClause(ClauseRequest clauseRequest)
    {
        try
        {
            var channelId = "19:cf4306bb3aff4996.......@thread.skype";
            var serviceURL = "https://smba.trafficmanager.net/apac-client-ss.msg/";
            var connector = new ConnectorClient(new Uri(serviceURL));
            var channelData = new Dictionary<string, string>();
            channelData["teamsChannelId"] = channelId;
            IMessageActivity newMessage = Activity.CreateMessageActivity();
            newMessage.Type = ActivityTypes.Message;


            var good = new CardAction("invoke", "Good", null, "{\"invokeValue\": \"Good\"}");
            var bad = new CardAction("invoke", "Bad", null, "{\"invokeValue\": \"Bad\"}");
            var card = new HeroCard("How are you today?", null, null, null, new List<CardAction> { good, bad }).ToAttachment();


            newMessage.Attachments.Add(card);

            ConversationParameters conversationParams = new ConversationParameters(
                isGroup: true,
                bot: null,
                members: null,
                topicName: "Test Conversation",
                activity: (Activity)newMessage,
                channelData: channelData);
            MicrosoftAppCredentials.TrustServiceUrl(serviceURL, DateTime.MaxValue);
            await connector.Conversations.CreateConversationAsync(conversationParams);
            var response = Request.CreateResponse(HttpStatusCode.OK);
            return response;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

关于c# - 使用自定义 API 机器人无法使用 Bot 框架在 Microsoft Teams channel 中发布操作卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45095567/

相关文章:

node.js - 如何在微软团队机器人中使用自适应卡在其他浏览器中打开网址?

c# - 表单流的动态返回类型

c# - 获取有关谁安装了 Microsoft Teams Bot 应用的信息

c# - 为什么此通用代码中不允许隐式转换?

c# - C# 中的开源 cad 绘图 (dwg) 库

C# 网络打印

c# - 来自 HTML Agility Pack 中行号/位置号的 HTMLNode

在 Slack 中阻止坏话/不当图片

python-3.x - 无法在 balebot 中使用 time.sleep() 发送一组消息

botframework - 在动态对话框中使用 Microsoft bot 框架