• This repository has been archived on 15/Aug/2024
  • Stars
    star
    259
  • Rank 156,980 (Top 4 %)
  • Language
    Java
  • License
    MIT License
  • Created over 8 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code snippets for the Twilio API Documentation

Code snippets for the Twilio API documentation

Build Status

Guidelines

  1. Snippets should use environmental variables to store user account information instead of placeholders. For example (in NodeJS):

    accountSid = process.env.TWILIO_ACCOUNT_SID
    

    Instead of:

    accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    

    As the latter impose a high security risk for users who push their own credentials on public repositories.

  2. Critical user account information that should be stored on environmental variables is:

    • Account SID
    • Sub accounts SIDs
    • Authentication Token
    • API key SID
    • API key Secret

    Any other information such as Call SID, etc. can have placeholders on them:

    Call SID:    CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    

    In the case of phone numbers, the following should be used:

    Destination Phone Number: +15558675310
    From/Twilio Number: +15017122661
    
  3. Snippet file names are important. A snippet's file extension is the only way to mark them for a specific language. This is important because the language will be visible on the docs. Also, you must mark snippets with the twilio-client version that is used in that particular snippet.

    For example, a java snippet that uses twilio-java v7.x should be named as: snippet-example-1.7.x.java

    The same applies to the rest of languages and library versions, and this is the list of currently supported languages and versions:

    snippet-example-1.6.x.java
    snippet-example-1.7.x.java
    snippet-example-1.2.x.js
    snippet-example-1.4.x.js
    snippet-example-1.6.x.rb
    snippet-example-1.6.x.cs
    snippet-example-1.4.x.php
    snippet-example-1.7.x.php
    snippet-example-1.8.x.py
    

    And the full structure should look like this:

    rest/
    |-- resource-example/
        └── snippet-example-1/
            |-- output/
            |   └── snippet-example-1.json
            |-- snippet-example-1.7.x.java
            |-- snippet-example-1.<sdk-version>.x.<language>
            |-- snippet-example-1.json.curl
            |-- snippet-example-1.xml.curl
            └── meta.json
    

    Client version is important, as that is how the test harness knows which version of the client it should use for testing.

Continuous Integration

All snippets are run in a container-based environment for each language (see .travis.yml file), so the first check we do is for syntax error.

There're two kind of snippets we test:

  • API/REST snippets: For these cases we only check syntax error. But, these snippets also do a request to Twilio's endpoints, so we mock this behaviour using a fake API server.

    Note: The test doesn't check if the http request body is well formed to do what the intended snippet is supposed to do, e.g., if you have a snippet doing resource.update(propA=True), but the expected result is to update propB, the test may go green because there's no syntax error. That's because some attributes are optional to update, so the request body may or may not has the parameters needed to update the resource according to the expected result.

  • TwiML snippets: When running TwiML snippets, the snippets should print the xml result to the standard output, so we can capture that output and compare it with the expected xml sample. We read this /output/sample.xml file which is placed in the same folder of the snippet.

How to mark specific folders as testable

If you want a snippet folder to be tested, there are two ways to mark it as testable:

  1. Using a meta.json file: This file is required and must live in all snippet folders, inside you can enable its specific folder for testing, adding "testable": true like so:

    {
      "title": "Snippet title",
      "type": "server",
      "testable": true
    }
  2. Using a test.yml file: This file is optional and can live inside any folder, it's used to specify a deep recursive testing for all folders inside. So, if the marked directory contains several snippets, all of them will be marked as testable recursively. You just need to add testable: true to the file like so:

    testable: true

Notes:

  • For testing TwiML snippets the option available is:

    meta.json -> "test_output": true
    test.yml -> test_output: true
    

    This will compare the output of the snippet with the output/sample.xml file inside the same directory of the snippet.

  • You can mark a directory or a snippet to be not testable too.

    meta.json -> "testable": false
    test.yml -> testable: false
    

    This way, you can mark snippets or directories to be tested or not, by exception.

  • For a snippet to be testable, it has to contain the basic things a program in that language should have. For example in Java, the code snippet should have a public class and a main method in it.

  • Specific dependencies are supported for snippets. If a new dependency is introduced, the testing scripts should be modified to support it.

Local Snippet Testing

The next steps describe how to run the snippets test locally in a UNIX based operating system (examples will be provided for Debian based Linux distributions and OSX using Homebrew). This has not been tested in a Windows OS.

Setting Up the Environment

