简体   繁体   中英

Using Custom API bot can't post action card in Microsoft Teams channel using Bot framework

I have to send actionable card from bot when I call my API then that API will push action card throught Bot in microsoft teams channel where I will pass channel ID and service URL

Currently I am successfully able to send Simple message into Microsoft teams channel using custom API ie working for send simple messages. But while sending action card it gives exception such as,

{"Activity resulted into multiple skype activities"}

    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();
    }

As mentioned here in documentation :

Teams does not support SuggestedActions

So I update my code Its works now :)

    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;
        }
    }

I think this would have worked as well:

new CardAction(){ Title = "Approve", Type="ImBack", Value="Approve" },  
new CardAction(){ Title = "Decline", Type="ImBack", Value="Decline" }

according to this snippet in this example

CardAction plButton = new CardAction()
{
Value = $"https://en.wikipedia.org/wiki/{cardContent.Key}",
Type = "openUrl",
Title = "WikiPedia Page"
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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