0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ターミナルのコマンドからTeamsのWebhookに投げる

Last updated at Posted at 2025-09-22

はじめに

LambdaからTeamsにメッセージを発行したい!となった時に、AdaptiveCardの内容などがどういう表示になるか、そもそもちゃんと送れるのかなどを試したくなると思います。
ターミナルから送信できて確認できれば便利だよねと思ったのでメモします。

#!/bin/bash
WEBHOOK_URL="<ワークフローで作成・発行されたURL>"

curl -X POST "${WEBHOOK_URL}" \
  -H "Content-Type: application/json" \
  -d '{
    "attachments": [
      {
        "contentType": "application/vnd.microsoft.card.adaptive",
        "content": {
          "type": "AdaptiveCard",
          "$schema": "https://adaptivecards.io/schemas/adaptive-card.json",
          "version": "1.5",
          "body": [
            {
              "type": "TextBlock",
              "weight": "Bolder",
              "text": "✅ ターミナルからの送信テスト",
              "wrap": true,
              "size": "ExtraLarge",
              "color": "Good"
            }
          ]
        }
      }
    ]
  }' \
  -v

実行すると HTTP/1.1 202 Accepted が帰ってきます。レスポンスないんだね。

参考までに

Lambdaで書くとこうなる

import os
import json
import urllib.request
from typing import Dict, Any

WEBHOOK_URL = os.getenv("WEBHOOK_URL")

def get_payload() -> Dict[str, Any]:
    """Teams通知用のペイロード"""
    return {
        "attachments": [
            {
                "contentType": "application/vnd.microsoft.card.adaptive",
                "content": {
                    "type": "AdaptiveCard",
                    "$schema": "https://adaptivecards.io/schemas/adaptive-card.json",
                    "version": "1.5",
                    "body": [
                        {
                            "type": "TextBlock",
                            "weight": "Bolder",
                            "text": "✅ Lambdaからの送信テスト",
                            "wrap": True,
                            "size": "ExtraLarge",
                            "color": "Good"
                        }
                    ]
                }
            }
        ]
    }

def send_webhook(payload: Dict[str, Any]) -> None:
    """Webhookを送信"""
    if not WEBHOOK_URL:
        return
    
    headers = {"Content-Type": "application/json"}
    data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
    
    try:
        req = urllib.request.Request(WEBHOOK_URL, data=data, headers=headers)
        with urllib.request.urlopen(req, timeout=10) as resp:
            status_code = resp.getcode()
            print(f"送信完了: ステータス {status_code}")
    except Exception as e:
        print(f"送信エラー: {e}")

def handler(event, context):
    """Lambdaハンドラ"""
    send_webhook(get_payload())
    return {"statusCode": 200, "body": "送信処理完了"}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?