Make sure you have the following dependencies installed on your system.

  1. Install Node.js. The best option for this is to use nvm

    curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash \
    export NVM_DIR="$HOME/.nvm" \
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" \
    nvm install --lts
    npm install
  2. Install Python and PIP.

    • On Linux:
    sudo apt-get install python-pip python2.7-dev build-essential \
    sudo pip install --upgrade pip
    • On OSX:
    brew install python

    Once you have python and pip ready, run the command below

    pip install -r requirements.txt
    
  3. Install JDK8 and Gradle. The best option for this is to use sdkman

    curl -s "https://get.sdkman.io" | bash \
    source "$HOME/.sdkman/bin/sdkman-init.sh" \
    sdk install java \
    sdk install gradle
  4. Install PHP 5 with CURL.

    • On Linux:
    sudo add-apt-repository ppa:ondrej/php -y
    sudo apt-get update
    sudo apt-get install -y --force-yes php5.6
    • On OSX:
    brew install php56

    Once you have php ready, please install Composer

    curl -sS https://getcomposer.org/installer | \
    sudo php -- --install-dir=/usr/local/bin --filename=composer
  5. Install Ruby and RubyGems.

    curl -sSL https://get.rvm.io | bash -s stable --ruby
  6. Install MonoDevelop.

    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF \
    echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list \
    sudo apt-get update \
    sudo apt-get install mono-complete -y
    • On OSX:
    brew install mono

Install Language Dependencies

The testing and installation scripts use ruby. Before installing language dependencies you need to install the following gems:

gem install json colorize nokogiri

You can use the following command to install language dependencies, which will download the latest version of the helper libraries for each language:

ruby tools/snippet-testing/model/dependency.rb

Note: That file specifies the latest version for every helper library for each language, but for java the files to specify the versions are located in: tools/snippet-testing/language_handler/file-templates/build.<version>.gradle where <version> should be 6 or 7.

Another option is to use the snippet_tester.rb file and pass the -i flag, this will try to install whatever dependency is needed before running the tests:

ruby tools/snippet-testing/snippet_tester.rb -i

Note: This will also install missing dependencies before running the tests. sudo will be used within the dependency installation script so you might need to enter your password. DO NOT run the whole script with sudo as it would install dependencies for the wrong user.

Run the Tests

  1. Clone and run the fake-api server in a different terminal session.

    git clone [email protected]:TwilioDevEd/twilio-api-faker.git \
    cd twilio-api-faker \
    sudo gradle run
  2. Make your system trust the fake server's self signed certificate.

    • On Linux:
    sudo apt-get install ca-certificates \
    sudo cp twilio-api-faker/keystore/twilio_fake.pem /usr/local/share/ca-certificates/twilio_fake.crt \
    sudo update-ca-certificates
    • On OSX: Use the system's keychain to trust the provided certificate in the keystore directory of the fake-api repo. Go here for more information.
  3. Change your hosts file.

    Edit your /etc/hosts file. Add the following entries:

    127.0.0.1 api.twilio.com
    127.0.0.1 chat.twilio.com
    127.0.0.1 fax.twilio.com
    127.0.0.1 ip-messaging.twilio.com
    127.0.0.1 lookups.twilio.com
    127.0.0.1 messaging.twilio.com
    127.0.0.1 monitor.twilio.com
    127.0.0.1 notifications.twilio.com
    127.0.0.1 notify.twilio.com
    127.0.0.1 pricing.twilio.com
    127.0.0.1 preview.twilio.com
    127.0.0.1 sync.twilio.com
    127.0.0.1 taskrouter.twilio.com
    127.0.0.1 video.twilio.com
    127.0.0.1 wireless.twilio.com
    
  4. Make a copy of the .env.example file.

    cp .env.example .env
  5. Set the necessary environment variables.

    Change environment variables in the .env file to match your configuration and then use the source command to export the variables.

    source .env
  6. Finally, run the tests.

    ruby tools/snippet-testing/snippet_tester.rb

    Note: Remember to mark the directories you want to be tested with a test.yaml file. For more information go here.

    You can also specify a directory to be tested (relative or absolute path). If a directory is specified, then the default testing behavior for that directory and everything it contains is true.

    ruby tools/snippet-testing/snippet_tester.rb -d rest/making-calls

More Repositories

1

webhooks-course

JavaScript
614
star
2

introduction-to-apis-notes

Introduction to APIs - v2
HTML
546
star
3

message-segment-calculator

JS-based tool to calculate and display message segmentation data for a given message body
TypeScript
85
star
4

sdk-starter-node

Demo application showcasing Twilio API usage in Node.js
JavaScript
79
star
5

appointment-reminders-django

A working Appointment Reminders sample built in Django
Python
77
star
6

appointment-reminders-flask

Use Twilio to send SMS reminders to your customers about upcoming appointments
Python
71
star
7

video-access-token-server-node

Twilio Video server-side web app in Node.js
JavaScript
65
star
8

browser-calls-node

A sample application which shows you how to make and receive phone calls with a browser and Twilio Client
JavaScript
63
star
9

