Scala: AkkaHttp로 Microsoft Teams에 메시지 보내기
10518 단어 Akka-HTTPScalaPowerShell팀
1. 소개
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.sbtlazy 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의 경우
sbt new sbt/scala-seed.g8
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의 경우
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 }
//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}"}"""
)
)
)
}
}
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
}
}
5. 기타
powerwhell의 경우
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
이상.
Reference
이 문제에 관하여(Scala: AkkaHttp로 Microsoft Teams에 메시지 보내기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ytayta/items/bd19018f96189d8ab3ce텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)