microsoft-teams icon indicating copy to clipboard operation
microsoft-teams copied to clipboard

Mentioning users in message & link with text

Open evajavor opened this issue 3 years ago • 12 comments

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.

evajavor avatar May 03 '22 09:05 evajavor

MessageCard does not support mentions.

You need Adaptive Card to mention users.

Tahiaji avatar Jun 02 '22 11:06 Tahiaji

@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.

evajavor avatar Jun 03 '22 07:06 evajavor

@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:

  • getWebhookUrl()
  • toArray()

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;
}

Tahiaji avatar Jun 03 '22 09:06 Tahiaji

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

Tob0t avatar Jun 03 '22 10:06 Tob0t

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.

Tahiaji avatar Jun 03 '22 16:06 Tahiaji

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.

umer-448 avatar Oct 04 '22 08:10 umer-448

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

Tob0t avatar Oct 07 '22 07:10 Tob0t

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

But Teams supports.

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

Tahiaji avatar Oct 07 '22 08:10 Tahiaji

@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"
          }
        }
      ]

}

umer-448 avatar Oct 26 '22 10:10 umer-448

@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

Tahiaji avatar Nov 06 '22 09:11 Tahiaji

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

neowolfman avatar Nov 07 '22 05:11 neowolfman

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,

RohanRajShrestha avatar Jun 14 '24 11:06 RohanRajShrestha

Closed due to inactivity

Tob0t avatar Aug 31 '24 15:08 Tob0t