Giter Site home page Giter Site logo

ex_twiml's Introduction

ExTwiml

Build Status Hex Version Inline docs

Generate TwiML for your Twilio integration, right inside Elixir! Built to be used with Telephonist, but it can be used entirely on its own.

Installation

ExTwiml can be installed through mix as a hex package. First, update your dependencies in mix.exs.

defp deps do
  [{:ex_twiml, "~> 2.1.3"}]
end

If you prefer, you can depend on the latest version from Github:

defp deps do
  [{:ex_twiml, github: "danielberkompas/ex_twiml"}]
end

Add it to your application list in mix.exs:

def application do
  [mod: {YourApp, []},
+   applications: [:ex_twiml]]
end

Then run mix deps.get, and ExTwiml will be installed.

Usage

Import the ExTwiml module into your module, and then use the twiml macro to generate your TwiML, like so:

defmodule YourModule do
  import ExTwiml

  def render do
    twiml do
      play "/assets/welcome.mp3"
      gather digits: 4, finish_on_key: "#" do
        say """
        Please enter the last four digits of your credit card number, followed
        by the pound sign.
        """, voice: "woman"
      end
    end
  end
end

You can then render the TwiML by calling YourModule.render/0. The output will be a binary in this format:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Play>/assets/welcome.mp3</Play>
  <Gather digits="4" finishOnKey="#">
    <Say voice="woman">
      Please enter the last four digits of your credit card number, followed by
      the pound sign.
    </Say>
  </Gather>
</Response>

The twiml macro simply returns a binary (or string), so you're not limited to the pattern above. Just use twiml wherever you need a TwiML string.

Configuration

You can configure default options for verbs. For example, suppose you wanted all <Say> verbs to use the "Alice" voice, and all <Gather> verbs to have the method "GET".

Simply add this code to your config/config.exs:

config :ex_twiml, :defaults,
  say: [voice: "alice"],
  gather: [method: "GET"]

Then this code:

twiml do
  gather do
    say "Hello"
  end
end

Will result in this TwiML:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Gather method="GET">
    <Say voice="alice">Hello</Say>
  </Gather>
</Response>

Defaults can be overridden:

gather method: "POST"
say "Hello", voice: "woman"

Supported Verbs and Nouns

See the Twilio Documentation for a complete list of verbs supported by Twilio. ExTwiml has built in macros for the following verbs and nouns:

To maximize compatibility with future versions of the TwiML spec, ExTwiml does not validate the attributes passed to the various verbs to ensure that they are supported by Twilio. You'll need to do this yourself.

Unsupported Verbs?

If the Twilio API adds a verb and I haven't yet added support for it, you can do two things:

  1. Submit a PR to add it
  2. Use the tag macro in the meantime while the PR is reviewed

The tag macro can be used to create arbitrary XML tags in the output, like this:

tag :custom, option_1: "value" do
  tag :speak, do: "Whatever you say, boss!"
end
# => "<Custom option1="value"><Speak>Whatever you say, boss!</Speak></Custom>"

Contributing

Contributions are welcome!

  1. Fork the repository
  2. Code up your changes
  3. Submit a PR back here with your fix.

ex_twiml's People

Contributors

danielberkompas avatar matteosister avatar tomciopp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ex_twiml's Issues

Dynamically calling verbs.

Hi Daniel,

I'm having some trouble trying to build a menu dynamically from an attribute, e.g.:

  @menu_items [
    {"1", "first option"},
    {"2", "second option"},
  ]

  def menu(params) do
    twiml do
      gather numDigits: "1", method: "GET" do
        @menu_items |> Enum.each fn ({number, option_text}) ->
          say "For #{option_text} press #{number}."
        end
      end 
    end 
  end

The error I'm getting is:

** (CompileError) lib/telephone_provisioner/call_flows/job/details.ex:15: cannot invoke remote function ExTwiml.Utilities.create_tag/3 inside match

My guess is that it has something to do with metaprogramming, but I'm new enough to that side of elixir that I can't quite figure out how ex_twiml works. Is there a way you've used successfully for doing such a thing?

Cheers,
Brenton.

consider validating requests are from twilio

It might make sense to implement a helper function to validate requests are actually from Twilio by inspecting the X-Twilio-Signature header, full request url, and user's TWILIO_AUTH_TOKEN (as described in their security docs here).

