Skip to content

Instantly share code, notes, and snippets.

@marrold
Created March 26, 2025 20:27
Show Gist options
  • Save marrold/894a49df5b6e5a58a0259b4329cbab8d to your computer and use it in GitHub Desktop.
Save marrold/894a49df5b6e5a58a0259b4329cbab8d to your computer and use it in GitHub Desktop.
requiredversion 2.0
package require httpios 1.0
# -----------------------
# Initialization
# -----------------------
proc init { } {
global param
set param(interruptPrompt) true
set param(terminationKey) #
set param(abortKey) *
set param(maxDigits) 1
}
# -----------------------
# HTTP Callback
# -----------------------
proc http_callback {token} {
global dest incoming outgoing
# Retrieve the HTTP response data
set response [httpios::data $token]
set response [string map {"\n" "" "\r" ""} $response]
set code [httpios::code $token]
puts "DEBUG: HTTP status code: $code"
puts "DEBUG: HTTP response: $response"
# Determine destination from response
if {[string length $response] > 0} {
set dest $response
} else {
set dest "98150"
}
puts "DEBUG: Routing to $dest"
# Setup the outgoing leg
leg setup $dest callInfo leg_outgoing
# Clean up the token
httpios::cleanup $token
}
# -----------------------
# Setup Inbound Call and Determine Route
# -----------------------
proc act_Setup { } {
global called incoming outgoing
leg proceeding leg_incoming
leg connect leg_incoming
# Get called number (DNIS)
set called [infotag get evt_handoff dnis]
puts "DEBUG: Called number is $called"
# Make HTTP request to get destination
set token [httpios::geturl "http://10.222.0.103:8080" -command http_callback]
}
# -----------------------
# Call Setup Success
# -----------------------
proc act_RouteSuccess { } {
set incoming [infotag get leg_incoming]
set outgoing [infotag get leg_outgoing]
set status [infotag get evt_status]
puts "DEBUG: status $status"
if {$status == "ls_000"} {
puts "DEBUG: incoming $incoming outgoing $outgoing"
connection create $incoming $outgoing
fsm setstate ROUTING
} else {
puts "DEBUG: FAILED"
}
}
# -----------------------
# Call Setup Failure
# -----------------------
proc act_RouteFail { } {
puts "DEBUG: Call setup failed. Going to cleanup."
call close
}
# -----------------------
# Cleanup
# -----------------------
proc act_Cleanup { } {
puts "DEBUG: Cleanup"
leg disconnect leg_incoming
leg disconnect leg_outgoing
call close
}
# -----------------------
# FSM Definition
# -----------------------
init
# Define transitions
set MyFSM(CALL_INIT,ev_setup_indication) "act_Setup, ROUTING"
set MyFSM(CALL_INIT,ev_handoff) "act_Setup, ROUTING"
set MyFSM(ROUTING,ev_setup_done) "act_RouteSuccess, CALL_ACTIVE"
#set MyFSM(ROUTING,ev_setup_failed) "act_RouteFail, CLEANUP"
set MyFSM(CALL_ACTIVE,ev_disconnect_done) "act_Cleanup, same_state"
set MyFSM(CALL_ACTIVE,ev_disconnected) "act_Cleanup, same_state"
set MyFSM(any_state,ev_disconnect_done) "act_Cleanup, same_state"
set MyFSM(any_state,ev_media_done) "act_Cleanup, same_state"
set MyFSM(any_state,ev_disconnected) "act_Cleanup, same_state"
fsm define MyFSM CALL_INIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment