Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft Adaptive Cards #1

Merged
merged 4 commits into from
Feb 27, 2025
Merged

Microsoft Adaptive Cards #1

merged 4 commits into from
Feb 27, 2025

Conversation

johnatricorocks
Copy link
Collaborator

@johnatricorocks johnatricorocks commented Feb 17, 2025

The original https://github.com/laravel-notification-channels/microsoft-teams package is an out of date package.
With the connectors being deprecated. Issue = laravel-notification-channels#30
There is a suggested fix with issue: laravel-notification-channels#31
This fix is out of date with it being version 1.2 and when tried and tested doesn't actually work.

This PR fixes the issue with a new Microsoft Adaptive Card Class. The Adaptive Cards replace the messages.
This PR includes Text Blocks, Icon Block, FactSets and Action Url Button with the ability to customise them too.

The blocks are based on the components from https://dev.teams.microsoft.com/cards/new?cardName=test

To test this out, I created a Laravel 10 test project.

Added this to composer.json

    "repositories": [
        {
            "type": "path",
            "url": "path to microsoft teams package"
        }
    ],

set "minimum-stability": "dev", in composer.json
and set in composer.json

"require": {
       "laravel-notification-channels/microsoft-teams" : "*"
   },

In the .env I set TEAMS_WEBHOOK_URL=

Then in services.php I set

 'microsoft_teams' => [
        'webhook_url' => env('TEAMS_WEBHOOK_URL'),
    ],

Then in a controller action I set

  try {
            Notification::route('microsoft-teams', null)
                ->notify(new \App\Notifications\TestNotification());
            return 'Notification sent successfully';
        } catch (\Exception $e) {
            return 'Error: ' . $e->getMessage();
        }

and create a notification to be run from the controller action. Notification class =

class TestNotification extends Notification
{
    use Queueable;

    public function via($notifiable)
    {
        Log::info('Notification via method called');
        return [MicrosoftTeamsChannel::class];
    }

    public function toMicrosoftTeams($notifiable)
    {
        try {
            $message = MicrosoftTeamsAdaptiveCard::create()
                ->to(config('services.microsoft_teams.webhook_url'))
                ->cardTitle('Test alert')
                ->cardContent([
                    TextBlock::create()
                        ->setText('This is a text block text with extra large text')
                        ->setFontType('MonoSpace')
                        ->setWeight('Bolder')
                        ->setSize('ExtraLarge')
                        ->setSpacing('ExtraLarge')
                        ->setStyle('Heading')
                        ->setColor('Light')
                        ->setHorizontalAlignment('Center')
                        ->setSeparator(true),
                    Icon::create()
                        ->setName('Alert')
                        ->setColor('Good')
                        ->setHorizontalAlignment('Center')
                        ->setStyle('Regular')
                        ->setSize('xxLarge'),
                    FactSet::create()
                        ->setSpacing('ExtraLarge')
                        ->setSeparator(true)
                        ->setFacts([
                            Fact::create()->setTitle('Fact 1')->setValue('Fact Fact'),
                            Fact::create()->setTitle('Fact 2')->setValue('Fact Fact'),
                        ])
                ])
                ->cardActions([
                    ActionOpenUrl::create()
                        ->setMode('Primary')
                        ->setStyle('Positive')
                        ->setTitle('View Website')
                        ->setUrl("https://test.com"),
                ]);

            return $message;
        } catch (\Exception $e) {
            throw $e;
        }
    }
}

@johnatricorocks
Copy link
Collaborator Author

Should we completely remove the Microsoft Messages too as it's not used anymore?

@johnatricorocks
Copy link
Collaborator Author

Should we keep the enums I added too? I was thinking it would be good to know what values are allowed for each property to minimise errors thrown by wrong types/naming. There maybe a better way to implement the allowed property types then enums?

@nedwors
Copy link

nedwors commented Feb 18, 2025

Should we keep the enums I added too? I was thinking it would be good to know what values are allowed for each property to minimise errors thrown by wrong types/naming. There maybe a better way to implement the allowed property types then enums?

The package must support PHP >7.2, so we won't be able to use Enums:

CleanShot 2025-02-18 at 11 28 12

Should we completely remove the Microsoft Messages too as it's not used anymore?

If it's not used anymore, then yes.

*
* @return array
*/
public function toArray() : array

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we update our toArray methods in all of these classes to be less dynamic:

return [
    'facts' => $this->facts->toArray(),
    ...
];

We can leave the MicrosoftTeamsAdaptiveCard as is because it follows the project pattern of using payload

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated methods

@johnatricorocks johnatricorocks merged commit 28006f1 into master Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants