This package is designed for sending messages to Microsoft Teams (MS Teams) channels using webhooks (incoming webhook or connector). For more information about sending messages to MS Teams channels using web hooks see https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL.
composer require rocketfellows/ms-teams-webhook-message-sender
Current implementation dependencies:
- guzzle client - https://github.com/guzzle/guzzle - using for http request to webhook.
rocketfellows\MSTeamsWebhookMessageSender\configs\Connector
- a class that encapsulates the connection configuration for sending a message.
Class description:
incomingWebhookUrl
- string - link to a webhook (connector) for sending a message;create
- static function - static factory function that returns a value of typeConnector
;getIncomingWebhookUrl
- function - getter that returns the value of theincomingWebhookUrl
attribute.
rocketfellows\MSTeamsWebhookMessageSender\models\Message
- a class that encapsulates message data to be sent via a webhook and implements the JsonSerializable
interface.
Class description:
text
- string - message text to send;title
- string | null - message title to send;create
- static function - static factory function returning a value of typeMessage
;convertToJson
- function - function, returns a representation of aMessage
type value as a json string;jsonSerialize
- function - implementation of theJsonSerializable
interface;getText
- function - getter returningtext
attribute value;getTitle
- function - getter returningtitle
attribute value.
rocketfellows\MSTeamsWebhookMessageSender\MSTeamsWebhookMessageSenderInterface
- interface for sending a message via a webhook uses a Connector
type value as a connection, and a Message
type value as a message.
public function sendMessage(Connector $connector, Message $message): void;
Interface exceptions:
EmptyIncomingWebhookUrlException
- thrown if the link to the webhook is an empty string.InvalidIncomingWebhookUrlException
- thrown if the link to the webhook is not valid (for example, the link is not an url).EmptyMessageException
- thrown if the message text is an empty string.ConnectorException
- thrown if an error occurred when sending a message (for example, if the HTTP response code is not 200).
Send message with title:
$sender->sendMessage(Connector::create(INCOMING_WEBHOOK_URL), Message::create('Hello world!', 'Hello!'));
Result:
Send message without title:
$sender->sendMessage(Connector::create(INCOMING_WEBHOOK_URL), Message::create('Hello world!'));
Result:
rocketfellows\MSTeamsWebhookMessageSender\MSTeamsWebhookTextSenderInterface
- interface for sending a message via a webhook uses a value of type Connector
as a connection, and a value of type string (message text) as a message.
public function sendText(Connector $connector, string $text): void;
Interface exceptions:
EmptyIncomingWebhookUrlException
- thrown if the link to the webhook is an empty string.InvalidIncomingWebhookUrlException
- thrown if the link to the webhook is not valid (for example, the link is not an url).EmptyMessageException
- thrown if the message text is an empty string.ConnectorException
- thrown if an error occurred when sending a message (for example, if the HTTP response code is not 200).
Send text:
$sender->sendText(Connector::create(INCOMING_WEBHOOK_URL), 'Hello world!');
Result:
rocketfellows\MSTeamsWebhookMessageSender\MSTeamsWebhookArrayMessageSenderInterface
- interface for sending a message via a webhook uses a Connector
type value as a connection, and an array type value as a message.
public function sendMessageFromArray(Connector $connector, array $messageData): void;
Interface exceptions:
EmptyIncomingWebhookUrlException
- thrown if the link to the webhook is an empty string.InvalidIncomingWebhookUrlException
- thrown if the link to the webhook is not valid (for example, the link is not an url).EmptyMessageDataException
- thrown if the array with message data is empty.ConnectorException
- thrown if an error occurred when sending a message (for example, if the HTTP response code is not 200).
Send message with title:
$sender->sendMessageFromArray(
Connector::create(INCOMING_WEBHOOK_URL),
[
'text' => 'Hello world!',
'title' => 'Hello!',
]
);
Result:
Send message without title:
$sender->sendMessageFromArray(
Connector::create(INCOMING_WEBHOOK_URL),
[
'text' => 'Hello world!',
]
);
Result:
Send message with section:
$sender->sendMessageFromArray(
Connector::create(INCOMING_WEBHOOK_URL),
[
'text' => 'Hello world!',
'sections' => [
[
"activityTitle" => "Larry Bryant created a new task",
"activitySubtitle" => "On Project Tango",
"activityImage" => "https://adaptivecards.io/content/cats/3.png",
"facts" => [
[
"name" => "Assigned to",
"value" => "Unassigned",
],
[
"name" => "Due date",
"value" => "Mon May 01 2017 17:07:18 GMT-0700 (Pacific Daylight Time)"
],
[
"name" => "Status",
"value" => "Not started"
]
],
"markdown" => true,
]
],
]
);
Result:
rocketfellows\MSTeamsWebhookMessageSender\MSTeamsWebhookJsonMessageSenderInterface
- interface for sending a message via a webhook uses a value of type Connector
as a connection, and a value of type string (a json string with message data) as a message.
public function sendJsonMessage(Connector $connector, string $jsonMessage): void;
Interface exceptions:
EmptyIncomingWebhookUrlException
- thrown if the link to the webhook is an empty string.InvalidIncomingWebhookUrlException
- thrown if the link to the webhook is not valid (for example, the link is not an url).EmptyMessageDataException
- thrown if the array with message data is empty.InvalidJsonMessageException
- thrown if the json string with the message data is not valid (not valid from the point of view of the json format).ConnectorException
- thrown if an error occurred when sending a message (for example, if the HTTP response code is not 200).
Send message with title:
$sender->sendJsonMessage(
Connector::create(INCOMING_WEBHOOK_URL),
'{"text": "Hello world!", "title": "Hello!"}'
);
Result:
Send message without title:
$sender->sendJsonMessage(
Connector::create(INCOMING_WEBHOOK_URL),
'{"text": "Hello world!"}'
);
Result:
Send message with sections:
$sender->sendJsonMessage(
Connector::create(INCOMING_WEBHOOK_URL),
'{
"text": "Hello world!",
"sections": [
{
"activityTitle": "Larry Bryant created a new task",
"activitySubtitle": "On Project Tango",
"activityImage": "https://adaptivecards.io/content/cats/3.png",
"facts": [
{
"name": "Assigned to",
"value": "Unassigned"
},
{
"name": "Due date",
"value": "Mon May 01 2017 17:07:18 GMT-0700 (Pacific Daylight Time)"
},
{
"name": "Status",
"value": "Not started"
}
],
"markdown": true
}
]
}'
);
Result:
rocketfellows\MSTeamsWebhookMessageSender\senders\MSTeamsWebhookMessageSender
- service for sending messages via a webhook.
Sender implements the following interfaces:
rocketfellows\MSTeamsWebhookMessageSender\MSTeamsWebhookMessageSenderInterface
;rocketfellows\MSTeamsWebhookMessageSender\MSTeamsWebhookTextSenderInterface
;rocketfellows\MSTeamsWebhookMessageSender\MSTeamsWebhookArrayMessageSenderInterface
;rocketfellows\MSTeamsWebhookMessageSender\MSTeamsWebhookJsonMessageSenderInterface
.
GuzzleHttp Client
is used to send a request with a message.
Sending a Message
without a title:
$sender = new MSTeamsWebhookMessageSender(new \GuzzleHttp\Client());
$sender->sendMessage(Connector::create(INCOMING_WEBHOOK_URL), Message::create('Hello world!'));
Result:
Sending a Message
with a title:
$sender = new MSTeamsWebhookMessageSender(new \GuzzleHttp\Client());
$sender->sendMessage(
Connector::create(INCOMING_WEBHOOK_URL),
Message::create('Hello world!', 'Hello!')
);
Result:
Sending a message as a string:
$sender = new MSTeamsWebhookMessageSender(new \GuzzleHttp\Client());
$sender->sendText(
Connector::create(INCOMING_WEBHOOK_URL),
'Hello world!'
);
Result:
Sending a message as an array:
$sender = new MSTeamsWebhookMessageSender(new \GuzzleHttp\Client());
$sender->sendMessageFromArray(
Connector::create(INCOMING_WEBHOOK_URL),
[
'title' => 'Message title',
'text' => 'Hello world!',
]
);
Result:
Sending a message as a json string:
$sender = new MSTeamsWebhookMessageSender(new \GuzzleHttp\Client());
$sender->sendJsonMessage(
Connector::create(INCOMING_WEBHOOK_URL),
'{"title": "Message title", "text": "Hello world!"}'
);
Result:
Welcome to pull requests. If there is a major changes, first please open an issue for discussion.
Please make sure to update tests as appropriate.