io.github.nafg.dialoguestate.Menu.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core_3 Show documentation
Show all versions of core_3 Show documentation
Define a Twilio app in terms of a sane call tree
The newest version!
package io.github.nafg.dialoguestate
import zio.ZIO
import zio.prelude.NonEmptyList
//noinspection ScalaUnusedSymbol
object Menu {
def apply[A: ToText](title: CallTree.NoContinuation, choices: NonEmptyList[A], preposition: String = "for")(
handler: A => CallTree.Callback
): CallTree.Gather = {
val withNums = LazyList.from(1).map(_.toString).zip(choices.toCons)
lazy val withNumsMap = withNums.toMap
val prompts = title +: withNums.flatMap { case (n, d) =>
Seq(CallTree.Say(s"Press $n $preposition"), CallTree.Say(d), CallTree.Pause())
}
CallTree.Gather(finishOnKey = None, actionOnEmptyResult = true)(prompts*) {
case "" => ZIO.fail(Left("Please make a selection"))
case withNumsMap(a) => handler(a)
case _ => ZIO.fail(Left("That is not one of the choices"))
}
}
def yesNo(title: CallTree.NoContinuation)(handler: Boolean => CallTree.Callback): CallTree.Gather =
Menu[Boolean](title, NonEmptyList(true, false))(handler)
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy