티스토리 뷰

문제해결

Teams webhook 으로 멘션하기

zeroco2 2024. 3. 22. 11:40

그냥 String 으로 @이름 박으면 알아서 해주는줄 알았더니 형식이 다름. 

POST 로 webhook URL로 아래 형식으로 보내야됨

url 참조

Text formatting in cards - Teams | Microsoft Learn

{
    "type": "message",
    "attachments": [
        {
        "contentType": "application/vnd.microsoft.card.adaptive",
        "content": {
            "type": "AdaptiveCard",
            "body": [
                {
                    "type": "TextBlock",
                    "size": "Medium",
                    "weight": "Bolder",
                    "text": "Sample Adaptive Card with User Mention"
                },
                {
                    "type": "TextBlock",
                    "text": "Hi <at>Adele UPN</at>, <at>Adele Microsoft Entra ID</at>"
                }
            ],
            "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
            "version": "1.0",
            "msteams": {
                "entities": [
                    {
                        "type": "mention",
                        "text": "<at>Adele UPN</at>",
                        "mentioned": {
                          "id": "AdeleV@contoso.onmicrosoft.com",
                          "name": "Adele Vance"
                        }
                      },
                      {
                        "type": "mention",
                        "text": "<at>Adele Microsoft Entra ID</at>",
                        "mentioned": {
                          "id": "87d349ed-44d7-43e1-9a83-5f2406dee5bd",
                          "name": "Adele Vance"
                        }
                      }
                ]
            }
        }
    }]
}

Spring boot 환경에서 배치로 서버 Health Check알람 보내는 상황

DTO

package com.humuson.amc.common.model.teams;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;

import java.util.List;
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
public class WeHookMentionMessage {

  @JsonProperty("type")
  private String type;

  @JsonProperty("attachments")
  private List<Attachment> attachments;

  @Getter
  @AllArgsConstructor
  @Builder
  @ToString
  public static class Attachment {
    @JsonProperty("contentType")
    private String contentType;

    @JsonProperty("content")
    private Content content;
  }

  @Getter
  @AllArgsConstructor
  @Builder
  @ToString
  public static class Content {
    @JsonProperty("type")
    private String type;

    @JsonProperty("body")
    private List<Body> body;

    @JsonProperty("$schema")
    private String schema;

    @JsonProperty("version")
    private String version;

    @JsonProperty("msteams")
    private Msteams msteams;
  }

  @Getter
  @AllArgsConstructor
  @Builder
  @ToString
  public static class Body {
    @JsonProperty("type")
    private String type;

    @JsonProperty("text")
    private String text;

    @JsonProperty("size")
    private String size;

    @JsonProperty("weight")
    private String weight;
  }

  @Getter
  @AllArgsConstructor
  @Builder
  @ToString
  public static class Msteams {
    @JsonProperty("entities")
    private List<Entity> entities;
  }

  @Getter
  @AllArgsConstructor
  @Builder
  @ToString
  public static class Entity {
    @JsonProperty("type")
    private String type;

    @JsonProperty("text")
    private String text;

    @JsonProperty("mentioned")
    private Mentioned mentioned;
  }
  @Getter
  @AllArgsConstructor
  @Builder
  @ToString
  public static class Mentioned {
    @JsonProperty("id")
    private String id;

    @JsonProperty("name")
    private String name;
  }
}

Batch Step임

  private void sendWebhookError(String responseBody) {
    List<WeHookMentionMessage.Attachment> attachments = Arrays.asList(
        WeHookMentionMessage.Attachment.builder()
            .contentType("application/vnd.microsoft.card.adaptive")
            .content(WeHookMentionMessage.Content.builder()
                .type("AdaptiveCard")
                .body(Arrays.asList(
                    WeHookMentionMessage.Body.builder()
                        .type("TextBlock")
                        .size("Medium")
                        .weight("Bolder")
                        .text("Webhook Server Check")
                        .build(),
                    WeHookMentionMessage.Body.builder()
                        .type("TextBlock")
                        .text("Webhook 서버 점검하세요. 재기동됩니다. <at>이름1</at>, <at>이름2</at>, <at>이름3</at> " + responseBody)
                        .build()
                ))
                .schema("http://adaptivecards.io/schemas/adaptive-card.json")
                .version("1.0")
                .msteams(WeHookMentionMessage.Msteams.builder()
                    .entities(Arrays.asList(
                        WeHookMentionMessage.Entity.builder()
                            .type("mention")
                            .text("<at>이름1</at>")
                            .mentioned(WeHookMentionMessage.Mentioned.builder()
                                .id("이름2@test.onmicrosoft.com")
                                .name("이름1")
                                .build())
                            .build(),
                        WeHookMentionMessage.Entity.builder()
                            .type("mention")
                            .text("<at>이름2</at>")
                            .mentioned(WeHookMentionMessage.Mentioned.builder()
                                .id("이름2@test.onmicrosoft.com")
                                .name("이름2")
                                .build())
                            .build(),
                        WeHookMentionMessage.Entity.builder()
                            .type("mention")
                            .text("<at>이름3</at>")
                            .mentioned(WeHookMentionMessage.Mentioned.builder()
                                .id("이름3@test.onmicrosoft.com")
                                .name("이름3")
                                .build())
                            .build()
                    ))
                    .build())
                .build())
            .build()
    );

    String webhookUrl = ",,,";

    WeHookMentionMessage message = WeHookMentionMessage.builder()
        .type("message")
        .attachments(attachments)
        .build();

    String webhookMessageJson = gson.toJson(message);
    teamsWebhookApiUtil.send(webhookUrl, webhookMessageJson);

    /**
     *  Webhook 서버 재시작 로직 실행
     */
      try{
        Session session = sshCommandConfig.sshSession();

		...

      } catch(Exception e){
        log.error("서버 재시작 로직 중 에러 발생... {} " , e);
      }
    }
}

대충이렇게 만듦..하드코딩으로 박아버리기