Skip to content

Instantly share code, notes, and snippets.

@gjesse
Created April 28, 2022 19:39
Show Gist options
  • Save gjesse/e62cd859358ddcfc19d555d8f0ea1a74 to your computer and use it in GitHub Desktop.
Save gjesse/e62cd859358ddcfc19d555d8f0ea1a74 to your computer and use it in GitHub Desktop.
import java.time.Duration
class CallActionBuilder {
val actions: MutableList<CallAction> = mutableListOf()
fun answer() {
actions.add(AgentAnswer())
}
fun pause(seconds: Long = 1) {
actions.add(Pause(Duration.ofSeconds(seconds)))
}
fun agentSay(utterance: String) {
actions.add(AgentSay(utterance))
}
fun userSay(utterance: String) {
actions.add(TwilioSay(utterance))
}
fun buildTwilioActions(): List<CallAction> {
return actions.map {
if (it is AllAction || it is TwilioAction ) {
it
} else {
it.toAllAction()
}
}
}
fun buildAgentSpeechActions(): List<CallAction> {
return actions.map {
if (it is AllAction || it is AgentSpeechAction ) {
it
} else {
it.toAllAction()
}
}
}
fun buildAgentUiActions(): List<CallAction> {
return actions.map {
if (it is AllAction || it is AgentUiAction ) {
it
} else {
it.toAllAction()
}
}
}
}
sealed interface CallAction {
fun length(): Duration
fun toAllAction() = Pause(length())
}
interface AllAction: CallAction
interface TwilioAction : CallAction
interface AgentSpeechAction : CallAction
interface AgentUiAction : CallAction
class TwilioSay(val utterance: String) : TwilioAction {
override fun length(): Duration {
// todo figure out how long these should be
return Duration.ofSeconds(1)
}
}
class AgentSay(val utterance: String) : AgentSpeechAction {
override fun length(): Duration {
// todo figure out how long these should be
return Duration.ofSeconds(1)
}
}
class AgentAnswer : AgentUiAction {
override fun length(): Duration {
return Duration.ofMillis(100)
}
}
class Pause(private val duration: Duration) : AllAction {
override fun length() = duration
}
fun call(work: CallActionBuilder.() -> Unit): CallActionBuilder {
val builder = CallActionBuilder()
builder.work()
return builder
}
val callActionBuilder = call {
answer()
pause(3)
userSay(
"hi it's me, Jesse"
)
pause(2)
agentSay(
"hey, jesse, how can i help you"
)
pause(3)
}
println("twilio")
callActionBuilder.buildTwilioActions().forEach{
println ("do ${it::class.simpleName} for ${it.length()}")
}
println("")
println("ui")
callActionBuilder.buildAgentUiActions().forEach{
println ("do ${it::class.simpleName} for ${it.length()}")
}
println("")
println("agent speech")
callActionBuilder.buildAgentSpeechActions().forEach{
println ("do ${it::class.simpleName} for ${it.length()}")
}
println("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment