コンテンツにスキップ

Python から pymsteams でメンション付きのメッセージを送信する

以前に以下のメモを書きました。

今回は pymsteams を使い、メンション付きのメッセージを送信するサンプルをメモしておきます。

検証環境

対象 バージョン
macOS 15.1.1
Python 3.12.7
pymsteams 0.2.3

インストール

pypi からインストールします。

uv pip install pymsteams

サンプルコード

from pymsteams import connectorcard

url = "https://YOUR-URL"
user_id = "foobar@example.com"
user_display_name = "Foo Bar"

msteams = connectorcard(url)
mention_entity = {
    "type": "mention",
    "text": f"<at>{user_display_name}</at>",
    "mentioned": {"id": user_id, "name": user_display_name},
}
adaptive_card_content = {
    "type": "AdaptiveCard",
    "body": [{"type": "TextBlock", "text": f"Hello {mention_entity['text']}"}],
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "version": "1.4",
    "msteams": {"entities": [mention_entity]},
}
msteams.payload = {
    "type": "message",
    "attachments": [
        {
            "contentType": "application/vnd.microsoft.card.adaptive",
            "content": adaptive_card_content,
        }
    ],
}
msteams.send()

これで MS Teams へメンション付きのメッセージを送信することが出来ます。