laravel-notification-channels / microsoft-teams

Microsoft Teams Notifications Channel for Laravel
https://laravel-notification-channels.com
MIT License
132 stars 14 forks source link

Mentioning users in message & link with text #19

Open evajavor opened 2 years ago

evajavor commented 2 years ago

How do I mention users in the content/title of a message?

I tried doing: @My Name @my_email @microsoft_user_id

<at>My Name</at>
<at>my_email</at>
<at>microsoft_user_id</at>
return MicrosoftTeamsMessage::create()
            ->to(config('services.microsoft_teams.webhook_url'))
            ->type($contents[1])
            ->title($contents[0])
            ->content($contents[0], [
                [
                    'entities' => [
                        [
                            'type' => 'mention',
                            'text' => '<at>' . $this->users[0]->getFullName() . '</at>',
                            'mentioned' => [
                                'id' => $this->users[0]->email,
                                'name' => $this->users[0]->getFullName()
                            ]
                        ],
                        [
                            'type' => 'mention',
                            'text' => '<at>' . $this->users[0]->email . '</at>',
                            'mentioned' => [
                                'id' => {microsoft_user_id}',
                                'name' => $this->users[0]->getFullName()
                            ]
                        ]
                    ]
                ]
            ])

But I just can't seem to get a proper mention.

Tahiaji commented 2 years ago

MessageCard does not support mentions.

You need Adaptive Card to mention users.

evajavor commented 2 years ago

@Tahiaji your comment does not respond to my question. I know you need an adaptive card since I done the research myself but I am asking this in this package since this is the package I'm using. So if you could explain me how to do it within this package please let me know.

Tahiaji commented 2 years ago

@evajavor There is no way to achieve this using this package.

P.S. In my case, I'm now trying to create a new type of card that will be compatible with the usual "Messagecard"

To be compatible with existing implementation MicrosoftTeamsAdaptiveMessage should implement only 2 methods:

I still do not have full implementation, but the method that defines the structure may look something like this:

public function toArray(): array
{
    $card = [
        'contentType' => 'application/vnd.microsoft.card.adaptive',
        'contentUrl' => null,
        'content' => [
            '$schema' => 'http://adaptivecards.io/schemas/adaptive-card.json',
            'type' => 'AdaptiveCard',
            'version' => $this->version,
            'msteams' => [
                'width' => 'full'
            ]
        ],
    ];

    if (isset($this->body)) {
        $card['content']['body'] = $this->body;
    }
    if (isset($this->actions)) {
        $card['content']['actions'] = $this->actions;
    }
    if (isset($this->mentions)) {
        $card['content']['msteams']['entities'] = $this->mentions;
    }

    return [
        'type' => 'message',
        'attachments' => [$card],
    ];
}

And mention method

 /**
 * Add mention to user
 * Note: if text do not contain `<at>' . $name . '</at>` - 400 error returned
 * @see https://docs.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-format?tabs=adaptive-md%2Cconnector-html#user-mention-in-incoming-webhook-with-adaptive-cards
 * @param string $id
 * @param string $name
 * @return self
 */
public function mention(string $id, string $name): self
{
    $mention = [
        'type' => 'mention',
        'text' => '<at>' . $name . '</at>',
        'mentioned' => [
            'id' => $id,
            'name' => $name
        ]
    ];
    if (!isset($this->mentions)) {
        $this->mentions = [];
    }
    $this->mentions[] = $mention;
    return $this;
}
Tob0t commented 2 years ago

Hi @evajavor & @Tahiaji , thanks for your input. Since this an open-source project feel free to contribue with a PR and we will review it

Please check the contribution guidelines before: https://github.com/laravel-notification-channels/microsoft-teams/blob/master/CONTRIBUTING.md

Tahiaji commented 2 years ago

I do not have complete solution for Adaptive Messages - too many options here. And unfortunately, my current implementation already has too many differences to be fully compatible with the current package.

umer-448 commented 1 year ago

I am stuck in the same problem. I want to use @mention functionality but I can't with this package.

@evajavor @Tahiaji if you have done it then please let me know.

Tob0t commented 1 year ago

Seems like Teams is not supporting adaptive cards (yet). teams https://learn.microsoft.com/en-gb/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook#add-an-incoming-webhook-to-a-teams-channel

If you find another way feel free to contribute with a PR

Tahiaji commented 1 year ago

Seems like Teams is not supporting adaptive cards (yet).

But Teams supports.

I even send some with mentions изображение

umer-448 commented 1 year ago

@Tahiaji please share code of ac-test-hook ... currently i am using incomming webhook but @mention channel is not working for me...

My codes is :

{ "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", "size": "ExtraLarge", "weight": "Bolder", "text": "Happy Birthday Name", "wrap": true, "style": "heading" }, { "type": "TextBlock", "text": "Wishing you the best on your birthday and everything good in the year ahead.", "wrap": true }, { "type": "Image", "url": "https://birthdaycake24.com/uploads/worigin/2019/10/29/birthday-gif5db83c5a86e02_62a1f6b87b5f3a789a88ae423be83bc4.gif" } ]

    }
   }
],
 "entities": [
        {
          "type": "mention",
          "text": "<at>Testing Notification in laravel</at>",
          "mentioned": {
            "id": "2b2bd1bd-dcdd-4652-8b67-e434113c915a",
            "name": "Testing Notification in laravel"
          }
        }
      ]

}

Tahiaji commented 1 year ago

@Tahiaji please share code of ac-test-hook ...

This is not package or ready solution, but you can use it as example: https://github.com/Tahiaji/microsoft-teams-component

neowolfman commented 1 year ago

Hello, I would like to send an online message after a task, is it possible? Could you give me an example?

Note: I have not been able to send without creating a Controller, that's why my query.

Thank you

RohanRajShrestha commented 3 weeks ago

What I would suggest is instead of relying on the package(as it doesn't include the mention feature yet), just create a new channel in your Laravel application as such: `<?php

namespace App\Channels;

use App\Helpers\HandleExceptionHelper; use GuzzleHttp\Client; use Illuminate\Notifications\Notification;

class MicrosoftTeamsCustomChannel { public function send($notifiable, Notification $notification) {

    $message = $notification->toMicrosoftCustomTeams($notifiable);

    $to = $notifiable->routeNotificationFor('microsoftTeams', $notification);

    $response = $this->sendMessageToTeams($to, $message);
    return $response;
}

private function sendMessageToTeams($teamsWebhookUrl,$message)
{
    try {
        $client = new Client();
        $request = $client->request('POST', $teamsWebhookUrl, [ 'json' => $message]);
        $request->getBody()->getContents();
        return true;
    } catch (\Exception $e) {
        HandleExceptionHelper::handler($e, true);
        return false;
    }
}

}`

register it in your service provider as such: $this->app->when(MicrosoftTeamsCustomChannel::class) ->give(function ($app) { return new MicrosoftTeamsCustomChannel(); }); now you can just add this function to your notification modal public function routeNotificationForMicrosoftTeams() { // return webhook } then

return payload with mentions https://adaptivecards.io/designer/

Then just call the notifier from your desired as @umer-448 Then you can just call the notifier class,