server-notifications-node

server notifications demo app
JavaScript
58
star
10

appointment-reminders-node

Node implementation of Appointment Reminders
JavaScript
58
star
11

twiliochat-node

Node implementation of Twilio Chat
JavaScript
54
star
12

airtng-node

AirTNG - Rental-by-owner properties fit for a Captain, implemented with Node.js and Express
JavaScript
51
star
13

automated-survey-django

A sample app for automated phone surveys with Twilio, TwiML, Python and Django
Python
50
star
14

client-quickstart-node

Twilio Client Quickstart App
JavaScript
47
star
15

twiliochat-swift

Swift implementation of Twilio Chat
Swift
46
star
16

client-quickstart-js-deprecated

Twilio Client Quickstart for Web/Javascript
JavaScript
46
star
17

server-notifications-django

server notifications demo application
Python
42
star
18

ivr-phone-tree-node

Create an IVR phone tree with Node.js and Express
JavaScript
41
star
19

voice-javascript-sdk-quickstart-node

JavaScript
40
star
20

ipm-quickstart-node

39
star
21

clicktocall-node

Node.js Click To Call Demo
HTML
39
star
22

survey-node

Automated surveys via text and voice in Node.js with Express and MongoDB
JavaScript
38
star
23

appointment-reminders-rails

A working Appointment Reminders sample built in Ruby on Rails
Ruby
36
star
24

appointment-reminders-laravel

Example App and Tutorial content for Appointment Reminders in PHP | Laravel
PHP
33
star
25

browser-calls-django

A sample application which shows you how to make and receive phone calls with a browser and Twilio Client
Python
33
star
26

conversations-demo

Demo chat app for use in Conversations Quickstart, with updated UI.
JavaScript
32
star
27

account-security-quickstart-django

A simple implementation of a Twilio Account Security protected site using Python and Django.
Python
31
star
28

ivr-phone-tree-python

Create an IVR phone tree with Python and Twilio.
Python
31
star
29

server-notifications-laravel

demo application demonstrating SMS notifications for Laravel
PHP
30
star
30

authy2fa-flask

2FA Implementation with Authy and Python Flask
Python
30
star
31

browser-calls-laravel

A sample application which shows you how to make and receive phone calls with a browser and Twilio Client
PHP
30
star
32

verify-v2-quickstart-node

JavaScript
29
star
33

whatsapp-group-messaging

Code samples and instructions for implementing group messaging in WhatsApp with the Twilio Conversations API
JavaScript
29
star
34

authy2fa-laravel

2FA Implementation with Authy and Laravel (PHP)
PHP
26
star
35

video-access-token-server-php

Twilio Video server-side web app in PHP
PHP
26
star
36

account-security-2fa-node

Deprecated repository. Please see https://github.com/TwilioDevEd/account-security-quickstart-node
HTML
25
star
37

browser-dialer-react

A browser-based dialer interface powered by React and Twilio Client
JavaScript
25
star
38

automated-survey-laravel

Sample application demonstrating automated voice surveys in PHP with laravel
PHP
24
star
39

browser-dialer-vue

A browser-based dialer interface powered by Vue.js and Twilio Client
JavaScript
24
star
40

twiliochat-csharp

C# implementation of Twilio Chat using ASP.NET MVC
JavaScript
23
star
41

airtng-csharp

AirTNG - Rental-by-owner properties fit for a Captain, implemented in C# with .NET MVC
C#
23
star
42

airtng-laravel

AirTNG - Rental-by-owner properties fit for a Captain, implemented in PHP with Laravel
PHP
23
star
43

essentials-messaging-voice-course

Accompanies the upcoming Twilio Essentials - Programmable Messaging and Programmable Voice course
JavaScript
22
star
44

twiliochat-laravel

Laravel implementation of Twilio Chat
PHP
22
star
45

voice-javascript-sdk-quickstart-python

JavaScript
22
star
46

video-access-token-server-python

Twilio Video server-side web app in Python
Python
21
star
47

client-quickstart-php

Twilio Client Quickstart App
JavaScript
21
star
48

browser-calls-rails

A sample application which shows you how to make and receive phone calls with a browser and Twilio Client
Ruby
21
star
49

sms-verification-android-node

SMS Verification for Android with Node.js
JavaScript
20
star
50

ivr-phone-tree-laravel

Create an IVR phone tree with PHP and Laravel 5
PHP
19
star
51

clicktocall-laravel

Laravel version of click to call demo
PHP
19
star
52

airtng-flask

AirTNG - Rental-by-owner properties fit for a Captain, implemented in Python with Flask
Python
19
star
53

account-verification-node

example app demonstrating account verification in Node using Authy
HTML
19
star
54

mediastreams-consume-websockets-flask

Tutorial for using Twilio Media Streams
Python
19
star
55

