A tool to help generate sample code for creating TwiML with Twilio's Helper Libraries
Helper Library | Version |
---|---|
twilio-csharp | 5.x |
twilio-java | 7.x |
twilio-node | 3.x |
twilio-php | 5.x |
twilio-python | 6.x |
twilio-ruby | 5.x |
The generator tool will try to test the generated snippets using a local environment for every language's SDK version.
In order to use the testing functionality, you need to install each Helper Library.
Click to see installation instructions for each Helper Library and related dependencies
- twilio-csharp GitHub Repo
- Requirements:
twilio-csharp
installation:- From within this project's root directory run the following command:
dotnet add package Twilio
- From within this project's root directory run the following command:
-
Requirements:
-
Java 8
To install Java 8, run the following command:
brew tap adoptopenjdk/openjdk brew install adoptopenjdk/openjdk/adoptopenjdk8 --cask
-
-
twilio-java
installation:- Go to mvnrepository.com and click on the version of the Helper Library you need.
- In the table at the top of the page, find Files and click on jar (may need to click on View All and select the
twilio-<version number>-jar-with-dependencies.jar
file?) - Create a
lib
directory in the root of this project and place the.jar
file in it.
-
Requirements:
- Python 3.7+
-
twilio-python
installation:-
Install
twilio
sdk withpip
(Note: If you followed the Installation instructions for this repo,
twilio-python
is already installed!)pip3 install twilio
-
-
Requirements:
-
Ruby 3.1 and rbenv
To install
rbenv
, run the following command:brew install rbenv rbenv init
Follow the instructions printed out from rbenv init for setting up the rbenv shell integration.
NOTE: You must also add the output of
rbenv init
to your~/.bash_profile
, even if you use another shell, such as.zsh
. The generator script uses Python's subprocess module, which will only load yourbash_profile
to run commands.Then, install your desired Ruby version:
rbenv install 2.6.3 # or other preferred version; the twilio-ruby works with ruby >1.9.3 rbenv global 2.6.3 # or whichever version you installed rbenv rehash # installs shims -- run this after installing a new ruby version with rbenv gem update --system # update the RubyGems system software
-
-
twilio-ruby
installation:Run the following command:
gem install twilio-ruby
or install a specific
twilio-ruby
version:gem install twilio-ruby -v 5.76.0
-
Requirements:
-
PHP 8
To install PHP 8, run the following command:
brew install php
-
Run the following command:
brew install composer
-
-
twilio-php
installation:In the root directory of this project, run the following command:
composer require twilio/sdk
This will create
composer.json
andcomposer.lock
files.
-
Requirements:
-
Node.js 14+
To install Node.js, run the following command:
brew install node
-
-
twilio-node
installation:In the root directory of this project, run the following command:
npm install twilio
Create a Python virtual environment:
python3 -m venv venv
Activate the environment and install requirements:
source venv/bin/activate
pip install -r requirements.txt
You can run the tool via the command line or use it as a Python library.
For either option, you first need to create a TwiML file.
-
Include the XML declaration line at the top of the file.
<?xml version="1.0" encoding="UTF-8"?>
-
<Response>
and</Response>
tags must be present. -
Use
example.com
domains for any sample URLs. -
⚠️ Unfortunately, the Helper Libraries don't do any sort of enforcement around required or associated attributes. You will only be made aware of errors once Twilio executes the TwiML (e.g. during a call). Therefore:- Make sure that you are including any required attributes and/or a body if necessary.
- If any attribute implies the use of another attribute, make sure to include it in the example. E.g. You wouldn't/shouldn't use
statusCallbackEvent
without also using thestatusCallback
attribute. - Don't force errors onto our customers. Not sure if the TwiML actually works? Hook up a Twilio Phone Number to a TwiML Bin and test it out yourself.
.twiml
.
You can generate the Helper Library code by running the generator.py
file.
In your terminal, run a command with the following format:
./generator.py <your .xml filepath> -l <Helper Library language>
The -l
option specifies the Helper Library for which you wish to generate code. The allowed values are:
python
ruby
csharp
java
php
node
Example:
./generator.py assets/dial-basic.xml -l node
By default, the tool outputs the Helper Library code into the /generators/<Helper Library language>
directory.
You can specify an output location with the -out
flag:
./generator.py <your .twiml filepath> -out <output filepath> -l <Helper Library language>
Example:
./generator.py assets/dial-basic.xml -out ./assets/dial-basic/dial-basic.4.x.js -l node
some-example.4.x.js
.
The vast majority of TwiML verbs are for Voice. If you would like to create a new TwiML code sample for Messaging rather than for Voice, you can pass in the --messaging
flag:
./generator.py <filepath of TwiML file> -out <filepath of code to verify> -l <Helper Library language> --messaging
This tool will automatically verify each code sample as it is created. You can also test an existing code sample without generating new sample code with the --verify
flag.
The format is:
./generator.py <filepath of TwiML file> -out <filepath of code to verify> -l <Helper Library language> --verify
Example:
./generator.py assets/call_on_hold.xml -out assets/call_on_hold.py -l python --verify
Below is a small example on how to use this tool in your Python code:
from twiml_code_generator import TwimlCodeGenerator
my_twiml_file = 'assets/record_voicemail.xml'
code_generator = TwimlCodeGenerator(my_twiml_file, language='python')
# Print out the generated code
print(code_generator)
# Write a file with the generated code
code_generator.write_code()
# Run the generated code and verify the output xml against the source
# Only: Python, PHP, Node
code_generator.verify()
(Coming soon)
MIT