Thanks for sharing this project!

Escape Characters

I just created the following TwilML:

msg = "Hello <> Goodbye"

twiml do
      say msg
end

and that generated the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>Hello <> Goodbye</Say>
</Response>

That's clearly not the expected behavior, specifically, we should expect:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<![CDATA[Hello <> Goodbye]]>
</Response>

Should we either 1) add escaping, or 2) add an actual XML generation library? In either case, we need to handle escaping of special characters in XML since this could be a huge security flaw due to injection if you have any dynamically generated data in your TwilML templates.

Fix Build Badge & Travis Build

Build Status

Build should say either pass or fail.

Current Build Log

Using worker: worker-linux-docker-949891bb.prod.travis-ci.org:travis-linux-7
system_info
Build system information
Build language: elixir
Build image provisioning date and time
Thu Feb  5 15:09:33 UTC 2015
Operating System Details
Distributor ID: Ubuntu
Description:    Ubuntu 12.04.5 LTS
Release:    12.04
Codename:   precise
Linux Version
3.13.0-29-generic
Cookbooks Version
a68419e https://github.com/travis-ci/travis-cookbooks/tree/a68419e
GCC version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
LLVM version
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Pre-installed Ruby versions
ruby-1.9.3-p551
Pre-installed Node.js versions
v0.10.36
Pre-installed Go versions
1.4.1
Redis version
redis-server 2.8.19
riak version
2.0.2
MongoDB version
MongoDB 2.4.12
CouchDB version
couchdb 1.6.1
Neo4j version
1.9.4
RabbitMQ Version
3.4.3
ElasticSearch version
1.4.0
Installed Sphinx versions
2.0.10
2.1.9
2.2.6
Default Sphinx version
2.2.6
Installed Firefox version
firefox 31.0esr
PhantomJS version
1.9.8
ant -version
Apache Ant(TM) version 1.8.2 compiled on December 3 2011
mvn -version
Apache Maven 3.2.5 (12a6b3acb947671f09b81f49094c53f426d8cea1; 2014-12-14T17:29:23+00:00)
Maven home: /usr/local/maven
Java version: 1.7.0_76, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-7-oracle/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "3.13.0-29-generic", arch: "amd64", family: "unix"
git.checkout
0.08s$ git clone --depth=50 --branch=master git://github.com/danielberkompas/ex_twiml.git danielberkompas/ex_twiml
Cloning into 'danielberkompas/ex_twiml'...
remote: Counting objects: 210, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 210 (delta 9), reused 6 (delta 6), pack-reused 193
Receiving objects: 100% (210/210), 38.81 KiB | 0 bytes/s, done.
Resolving deltas: 100% (106/106), done.
Checking connectivity... done.
$ cd danielberkompas/ex_twiml
$ git checkout -qf e6f521547ea4799d61d5c6b9a6f76ff43cfef0e6
This job is running on container-based infrastructure, which does not allow use of 'sudo', setuid and setguid executables.
If you require sudo, add 'sudo: required' to your .travis.yml
See http://docs.travis-ci.com/user/workers/container-based-infrastructure/ for details.
0.00s$ source $HOME/otp/17.4/activate
Installing Elixir 1.0.3
3.32s$ travis_retry kiex install 1.0.3 && kiex use 1.0.3
Unknown Elixir '1.0.3' ☹ 
Try
    kiex list known -- known releases
    kiex list branches -- current branches
The command "kiex install 1.0.3" failed. Retrying, 2 of 3.
Unknown Elixir '1.0.3' ☹ 
Try
    kiex list known -- known releases
    kiex list branches -- current branches
The command "kiex install 1.0.3" failed. Retrying, 3 of 3.
Unknown Elixir '1.0.3' ☹ 
Try
    kiex list known -- known releases
    kiex list branches -- current branches
The command "kiex install 1.0.3" failed 3 times.
The command "travis_retry kiex install 1.0.3 && kiex use 1.0.3" failed and exited with 1 during .
Your build has been stopped.

Using with Phoenix Framework

Daniel,

This looks like a great package. As an Elixir/Phoenix newcomer how might I use it with the Phoenix Framework?

I have it installed as a dependency but I'm unsure where to put the render method and how it sits with views and templates.

Thanks in advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.