Scala: AkkaHttp로 Microsoft Teams에 메시지 보내기

1. 소개


  • Temas 앱에서 webhook용 URI를 가져옵니다.
  • 메세지를 송신하고 싶은 채널로, 「・・・」→커넥터→Incoming Webhook→구성→작성을 실시해, 표시된 URI(①)를 삼가합니다.


  • 2. Scala 프로젝트 만들기


    sbt new sbt/scala-seed.g8
    

    3. 종속성 설정



    1) Dependencies.scala



    project/Dependencies.scala
    
    object Dependencies {
      lazy val akkaActor= "com.typesafe.akka" %% "akka-actor" % "2.6.3"
      lazy val akkaHttp= "com.typesafe.akka" %% "akka-http" % "10.1.11"
      lazy val akkaStream="com.typesafe.akka" %% "akka-stream" % "2.6.3"
    }
    

    2) build.sbt



    build.sbt
    lazy val root = (project in file("."))
      .settings(
        name := "teamsBotSample",
        libraryDependencies ++= Seq(
          akkaActor,
          akkaHttp,
          akkaStream
        ) 
    

    3. 구현



    1) import



    src/main/scala/Main.scala
    
    import akka.actor.ActorSystem
    import akka.http.scaladsl.Http
    import akka.http.scaladsl.model._
    import scala.concurrent.Future
    import scala.concurrent.ExecutionContext.Implicits.global
    import scala.util.{ Failure, Success }
    

    2) TeamsBotClient 클래스



    src/main/scala/Main.scala
    
    //TeamsBotClientクラス
    class TeamsBotClient (uri:String,implicit val system:ActorSystem){
      def request(message:String):Future[HttpResponse]={
        Http().singleRequest(
          HttpRequest(
            method = HttpMethods.POST,
            uri = uri
          ).withEntity(
            HttpEntity(
              ContentTypes.`application/json`,
              s"""{"text":"${message}"}"""
            )
          )
        )
      }
    }
    

    3) Main



    src/main/scala/Main.scala
    
    object Main extends App {
    
      /* Actorを起動 */
      val system = ActorSystem()
    
      /* ①で控えたwebhookのURI */
      val uri = "https://outlook.office.com/webhook/~~~~~~~~~~~~~~~~~~~~~~" 
    
      /* TeamsBotClientインスタンスを作成 */
      val teamsBotClient:TeamsBotClient = new TeamsBotClient(uri,system)
    
      /* 送信メッセージ */
      val message="はろーわーるど"
    
      /* メッセージを送信 */
      teamsBotClient.request(message).andThen {
          case Success(res) => {
            val httpStatus=res.status.intValue()
            httpStatus match{
              case x if x==200  =>{
                println("%s:request succeeded".format(httpStatus))
              }
              case _ => {
                println("%s:request error".format(httpStatus))
              }
            }
          }
          case Failure(t) => {
            t.printStackTrace()
          }
        }.andThen{
          case _ => system.terminate
        }
    }
    

    4. 송신 결과





    5. 기타



    powerwhell의 경우


  • 파일의 문자 코드는 ShiftJIS

  • teamsBotTest.ps1
    $uri = "https://outlook.office.com/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    $mes="はろー わーるど"
    $body = ConvertTo-JSON @{text = $mes}
    $body = [Text.Encoding]::UTF8.GetBytes($body)
    Invoke-RestMethod -uri $uri -Method Post -body $body -ContentType 'application/json
    

    이상.

    좋은 웹페이지 즐겨찾기