shahil / msgraph
A Laravel package for Microsoft Graph API Teams Message integration.
Requires
- php: >=8.0
- illuminate/support: ^9.0|^10.0|^11.0
README
shahil/ms-graph is a Laravel package that simplifies the integration with Microsoft's Graph API, enabling you to send messages, manage chats, send group messages, and utilize Adaptive Cards in your Laravel application effortlessly.
Features
- Fetch and manage OAuth tokens for Microsoft Graph API.
- Retrieve user details using email addresses.
- Create one-on-one or group chats dynamically.
- Send text messages or Adaptive Cards to users or groups via Microsoft Teams.
Installation
Requirements
- PHP 8.0+
- Laravel 8.0+
- A registered Azure AD application with required permissions for Microsoft Graph API.
Steps
-
Install the package via Composer:
composer require shahil/msgraph
-
Publish the configuration file:
php artisan vendor:publish --tag=msgraph-config
-
Update the
.env
file with your Azure AD application credentials:MICROSOFT_CLIENT_ID=your_client_id MICROSOFT_CLIENT_SECRET=your_client_secret MICROSOFT_TENANT_ID=your_tenant_id MICROSOFT_SCOPE=https://graph.microsoft.com/.default MICROSOFT_USERNAME=your_admin_email@example.com MICROSOFT_PASSWORD=your_admin_password
Configuration
The package provides a configuration file (config/msgraph.php
) for customizing default values:
return [ 'client_id' => env('MICROSOFT_CLIENT_ID'), 'client_secret' => env('MICROSOFT_CLIENT_SECRET'), 'tenant_id' => env('MICROSOFT_TENANT_ID'), 'scope' => env('MICROSOFT_SCOPE'), 'username' => env('MICROSOFT_USERNAME'), 'password' => env('MICROSOFT_PASSWORD'), ];
Usage
-
Retrieve User ID by Email
use Shahil\MSGraph\Facades\MSGraph; $userId = MSGraph::getUserId('user@example.com');
-
Send a Simple Message
use Shahil\MSGraph\Facades\MSGraph; MSGraph::sendMessageToUser('user@example.com', 'Hello from Laravel!');
-
Send a Message to a Group
use Shahil\MSGraph\Facades\MSGraph; // Retrieve the group chat ID by its name $chatId = MSGraph::getChatIdByGroupName('Your Group Name'); // Send a message to the group MSGraph::sendMessageToGroup($chatId, 'Hello group members!');
-
Send an Adaptive Card
use Shahil\MSGraph\Facades\MSGraph; $adaptiveCardPayload = [ 'type' => 'AdaptiveCard', 'version' => '1.4', 'body' => [ ['type' => 'TextBlock', 'text' => 'Welcome to MS Graph Integration!'] ], 'actions' => [ ['type' => 'Action.OpenUrl', 'title' => 'Learn More', 'url' => 'https://learn.microsoft.com/graph/'] ] ]; MSGraph::sendAdaptiveCardToUser('user@example.com', $adaptiveCardPayload);
-
Get Chat ID for Group by Name
use Shahil\MSGraph\Facades\MSGraph; $chatId = MSGraph::getChatIdByGroupName('Your Group Name');
-
Get Chat ID for One-on-One Chat
use Shahil\MSGraph\Facades\MSGraph; $targetUserId = MSGraph::getUserId('user@example.com'); $chatId = MSGraph::getChatId($targetUserId);
Examples in Controllers
Here’s an example of sending a group message inside a controller:
namespace App\Http\Controllers; use Shahil\MSGraph\Facades\MSGraph; class TeamsController extends Controller { public function sendGroupMessage() { try { $chatId = MSGraph::getChatIdByGroupName('Your Group Name'); MSGraph::sendMessageToGroup($chatId, 'Hello, team!'); return response()->json(['success' => 'Message sent to the group successfully.']); } catch (\Exception $e) { return response()->json(['error' => $e->getMessage()]); } } }
Testing
Run unit tests for the package:
php artisan test
Contributing
Contributions are welcome! Please fork this repository, create a feature branch, and submit a pull request.
License
This package is open-sourced software licensed under the MIT license.
Contact
For issues, questions, or feedback, please contact Shahil via GitHub or open an issue in this repository.