sebbmeyer / php-microsoft-teams-connector

PHP Microsoft Teams Connector
MIT License
109 stars 21 forks source link

AdaptiveCard support #16

Open kategray opened 3 years ago

kategray commented 3 years ago

Hello,

Microsoft has added AdaptiveCard support to incoming webhooks.

https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using

sebbmeyer commented 3 years ago

I'll try to look into it at the weekend, but finally adaptive cards :)

sebbmeyer commented 3 years ago

I started to work on integrating adaptive cards. It can be tested by checking out the feature branch feature/adaptive-cards. Currently it can be used in two ways:

1) Passing data as an array, you can design how you want

// create connector instance
$connector = new \Sebbmyr\Teams\TeamsConnector(<INCOMING_WEBHOOK_URL>);
// create data
$data = [
    "body" => [
        [
            "type" =>  "TextBlock",
            "text" =>  "Adaptive card test. For Samples and Templates, see https://adaptivecards.io/samples](https://adaptivecards.io/samples)",
        ],
    ],
];
// create card
$card  = new \Sebbmyr\Teams\Cards\Adaptive\BaseAdaptiveCard($data);
// send card via connector
$connector->send($card);

2) Using the CustomAdaptiveCard which currently only handles TextBlock and Image elements

// create connector instance
$connector = new \Sebbmyr\Teams\TeamsConnector(<INCOMING_WEBHOOK_URL>);
// create data
$textBlockA = new  \Sebbmyr\Teams\Cards\Adaptive\Elements\TextBlock("Adaptive card test");
$textBlockA->setColor( \Sebbmyr\Teams\Cards\Adaptive\Styles::COLORS_WARNING)
    ->setSize( \Sebbmyr\Teams\Cards\Adaptive\Styles::FONT_SIZE_LARGE)
;
$textBlockB = new  \Sebbmyr\Teams\Cards\Adaptive\Elements\TextBlock("Second text block element");
$textBlockB->setIsSubtle(true);
$image = new  \Sebbmyr\Teams\Cards\Adaptive\Elements\Image("https://adaptivecards.io/content/cats/1.png");
$image->setHorizontalAligment( \Sebbmyr\Teams\Cards\Adaptive\Styles::HORIZONTAL_ALIGNMENT_CENTER)
    ->setSize( \Sebbmyr\Teams\Cards\Adaptive\Styles::IMAGE_SIZE_MEDIUM)
;
// create card
$card = new \Sebbmyr\Teams\Cards\Adaptive\CustomAdaptiveCard();
$card->addElement($textBlockA)
    ->addElement($textBlockB)
    ->addElement($image)
;
// send card via connector
$connector->send($card);

The namespaces can changed while I'm working on this feature, so be cautious if you use it in production