How do I dynamically generate VoiceXML with VoiceXMLKit?
Last reviewed: 9/1/2012
HOW Article ID: H031308
The information in this article applies to:
- VoiceXMLKit
Summary
End users typically interact with VoiceXML applications running on servers over a phone connection.
VoiceXMLKit provides components for dynamically generating VoiceXML documents so interactive voice response applications can adapt at runtime.
More Information
The VoiceXMLKit conversation management component is designed to provide you a lot of flexiblity and minimize the programming necessary to dynamically generate VoiceXML.
To create a VoiceXML document, use the CreateDocument method to create the base VoiceXML document object to which to add VoiceXML elements. For example:
NChantVxmlDocument vxmlDocument = NChantXM1.CreateDocument();
NChantVxmlVxml vxml = vxmlDocument.AddVxml();
vxml.Version = "2.0";
// Add Menu
NChantVxmlMenu menu = vxml.AddMenu();
menu.Dtmf = true;
// Add Prompt
NChantVxmlPrompt prompt = menu.AddPrompt();
prompt.AddText("To hear today's specials, first select a department.");
NChantVxmlEnumerate enumerate = prompt.AddEnumerate();
enumerate.AddText("Say");
NChantVxmlValue value = enumerate.AddValue();
value.Expr = "_prompt";
enumerate.AddText(", or press");
value = enumerate.AddValue();
value.Expr = "_dtmf";
enumerate.AddText(". ");
// Add Choices
NChantVxmlChoice choice = menu.AddChoice();
choice.Event = "deli";
choice.AddText("Deli and Bakery");
choice = menu.AddChoice();
choice.Event = "produce";
choice.AddText("Fresh Produce");
choice = menu.AddChoice();
choice.Event = "meat";
choice.AddText("Meat and Seafood");
// Add a menu selection as user event handler
NChantVxmlCatch catchuserevent = menu.AddCatch();
catchuserevent.Event = "deli";
// Lookup Deli and Bakery specials
catchuserevent.AddText(GetDeliSpecials());
// Add return variables
NChantVxmlReturn menuReturn = catchuserevent.AddReturn();
menuReturn.Namelist = "_event";
// Add a menu selection as user event handler
catchuserevent = menu.AddCatch();
catchuserevent.Event = "produce";
// Lookup Fresh Produce specials
catchuserevent.AddText(GetProduceSpecials());
// Add return variables
menuReturn = catchuserevent.AddReturn();
menuReturn.Namelist = "_event";
// Add a menu selection as user event handler
catchuserevent = menu.AddCatch();
catchuserevent.Event = "meat";
// Lookup Meat and Seafood specials
catchuserevent.AddText(GetMeatSpecials());
// Add return variables
menuReturn = catchuserevent.AddReturn();
menuReturn.Namelist = "_event";
// Add catchall handler
NChantVxmlCatch catchall = menu.AddCatch();
catchall.Event = "noinput nomatch";
catchall.AddReprompt();
CChantVxmlDocument* pvxmlDocument = m_pChantXM->CreateDocument();
CChantVxmlVxml* pvxml = pvxmlDocument->AddVxml();
pvxml->SetVersion(L"2.0");
// Add Menu
CChantVxmlMenu* pmenu = pvxml->AddMenu();
pmenu->SetDtmf(true);
// Add Prompt
CChantVxmlPrompt* pprompt = pmenu->AddPrompt();
pprompt->AddText(L"To hear today's specials, first select a department.");
CChantVxmlEnumerate* penumerate = pprompt->AddEnumerate();
penumerate->AddText(L"Say");
CChantVxmlValue* pvalue = penumerate->AddValue();
pvalue->SetExpr(L"_prompt");
penumerate->AddText(L", or press");
pvalue = penumerate->AddValue();
pvalue->SetExpr(L"_dtmf");
penumerate->AddText(L". ");
// Add Choices
CChantVxmlChoice* pchoice = pmenu->AddChoice();
pchoice->SetEvent(L"deli");
pchoice->AddText(L"Deli and Bakery");
pchoice = pmenu->AddChoice();
pchoice->SetEvent(L"produce");
pchoice->AddText(L"Fresh Produce");
pchoice = pmenu->AddChoice();
pchoice->SetEvent(L"meat");
pchoice->AddText(L"Meat and Seafood");
// Add a menu selection as user event handler
CChantVxmlCatch* pcatchuserevent = pmenu->AddCatch();
pcatchuserevent->SetEvent(L"deli");
// Lookup Deli and Bakery specials
pcatchuserevent->AddText(GetDeliSpecials());
// Add return variables
CChantVxmlReturn* pmenuReturn = pcatchuserevent->AddReturn();
pmenuReturn->SetNamelist(L"_event");
// Add a menu selection as user event handler
pcatchuserevent = pmenu->AddCatch();
pcatchuserevent->SetEvent(L"produce");
// Lookup Fresh Produce specials
pcatchuserevent->AddText(GetProduceSpecials());
// Add return variables
pmenuReturn = pcatchuserevent->AddReturn();
pmenuReturn->SetNamelist(L"_event");
// Add a menu selection as user event handler
pcatchuserevent = pmenu->AddCatch();
pcatchuserevent->SetEvent(L"meat");
// Lookup Meat and Seafood specials
pcatchuserevent->AddText(GetMeatSpecials());
// Add return variables
pmenuReturn = pcatchuserevent->AddReturn();
pmenuReturn->SetNamelist(L"_event");
// Add catchall handler
CChantVxmlCatch* pcatchall = pmenu->AddCatch();
pcatchall->SetEvent(L"noinput nomatch");
pcatchall->AddReprompt();
CChantVxmlDocument* pvxmlDocument = m_pChantXM->CreateDocument();
CChantVxmlVxml* pvxml = pvxmlDocument->AddVxml();
pvxml->SetVersion("2.0");
// Add Menu
CChantVxmlMenu* pmenu = pvxml->AddMenu();
pmenu->SetDtmf(true);
// Add Prompt
CChantVxmlPrompt* pprompt = pmenu->AddPrompt();
pprompt->AddText("To hear today's specials, first select a department.");
CChantVxmlEnumerate* penumerate = pprompt->AddEnumerate();
penumerate->AddText("Say");
CChantVxmlValue* pvalue = penumerate->AddValue();
pvalue->SetExpr("_prompt");
penumerate->AddText(", or press");
pvalue = penumerate->AddValue();
pvalue->SetExpr("_dtmf");
penumerate->AddText(". ");
// Add Choices
CChantVxmlChoice* pchoice = pmenu->AddChoice();
pchoice->SetEvent("deli");
pchoice->AddText("Deli and Bakery");
pchoice = pmenu->AddChoice();
pchoice->SetEvent("produce");
pchoice->AddText("Fresh Produce");
pchoice = pmenu->AddChoice();
pchoice->SetEvent("meat");
pchoice->AddText("Meat and Seafood");
// Add a menu selection as user event handler
CChantVxmlCatch* pcatchuserevent = pmenu->AddCatch();
pcatchuserevent->SetEvent("deli");
// Lookup Deli and Bakery specials
pcatchuserevent->AddText(GetDeliSpecials());
// Add return variables
CChantVxmlReturn* pmenuReturn = pcatchuserevent->AddReturn();
pmenuReturn->SetNamelist("_event");
// Add a menu selection as user event handler
pcatchuserevent = pmenu->AddCatch();
pcatchuserevent->SetEvent("produce");
// Lookup Fresh Produce specials
pcatchuserevent->AddText(GetProduceSpecials());
// Add return variables
pmenuReturn = pcatchuserevent->AddReturn();
pmenuReturn->SetNamelist("_event");
// Add a menu selection as user event handler
pcatchuserevent = pmenu->AddCatch();
pcatchuserevent->SetEvent("meat");
// Lookup Meat and Seafood specials
pcatchuserevent->AddText(GetMeatSpecials());
// Add return variables
pmenuReturn = pcatchuserevent->AddReturn();
pmenuReturn->SetNamelist("_event");
// Add catchall handler
CChantVxmlCatch* pcatchall = pmenu->AddCatch();
pcatchall->SetEvent("noinput nomatch");
pcatchall->AddReprompt();
var
specialsVXML: string;
aTChantVxmlDocument: TChantVxmlDocument;
aTChantVxmlVxml: TChantVxmlVxml;
aTChantVxmlMenu: TChantVxmlMenu;
aTChantVxmlPrompt: TChantVxmlPrompt;
aTChantVxmlEnumerate: TChantVxmlEnumerate;
aTChantVxmlValue: TChantVxmlValue;
aTChantVxmlChoice: TChantVxmlChoice;
aTChantVxmlCatch: TChantVxmlCatch;
aTChantVxmlReturn: TChantVxmlReturn;
begin
aTChantVxmlDocument := ChantXM1.CreateDocument();
aTChantVxmlVxml := aTChantVxmlDocument.AddVxml();
aTChantVxmlVxml.Version := '2.0';
// Add Menu
aTChantVxmlMenu := aTChantVxmlVxml.AddMenu();
aTChantVxmlMenu.Dtmf := True;
// Add Prompt
aTChantVxmlPrompt := aTChantVxmlMenu.AddPrompt();
aTChantVxmlPrompt.AddText('To hear today''s specials, first select a department.');
aTChantVxmlEnumerate := aTChantVxmlPrompt.AddEnumerate();
aTChantVxmlEnumerate.AddText('Say');
aTChantVxmlValue := aTChantVxmlEnumerate.AddValue();
aTChantVxmlValue.Expr := '_prompt';
aTChantVxmlEnumerate.AddText(', or press');
aTChantVxmlValue := aTChantVxmlEnumerate.AddValue();
aTChantVxmlValue.Expr := '_dtmf';
aTChantVxmlEnumerate.AddText('. ');
// Add Choices
aTChantVxmlChoice := aTChantVxmlMenu.AddChoice();
aTChantVxmlChoice.Event := 'deli';
aTChantVxmlChoice.AddText('Deli and Bakery');
aTChantVxmlChoice := aTChantVxmlMenu.AddChoice();
aTChantVxmlChoice.Event := 'produce';
aTChantVxmlChoice.AddText('Fresh Produce');
aTChantVxmlChoice := aTChantVxmlMenu.AddChoice();
aTChantVxmlChoice.Event := 'meat';
aTChantVxmlChoice.AddText('Meat and Seafood');
// Add a menu selection as user event handler
aTChantVxmlCatch := aTChantVxmlMenu.AddCatch();
aTChantVxmlCatch.Event := 'deli';
// Lookup Deli and Bakery specials
aTChantVxmlCatch.AddText(GetDeliSpecials());
// Add return variables
aTChantVxmlReturn := aTChantVxmlCatch.AddReturn();
aTChantVxmlReturn.Namelist := '_event';
// Add a menu selection as user event handler
aTChantVxmlCatch := aTChantVxmlMenu.AddCatch();
aTChantVxmlCatch.Event := 'produce';
// Lookup Fresh Produce specials
aTChantVxmlCatch.AddText(GetProduceSpecials());
// Add return variables
aTChantVxmlReturn := aTChantVxmlCatch.AddReturn();
aTChantVxmlReturn.Namelist := '_event';
// Add a menu selection as user event handler
aTChantVxmlCatch := aTChantVxmlMenu.AddCatch();
aTChantVxmlCatch.Event := 'meat';
// Lookup Meat and Seafood specials
aTChantVxmlCatch.AddText(GetMeatSpecials());
// Add return variables
aTChantVxmlReturn := aTChantVxmlCatch.AddReturn();
aTChantVxmlReturn.Namelist := '_event';
// Add catchall handler
aTChantVxmlCatch := aTChantVxmlMenu.AddCatch();
aTChantVxmlCatch.Event := 'noinput nomatch';
aTChantVxmlCatch.AddReprompt();
end;
JChantVxmlDocument vxmlDocument = JChantXM1.createDocument();
JChantVxmlVxml vxml = vxmlDocument.addVxml();
vxml.setVersion("2.0");
// Add Menu
JChantVxmlMenu menu = vxml.addMenu();
menu.setDtmf(true);
// Add Prompt
JChantVxmlPrompt prompt = menu.addPrompt();
prompt.addText("To hear today's specials, first select a department.");
JChantVxmlEnumerate enumerate = prompt.addEnumerate();
enumerate.addText("Say");
JChantVxmlValue value = enumerate.addValue();
value.setExpr("_prompt");
enumerate.addText(", or press");
value = enumerate.addValue();
value.setExpr("_dtmf");
enumerate.addText(". ");
// Add Choices
JChantVxmlChoice choice = menu.addChoice();
choice.setEvent("deli");
choice.addText("Deli and Bakery");
choice = menu.addChoice();
choice.setEvent("produce");
choice.addText("Fresh Produce");
choice = menu.addChoice();
choice.setEvent("meat");
choice.addText("Meat and Seafood");
// Add a menu selection as user event handler
JChantVxmlCatch catchuserevent = menu.addCatch();
catchuserevent.setEvent("deli");
// Lookup Deli and Bakery specials
catchuserevent.addText(getDeliSpecials());
// Add return variables
JChantVxmlReturn menuReturn = catchuserevent.addReturn();
menuReturn.setNamelist("_event");
// Add a menu selection as user event handler
catchuserevent = menu.addCatch();
catchuserevent.setEvent("produce");
// Lookup Fresh Produce specials
catchuserevent.addText(getProduceSpecials());
// Add return variables
menuReturn = catchuserevent.addReturn();
menuReturn.setNamelist("_event");
// Add a menu selection as user event handler
catchuserevent = menu.addCatch();
catchuserevent.setEvent("meat");
// Lookup Meat and Seafood specials
catchuserevent.addText(getMeatSpecials());
// Add return variables
menuReturn = catchuserevent.addReturn();
menuReturn.setNamelist("_event");
// Add catchall handler
JChantVxmlCatch catchall = menu.addCatch();
catchall.setEvent("noinput nomatch");
catchall.addReprompt();
Dim specialsVXML As String
Dim vxmlDocument As NChantVxmlDocument
Dim vxml As NChantVxmlVxml
Dim menu As NChantVxmlMenu
Dim prompt As NChantVxmlPrompt
Dim enumerate As NChantVxmlEnumerate
Dim value As NChantVxmlValue
Dim choice As NChantVxmlChoice
Dim catchuserevent As NChantVxmlCatch
Dim menuReturn As NChantVxmlReturn
Dim catchall As NChantVxmlCatch
specialsVXML = String.Empty
' Build VoiceXML document from specials database
vxmlDocument = NChantXM1.CreateDocument()
vxml = vxmlDocument.AddVxml()
vxml.Version = "2.0"
' Add Menu
menu = vxml.AddMenu()
menu.Dtmf = True
' Add Prompt
prompt = menu.AddPrompt()
prompt.AddText("To hear today's specials, first select a department.")
enumerate = prompt.AddEnumerate()
enumerate.AddText("Say")
value = enumerate.AddValue()
value.Expr = "_prompt"
enumerate.AddText(", or press")
value = enumerate.AddValue()
value.Expr = "_dtmf"
enumerate.AddText(". ")
' Add Choices
choice = menu.AddChoice()
choice.Event = "deli"
choice.AddText("Deli and Bakery")
choice = menu.AddChoice()
choice.Event = "produce"
choice.AddText("Fresh Produce")
choice = menu.AddChoice()
choice.Event = "meat"
choice.AddText("Meat and Seafood")
' Add a menu selection as user event handler
catchuserevent = menu.AddCatch()
catchuserevent.Event = "deli"
' Lookup Deli and Bakery specials
catchuserevent.AddText(GetDeliSpecials())
' Add return variables
menuReturn = catchuserevent.AddReturn()
menuReturn.Namelist = "_event"
' Add a menu selection as user event handler
catchuserevent = menu.AddCatch()
catchuserevent.Event = "produce"
' Lookup Fresh Produce specials
catchuserevent.AddText(GetProduceSpecials())
' Add return variables
menuReturn = catchuserevent.AddReturn()
menuReturn.Namelist = "_event"
' Add a menu selection as user event handler
catchuserevent = menu.AddCatch()
catchuserevent.Event = "meat"
' Lookup Meat and Seafood specials
catchuserevent.AddText(GetMeatSpecials())
' Add return variables
menuReturn = catchuserevent.AddReturn()
menuReturn.Namelist = "_event"
' Add catchall handler
catchall = menu.AddCatch()
catchall.Event = "noinput nomatch"
catchall.AddReprompt()
To dynamically generate the VoiceXML, use the GenerateVXML method to create the base VoiceXML document. For example:
// Get the VoiceXML
string specialsVXML = vxmlDocument.GenerateVXML(ChantVXMLVersion.CVVAll);
// Delete the document object
vxmlDocument.Dispose();
// Get the VoiceXML
BSTR SpecialsVXML = SysAllocString(pvxmlDocument->GenerateVXML(CVVAll));
// Delete the document object
delete pvxmlDocument;
// Get the VoiceXML
String SpecialsVXML = pvxmlDocument->GenerateVXML(CVVAll);
// Delete the document object
delete pvxmlDocument;
// Get the VoiceXML
specialsVXML := aTChantVxmlDocument.GenerateVXML(CVVAll);
// Delete the document object
aTChantVxmlDocument.Destroy;
// Get the VoiceXML
String SpecialsVXML = vxmlDocument.generateVXML(ChantVXMLVersion.CVVAll);
// Delete the document object
vxmlDocument.finalize();
vxmlDocument = null;
' Get the VoiceXML
specialsVXML = vxmlDocument.GenerateVXML(ChantVXMLVersion.CVVAll)
' Delete the document object
vxmlDocument.Dispose()
For additional help with VoiceXMLKit, contact Chant Support via or web.