Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Created November 11, 2010 00:54
Show Gist options
  • Save rsbohn/671795 to your computer and use it in GitHub Desktop.
Save rsbohn/671795 to your computer and use it in GitHub Desktop.
Generates a Twilio app rulset for Kynetx
# you need a Kynetx developer account
# and you have to create a Kynetx ruleset
# your hook (for your Twilio phone number) will be:
# http://webhooks.kynetx.com:3098/t/--appID--/callstart
# (replace --appID-- with your app ID such as a8x16
# - or a8x16.dev to use the latest dev version of the ruleset)
#
# 2010-11-22 (rsbohn) Now with YAML!
# -- create kilter.yml in the same directory, then run kilter.rb
# -- sample YAML at the bottom of this file!
#
# by rsbohn
# released with BSD license http://www.opensource.org/licenses/bsd-license.php
# Randall Bohn 2010
require 'yaml'
begin
@yml = YAML::load_file "kilter.yml"
rescue
@yml = YAML::load DATA
end
def ruleset()
puts start_rule
puts meta
puts dispatch
puts global
puts greeting
puts givemenu
puts options
puts signoff
puts end_rule
end
def start_rule()
"ruleset ________ {"
end
def end_rule()
"// generated by Kilter 2010\n}\n"
end
def meta()
<<EOF
meta {
name "#{@yml[:name]}"
description <<
#{@yml[:description]}
>>
author "#{@yml[:author]}"
logging off
}
EOF
end
def dispatch()
"dispatch {\n\n}\n"
end
def global()
"global {\n"+fixed+option_strings+"}\n"
end
def fixed()
<<EOF
hook_event = "callstart"
greeting = "#{@yml[:greeting]}"
menu = "#{@yml[:menu]}"
error = "#{@yml[:error]}"
goodbye = "#{@yml[:goodbye]}"
EOF
end
def option_strings()
opts = @yml[:options]
rv = [1,2,3,4,5,6,7,8,9].collect { |n|
"\toption#{n}=\"#{opts[n]}\"" if opts[n] }
rv.join "\n"
end
def greeting()
<<EOF
rule greeting is active {
select when twilio callstart
twilio:say(greeting)
fired { raise explicit event givemenu }
}
EOF
end
def givemenu()
<<EOF
rule givemenu is active {
select when explicit givemenu
{
twilio:gather_start("keypress") with numDigits = "1"
and timeout = "12";
twilio:say(menu);
twilio:gather_stop()
}
}
EOF
end
def options()
rv = []
[1,2,3,4,5,6,7,8,9].each do |n|
if @yml[:options][n] then
rv.push(menurule n)
else
rv.push(deadkey n)
end
end
rv.join "\n"
end
def menurule(n)
<<EOF
rule menu#{n} is active {
select when twilio keypress Digits "#{n}"
twilio:say(option#{n})
fired { raise explicit event givemenu }
}
EOF
end
def deadkey(n)
<<EOF
rule menu#{n} is active {
select when twilio keypress Digits "#{n}"
twilio:say(error)
fired { raise explicit event givemenu }
}
EOF
end
def signoff()
<<EOF
rule signoff is active {
select when twilio keypress Digits "0"
or explicit signoff
{
twilio:say(goodbye);
twilio:hangup()
}
}
EOF
end
if __FILE__ == $0 then
ruleset
end
__END__
---
:author: Kilter User
:name: Kilter Demo
:description: kilter generated twilio ruleset
:greeting: Welcome to the Kilter Demo.
:menu: Press 1 to hear an important message, 2 for more information.
:error: sorry.
:goodbye: Thanks for calling. Goodbye!
:options: {
1: You selected option 1,
2: You selected option 2,
6: You were not expecting 6
}
@rsbohn
Copy link
Author

rsbohn commented Nov 13, 2010

This just spits out a ruleset. A future version will use the Kynetx API to upload and deploy the ruleset. Then it will set the webhook on Twilio to call the ruleset. You will have to authenticate to both services, but it will make your first Twilio app as easy as filling out a form and clicking 'Deploy'.

@rsbohn
Copy link
Author

rsbohn commented Nov 23, 2010

This version (2010-11-22) might work, but if you have errors in your external YAML it will spit a ruleset based on the internal YAML. Caveat emptor!

@rsbohn
Copy link
Author

rsbohn commented Nov 23, 2010

Should I add hook_event to the YAML file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment