snipe / snipe-it

A free open source IT asset/license management system
https://snipeitapp.com
GNU Affero General Public License v3.0
10.81k stars 3.13k forks source link

[Feature Request]: Microsoft Teams Webhook Update #15080

Open OGLECB opened 1 month ago

OGLECB commented 1 month ago

Is your feature request related to a problem? Please describe.

Update the Microsoft Teams Webhook test (SlackSettingsForm.php) msTeamTestWebhook() to accommodate Microsoft Decommissioning of the Office 365 web connector and migrating to Power Automate workflows

Describe the solution you'd like

The format of the payload will change when they fully migrate over to Teams Workflows and so it will need to be updated to the format bellow e.g: { "attachments": [ { "contentType": "application/vnd.microsoft.card.adaptive", "contentUrl": null, "type": "AdaptiveCard", "content": { "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "type": "AdaptiveCard", "version": "1.2", "body": [ { "type": "TextBlock", "wrap": true, "text": "This is the text of a card" } ], "actions": [ { "type": "Action.OpenUrl", "title": "Github URL", "url": "https://github.com" } ] } } ] }

Describe alternatives you've considered

No response

Additional context

No response

welcome[bot] commented 1 month ago

👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

snipe commented 1 month ago

Do you know when that deadline is?

OGLECB commented 1 month ago

Hi Snipe According to the Microsoft dev blogs the deadlines are as follows:

We will gradually roll out this change in waves:

Wave 1 – effective August 15th, 2024: All new Connector creation will be blocked within all clouds Wave 2 – effective October 1st, 2024: All connectors within all clouds will stop working

snipe commented 2 weeks ago

@Godmartinz Gentle reminder that the deadline is approaching (first deadline is past) :)

mus455 commented 2 days ago

I tried modifying the function msTeamTestWebhook() as OGLECB suggested and got some partial results. It works only with "Send a message in Teams Chat or Group" and with explicitly defined content as raw body from webhook. The "AdaptiveCard" format doesnt work with my modification, it always gets stuck in "for each loop" in the MS-suggested webhook connector (works with using the raw body output as explained below). My modified function code:

public function msTeamTestWebhook()
{
    $payload = [
        "type" => "message",
        "attachments" => [
            [
                "contentType" => "application/vnd.microsoft.card.adaptive",
                "contentUrl" => null,
                "content" => [
                    "\$schema" => "http://adaptivecards.io/schemas/adaptive-card.json",
                    "type" => "AdaptiveCard",
                    "version" => "1.2",
                    "body" => [
                        [
                            "type" => "TextBlock",
                            "wrap" => true,
                            "text" => trans('general.webhook_test_msg', ['app' => $this->webhook_name])
                        ]
                    ],
                    "actions" => [
                        [
                            "type" => "Action.OpenUrl",
                            "title" => "View in ".config('app.name'),
                            "url" => config('app.url')
                        ]
                    ]
                ]
            ]
        ]
    ];

    try {
        $response = Http::withHeaders([
            'content-type' => 'application/json',
        ])->post($this->webhook_endpoint, $payload)->throw();

        if (($response->getStatusCode() == 302) || ($response->getStatusCode() == 301)) {
            return session()->flash('error', trans('admin/settings/message.webhook.error_redirect', ['endpoint' => $this->webhook_endpoint]));
        }

        $this->isDisabled = '';
        $this->save_button = trans('general.save');
        return session()->flash('success', trans('admin/settings/message.webhook.success', ['webhook_name' => $this->webhook_name]));
    } catch (\Exception $e) {
        $this->isDisabled = 'disabled';
        $this->save_button = trans('admin/settings/general.webhook_presave');
        return session()->flash('error', trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage(), 'app' => $this->webhook_name]));
    }

    return session()->flash('error', trans('admin/settings/message.webhook.error_misc'));
}

Works with the "Send a message in Teams Chat or Group" and explicitly using raw message output: slika

Using it the intended: https://make.preview.powerautomate.com/galleries/public/templates/d271a6f01c2545a28348d8f2cddf4c8f/post-to-a-channel-when-a-webhook-request-is-received way results in following error: _The execution of template action 'Foreach' failed: the result of the evaluation of 'foreach' expression '@triggerOutputs()?['body']?['attachments']' is of type 'Null'. The result must be a valid array. The snipeit test message/AdaptiveCard works as intended.

I not so good at php but i hope it helps in some way.

Microsoft Power Automate
OGLECB commented 2 days ago

Hi @mus455, This looks like a good start for a implementation, I have been working on some other projects with this similar functionality but they have moved the body[] array outside of the content[] array. For Example return [ "type" => "message", "attachments" => [ [ "contentType" => "application/vnd.microsoft.card.adaptive", "content" => [ "\$schema" => "http://adaptivecards.io/schemas/adaptive-card.json", "type" => "AdaptiveCard", "version" => "1.2", "msteams" => [ "width" => "Full" ], "body" => $body ] ] ] ];

This might be the reason your implementation is having some issues with your foreach error, Not sure if you have tried this format?

mus455 commented 1 day ago

@OGLECB thanks for suggesting this, but i get the same error with your example

OGLECB commented 1 day ago

Hi @mus455,

I have just tested your original code, With the following set up in power automate: image and I have had no error with the updated function you have provided.

@snipe correct me if I am wrong but snipe uses the https://github.com/laravel-notification-channels/microsoft-teams driver to send the notifications to the webhook, This repo has not been updated to the new " Adaptive Card Template" because the repo owner has not been around allow for a pull request to update the repo.

@mus455 do you get the error when you try and request an asset as this uses the other repository rather than the snipe provided function?

Kind regards Ollie

mus455 commented 1 day ago

@OGLECB the test message works fine. i get the error when i checkout an asset.

OGLECB commented 22 hours ago

@mus455 The checkout notification is sent using the repo -> https://github.com/laravel-notification-channels/microsoft-teams which has not been updated to use the Adaptive card Template, so that is why you are getting the error when checking out a asset

GitHub
GitHub - laravel-notification-channels/microsoft-teams: Microsoft Teams Notifications Channel for Laravel
Microsoft Teams Notifications Channel for Laravel. Contribute to laravel-notification-channels/microsoft-teams development by creating an account on GitHub.