heczkoa / php-ms-teams-connector
PHP 微软团队连接器
0.2.2
2021-01-23 15:03 UTC
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-09-04 18:33:42 UTC
README
一个PHP包,通过使用“入站Webhook”将通知发送到微软团队。本包的目标是创建自己的卡片,并将通知简单地发送到您希望的渠道。目前,本包支持以下格式:MessageCard、AdaptiveCard和HeroCard。
包安装 - Composer
您可以通过composer安装此包
composer require sebbmeyer/php-microsoft-teams-connector
使用方法
当您想要向您的渠道发送简单通知时,您可以轻松创建一个SimpleCard并通过TeamConnector发送它
// create connector instance $connector = new \Sebbmyr\Teams\TeamsConnector(<INCOMING_WEBHOOK_URL>); // create card $card = new \Sebbmyr\Teams\Cards\SimpleCard(['title' => 'Simple card title', 'text' => 'Simple card text']); // send card via connector $connector->send($card);
MessageCard
要发送MessageCard,您可以使用提供的CustomCard类,并向其中添加颜色、事实、图片、活动、操作或摘要。
// create a custom card $card = new \Sebbmyr\Teams\Cards\CustomCard('Package update', 'A custom card class was added to the package.'); // add information $card->setColor('01BC36') ->addFactsText('Supported PHP versions',['<= 5.4.0','7.x']) ->addFactsText('Unsupported PHP versions',['Before Version 5.4']) ->addAction('Visit Github repository','https://github.com/sebbmeyer/php-microsoft-teams-connector') ->addFacts('Facts Section',['Fact Name 1' => 'Fact Value 1','Fact Name 2' => 'Fact Value 2']);
或者,您可以创建自己的卡片来满足各种需求,只需扩展AbstractCard
类并实现getMessage()
函数。以下是一个Laravel Forge部署卡片的示例
\\ Sebbmyr\LaravelTeams\Cards\ForgeCard.php public function getMessage() { return [ "@type" => "MessageCard", "@context" => "http://schema.org/extensions", "summary" => "Forge Card", "themeColor" => ($this->data["status"] === 'success') ? self::STATUS_SUCCESS : self::STATUS_ERROR, "title" => "Forge deployment message", "sections" => [ [ "activityTitle" => "", "activitySubtitle" => "", "activityImage" => "", "facts" => [ [ "name" => "Server:", "value" => $this->data["server"]['name'] ], [ "name" => "Site", "value" => "[". $this->data["site"]["name"] ."](http://". $this->data["site"]["name"] .")" ], [ "name" => "Commit hash:", "value" => "[". $this->data["commit_hash"] ."](". $this->data["commit_url"] .")" ], [ "name" => "Commit message", "value" => $this->data["commit_message"] ] ], "text" => ($this->data["status"] === 'success') ? $this->data["commit_author"] ." deployed some fresh code!" : "Something went wrong :/" ] ] ]; }
AdaptiveCard
您几乎可以在这里找到您需要的所有元素,除了Action.Submit。因此,Input元素目前是无用的。目前,它可以通过两种方式使用:
- 传递数组数据,您可以按需设计它。数据数组可以包含以下顶级键:
body
、actions
、selectAction
、fallbackText
、backgroundImage
、minHeight
、speak
、lang
和verticalContentAlignment
。属性type
、version
和$schema
由BaseAdaptiveCard设置。
// create connector instance $connector = new \Sebbmyr\Teams\TeamsConnector(<INCOMING_WEBHOOK_URL>); // create data $data = [ "body" => [ [ "type" => "AdaptiveCards", "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);
- 使用CustomAdaptiveCard,它目前仅处理TextBlock和Image元素,以及Action.OpenUrl。CustomAdaptiveCard仍在开发中,我很快将添加缺失的卡片元素、容器和操作。
// create connector instance $connector = new \Sebbmyr\Teams\TeamsConnector(<INCOMING_WEBHOOK_URL>); // create data $textBlockA = new \Sebbmyr\Teams\Cards\Adaptive\Elements\TextBlock("Adaptive card"); $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("Supported by composer package sebbmeyer/php-microsoft-teams-connector"); $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) ; $openUrl = new \Sebbmyr\Teams\Cards\Adaptive\Actions\OpenUrl $openUrl = new \Sebbmyr\Teams\Cards\Adaptive\Actions\OpenUrl("https://github.com/sebbmeyer/php-microsoft-teams-connector"); $openUrl->setTitle("Open Github"); // create card $card = new \Sebbmyr\Teams\Cards\Adaptive\CustomAdaptiveCard(); $card->addElement($textBlockA) ->addElement($textBlockB) ->addElement($image) ->addAction($openUrl) ; // send card via connector $connector->send($card);
HeroCard
该包还支持HeroCard,其描述如下这里。注意:该卡片有一个图片属性,它是一个图片数组的类型,但只显示一张卡片。您可以像这样使用HeroCard
// create connector instance $connector = new \Sebbmyr\Teams\TeamsConnector(<INCOMING_WEBHOOK_URL>); // create card $card = new \Sebbmyr\Teams\Cards\HeroCard(); $card->setTitle("Hero Card") ->setSubtitle("Featuring Deadpool") ->addImage("https://miro.medium.com/max/3840/1*0ubYRV_WNR9iYrzUAVINHw.jpeg") ->setText("Deadpool is a fictional character appearing in American comic books published by Marvel Comics. Created by writer Fabian Nicieza and artist/writer Rob Liefeld, the character first appeared in The New Mutants #98 (cover-dated February 1991). Initially Deadpool was depicted as a supervillain when he made his first appearance in The New Mutants and later in issues of X-Force, but later evolved into his more recognizable antiheroic persona. Deadpool, whose real name is Wade Winston Wilson, is a disfigured mercenary with the superhuman ability of an accelerated healing factor and physical prowess. The character is known as the \"Merc with a Mouth\" because of his tendency to talk and joke constantly, including breaking the fourth wall for humorous effect and running gags.") ->addButton("openUrl", "Wikipedia page", "https://en.wikipedia.org/wiki/Deadpool") ; // send card via connector $connector->send($card);
许可证
此PHP微软团队连接器是开源软件,许可协议为MIT许可证