lupuscoding/webhook-teams

A small library to create messages for Microsoft Teams

1.0.1 2021-12-21 23:07 UTC

This package is auto-updated.

Last update: 2024-09-22 05:32:55 UTC


README

A small library to create messages for Microsoft Teams.

Contents

Requirements #

  • PHP >= 7.4

Install #

composer require lupuscoding/webhook-teams

Usage #

Create a message card

// Insert uses
use LupusCoding\Webhooks\Teams\MessageCard;
use LupusCoding\Webhooks\Teams\ThemeColor;
// Create the card
$card = new MessageCard();
$card->setThemeColor(ThemeColor::SUCCESS)
    ->setSummary('My summary');

Create a message card section

use LupusCoding\Webhooks\Teams\MessageSection;
use LupusCoding\Webhooks\Teams\MessageCard;
// Create the section
$section = new MessageSection();
$section->setActivityTitle('My activity')
    ->setActivitySubtitle('This is a subtitle')
    ->setActivityImage('https://some/image.png')
    ->addFact('My fact', 'This is awesome')
    ->setMarkdown(false);
// Add section to card
$card = new MessageCard();
$card->addSection($section);

Create an action card

// Insert uses
use LupusCoding\Webhooks\Teams\ActionCard;
use LupusCoding\Webhooks\Teams\ThemeColor;
// Create the card
$card = new ActionCard();
$card->setName('my-action-name')
    ->setThemeColor(ThemeColor::DEBUG);

Create an action card input

use LupusCoding\Webhooks\Teams\ActionCard;
use LupusCoding\Webhooks\Teams\Input\TextInput;
// Create the input
$input = new TextInput();
$input->setId('input1')
    ->setTitle('Type something in')
    ->setMultiline(true)
    ;
// Add input to card
$card = new ActionCard();
$card->addInput($input);

Create an action card action

use LupusCoding\Webhooks\Teams\ActionCard;
use LupusCoding\Webhooks\Teams\CardAction\HttpPost;
// Create the action
$action = new HttpPost();
$action->setName('Click me')
    ->setTarget('http://lupuscoding.de');
// Add action to card
$card = new ActionCard();
$card->addAction($action);

Send a card

use LupusCoding\Webhooks\Teams\MessageCard;
$card = new MessageCard();
    
// Setup the hook url
$hookUrl = 'https://webhook.site/253013d5-4960-4857-85c4-596998c26e10';
// Init curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_URL, $hookUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($card));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/x-www-form-urlencoded']);
// Send request
$result = curl_exec($ch);
curl_close($ch);
// Test result
if (curl_errno($ch)) {
    // Failure
} else {
    // Success
}

Development #

  • Every contribution should respect PSR-2 and PSR-12.
  • Methods must provide argument types and return types.
  • Class properties must be typed.
  • doc blocks must only contain descriptive information.
  • doc blocks may additionally contain a type declaration for arguments or return values, if the type declaration is not precise.

For example: func(): array may not be precise if the method returns an array of arrays or objects. Consider a doc block entry like @return array[] or @return MyObject[] for clarification.

Testing #

First install phpunit by executing

composer install

Then start phpunit by executing

vendor/bin/phpunit

Further Testing (optional)

Every test request is sent to Pipedream. This site processes the requests and build valid responses. Also every request is forwarded to this Request Bin, so that you can view the request data.

To inspect the requests, you can login with your GitHub account and copy my published Webhook Workflow. Please make sure to change the publicBinUrl variable inside the workflow nodejs step, to your own Request Bin.
After creating your Pipedream Webhook Workflow, just modify the WEBHOOK_URL constant inside the MessageCardTest and ActionCardTest class.*