call-tracking-django

Use Twilio to track the effectiveness of your different marketing campaigns.
Python
19
star
56

on-demand-superclass

Twilio On-Demand Superclass README
JavaScript
18
star
57

sdk-starter-ruby

Demo application showcasing Twilio API usage in Ruby
JavaScript
17
star
58

anonymous-communications-rails

(AirTng: Part 2) Anonymous Communications use-case in Rails
Ruby
17
star
59

talkin-cedric-node

An example application using Media Streams and Amazon Transcribe
JavaScript
17
star
60

starter-node-express

A Twilio starter project for Node.js and Express
JavaScript
17
star
61

video-access-token-server-ruby

Twilio Video server-side web app in Ruby
Ruby
17
star
62

automated-survey-spring

Java Spring implementation of automated survey
Java
16
star
63

automated-survey-flask

A sample app for automated phone surveys with Twilio, TwiML, Python and Flask
Python
16
star
64

verify-v2-quickstart-rails

JavaScript
16
star
65

sdk-starter-python

Demo application showcasing Twilio API usage in Python
JavaScript
16
star
66

voice-javascript-sdk-quickstart-php

JavaScript
16
star
67

appointment-reminders-csharp

Example App and Tutorial content for Appointment Reminders in C# | .NET
C#
16
star
68

ivr-phone-tree-csharp

C#
15
star
69

browser-calls-flask

A sample application which shows you how to make and receive phone calls with a browser and Twilio Client
Python
14
star
70

call-forwarding-node

A sample implementation of advanced call forwarding using Twilio, Node.js and Express.js.
JavaScript
14
star
71

twilio_esp8266_arduino_example

Sending messages in C++ with the ESP8266 and the Arduino IDE
C++
14
star
72

conference-broadcast-laravel

Sample app of a Rapid Response Kit that showcases Twilio Voice conferencing and broadcasting
PHP
14
star
73

sms2fa-flask

SMS Two Factor Authentication implementation with Flask and Twilio
Python
14
star
74

marketing-notifications-node

Demo application showing how to implement SMS notifications (for a mobile marketing use case) in Node.js
HTML
14
star
75

call-forwarding-flask

A sample implementation of advanced call forwarding using Twilio and Flask.
Python
14
star
76

video-access-token-server-csharp

Twilio Video starter application for C# MVC
C#
14
star
77

call-tracking-node

Node.js Call Tracking demo app
JavaScript
13
star
78

authy2fa-csharp

Two Factor Authentication example app in C#
C#
13
star
79

account-security-quickstart-spring

A simple implementation of a Twilio Account Security protected site using Java and Spring.
Java
13
star
80

automated-survey-csharp

Automated surveys in C#
C#
13
star
81

ipm-quickstart-csharp

Starter app for IP Messaging with C# and ASP.NET MVC
JavaScript
12
star
82

sdk-starter-csharp

Demo application showcasing Twilio API usage in C#
HTML
12
star
83

hangouts-clone-react

A clone of Google Hangouts using React and WebRTC
JavaScript
12
star
84

chat-quickstart-swift

Chat Starter Application for iOS, using Swift
Swift
12
star
85

sdk-starter-php

Demo application showcasing Twilio API usage in PHP
JavaScript
12
star
86

browser-calls-csharp

A sample application which shows you how to make and receive phone calls with a browser and Twilio Client
JavaScript
12
star
87

account-security-csharp

Two-Factor Authentication with ASP.NET Core and Authy
C#
11
star
88

call-tracking-laravel

Call tracking demo app in Laravel
PHP
11
star
89

sms2fa-csharp

SMS Two Factor Authentication implementation with ASP.NET and Twilio
C#
11
star
90

verify-v2-quickstart-php

PHP
11
star
91

ivr-recording-laravel

Example App and Tutorial content for IVR Call Recording and Agent Call screening
PHP
11
star
92

survey-rails

Automated Survey application built on Rails
HTML
10
star
93

verify-v2-quickstart-python

JavaScript
10
star
94

verify-v2-quickstart-csharp

JavaScript
10
star
95

developer-fundamentals-notes

Notes for the Developer Fundamentals courses from Twilio
JavaScript
10
star
96

lead-alerts-flask

Instant lead alerts example implemented with Python and Flask
Python
10
star
97

automated-survey-rails

Sample application demonstrating automated voice surveys with Twilio and Ruby on Rails
Ruby
10
star
98

client-quickstart-python

Twilio Client Quickstart App
JavaScript
10
star
99

appointment-reminders-java

demo application implementing appointment reminders use case in Java 8 with the Spark framework
Java
10
star
100

marketing-notifications-flask

Demo application showing how to implement SMS notifications (for a mobile marketing use case) in Python
Python
10
star