pycall Documentation Release Randall Degges

Similar documents
flask-dynamo Documentation

contribution-guide.org Release

Unifer Documentation. Release V1.0. Matthew S

TPS Documentation. Release Thomas Roten

How To Get Your Word Document. Ready For Your Editor

django-dynamic-db-router Documentation

nacelle Documentation

One of the fundamental kinds of websites that SharePoint 2010 allows

Switchvox PBX User Manual

Signals Documentation

RIPE Atlas Cousteau Documentation

Customizing DAZ Studio

If Statements, For Loops, Functions

Project 1 Balanced binary

Introduction to Programming

Python Project Documentation

Intro. Scheme Basics. scm> 5 5. scm>

User Guide Get Started Manage Your Inbound Cal Features Using Schedules Find Administrators and Contacts

pytest-benchmark Release 2.5.0

CLIENT ONBOARDING PLAN & SCRIPT

Xchange for Samsung MAC User Guide. Version 2.4

CLIENT ONBOARDING PLAN & SCRIPT

Lutheran High North Technology The Finder

Python StatsD Documentation

1 Installation (briefly)

Learn Linux in a Month of Lunches by Steven Ovadia

Sucuri Webinar Q&A HOW TO IDENTIFY AND FIX A HACKED WORDPRESS WEBSITE. Ben Martin - Remediation Team Lead

Django Standalone Apps

CONVERSION TRACKING PIXEL GUIDE

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

4Sight for Mac User Guide. Version 2.4

Creating a Phone Survey

Python StatsD Documentation

COPYRIGHTED MATERIAL. Starting Strong with Visual C# 2005 Express Edition

XP: Backup Your Important Files for Safety

Python Schema Generator Documentation

shortcut Tap into learning NOW! Visit for a complete list of Short Cuts. Your Short Cut to Knowledge

Nirvana Documentation

NWIC EDITOR GUIDE August 2016

Django Test Utils Documentation

Creating a Survey COMMUNICATE. West Corporation. 100 Enterprise Way, Suite A-300 Scotts Valley, CA

dj-libcloud Documentation

git-pr Release dev2+ng5b0396a

HeroBase Administrator Training Guide 101

Ruby on Rails Welcome. Using the exercise files

Avaya Unified Messenger Telephone User Interface Online Guide

Exercise 6 - Addressing a Message

1. You re boring your audience

Moving from FrameMaker to Blaze: Best Practices

Using GitHub to Share with SparkFun a

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Lab 08. Command Line and Git

Shouldly Documentation

Managing your PBX- Administrator

Python wrapper for Viscosity.app Documentation

Evaluator Pro: Irrigation

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey.

CSC209. Software Tools and Systems Programming.

The RestructuredText Book Documentation

chatterbot-weather Documentation

Welcome Back! Without further delay, let s get started! First Things First. If you haven t done it already, download Turbo Lister from ebay.

Allo ISDN BRI CB200PD and Elastix Server

Python simple arp table reader Documentation

BT Unified Communicator Release 2 Client User Guide RV1.1. BT Unified Communicator Client User Guide UCV2 R2 V1.1

Frontier Documentation

Mantis STIX Importer Documentation

Python data pipelines similar to R Documentation

Lotusphere IBM Collaboration Solutions Development Lab

yardstick Documentation

lazy-object-proxy Release 1.3.1

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications

Text Input and Conditionals

Roman Numeral Converter Documentation

A Step-by-Step Guide to Survey Success

Django-CSP Documentation

ASCII Art. Introduction: Python

Dragon Mapper Documentation

Quick.JS Documentation

Design News Gadget Freak. AutoNag reminder system. Background: Server OS Installation:

Keeping Sane - Managing your

Copyright 2000: PictureTel Corporation Printed in U.S.A. PictureTel Corporation, 100 Minuteman Road, Andover, MA

Human-Computer Interaction Design

tinycss Documentation

smartfilesorter Documentation

CS354 gdb Tutorial Written by Chris Feilbach

InDesign UX Design Patterns. by Justin Putney

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

Getting Started Reliance Communications, Inc.

withenv Documentation

Content Curation Mistakes

2016 All Rights Reserved

django-embed-video Documentation

Lastly, in case you don t already know this, and don t have Excel on your computers, you can get it for free through IT s website under software.

Launch Store. University

This session will provide an overview of the research resources and strategies that can be used when conducting business research.

Project Collaboration

Craigslist Quick Tricks Manual helping you get your ads on Craigslist and getting people to click through to your site or product offer

QUICK START GUIDE NTS HOSTED PBX CALL MANAGER. Welcome. Getting Oriented

Google Analytics 101

Karlen Communications

Transcription:

pycall Documentation Release 2.3.2 Randall Degges Sep 28, 2017

Contents 1 Foreword 3 1.1 What is Asterisk?............................................. 3 1.2 What Are Call Files?........................................... 3 1.3 Why Should I Use pycall?........................................ 3 1.4 Target Audience............................................. 4 2 Installation 5 3 Usage 7 3.1 Preparation................................................ 7 3.2 Hello, World!............................................... 7 3.3 Scheduling a Call in the Future..................................... 8 3.4 Setting Call File Permissions....................................... 9 3.5 Adding Complex Call Logic....................................... 10 3.6 Setting a CallerID............................................ 10 3.7 Passing Variables to Your Dial Plan................................... 10 3.8 Track Your Calls with Asterisk Account Codes............................. 11 3.9 Specify Call Timing Values....................................... 11 3.10 Archiving Call Files........................................... 11 3.11 Dealing with Non-Standard Asterisk Installs.............................. 11 4 Developing 13 4.1 Project Goals............................................... 13 4.2 Code Organization............................................ 13 4.3 Tests................................................... 14 4.4 Code Style................................................ 14 4.5 Documentation.............................................. 14 4.6 Development Tracker.......................................... 14 4.7 Submitting Code............................................. 14 i

ii

Welcome to pycall s documentation. This documentation is divided into several different parts, and can be read through like a book. If you re relatively new to telephony, you may want to start at the Foreword, and read through to the end; otherwise, you can jump right into the Usage docs. If you want to contribute code to the project, please read the entire Developing section before diving into the code. Contents 1

2 Contents

CHAPTER 1 Foreword Thanks for checking out pycall. pycall is a simple python wrapper for creating Asterisk call files. If you re familiar with the Asterisk PBX system, and are building an application that needs to automatically place outbound calls, pycall may be just what you need! What is Asterisk? Asterisk is a popular open source PBX (phone system) which many businesses and hobbyists use for placing and receiving phone calls. Asterisk is also used for a wide variety of other purposes, all relating to telephony and communications. If you ve never used Asterisk, head over to the Asterisk website for more information. What Are Call Files? Since pycall is a python library for creating and using Asterisk call files, you may be wondering what a call file is. Call files are specially formatted text files that Asterisk uses to initiate outbound calls automtically. In a nutshell, Asterisk call files allow developers to automatically generate calls and launch programs through Asterisk, without any sort of complex network API. To learn more about call files, head over to the VoIP Info call files page. Why Should I Use pycall? There are lots of reasons why you should use pycall. I could just be vague and leave it at that, but you want to real reasons right? Simple pycall makes building telephony applications easy. 3

Flexible pycall gives you a lot of flexibility. If you re Asterisk environment is setup in a non-standard way, pycall won t mind. Well Documented pycall has great documentation (but you already know that by now, right?) and active development. Target Audience pycall is intended for usage by telephony developers who are building Asterisk applications. It has sensible defaults, and is simple to implement into any application. If you re familiar with python programming, and want to write some telephony software, give pycall a try and you won t be disappointed! 4 Chapter 1. Foreword

CHAPTER 2 Installation pycall can be installed just like any other python program, through pypi. To install it, simply run: $ pip install pycall If you d like to install the latest development version of pycall (not recommended), then download the latest release and run: $ python setup.py install If you re totally unfamiliar with installing python packages, you should probably read the official tutorial before continuing. 5

6 Chapter 2. Installation

CHAPTER 3 Usage Integrating pycall into your project is quick and easy! After reading through the sections below, you should be able to integrate pycall into your project, and understand what it can and cannot be used for. Preparation The rest of this guide assumes you have the following: 1. A working Asterisk server. 2. Some sort of PSTN (public switch telephone network) connectivity. Regardless of what sort of PSTN connection you have (SIP / DAHDI / ZAPTEL / ISDN / etc.), as long as you can make calls, you re fine. For simplicity s sake, I m going to assume for the rest of this guide that you have a SIP trunk named flowroute defined. Hello, World! pycall allows you to build applications that automate outbound calling. In the example below, we ll call a phone number specified on the command line, say hello world, then hang up! import sys from pycall import CallFile, Call, Application def call(number): c = Call('SIP/flowroute/%s' % number) a = Application('Playback', 'hello-world') cf = CallFile(c, a) cf.spool() if name == ' main ': call(sys.argv[1]) 7

Just save the code above in a file named call.py and run it with python! $ python call.py 18002223333 Assuming your Asterisk server is setup correctly, your program just placed a call to the phone number 18002223333, and said hello world to the person who answered the phone! Code Breakdown 1. First we imported the pycall classes. The CallFile class allows us to make call files. The Call class stores information about a specific call, and the Application class lets us specify an Asterisk application as our Action. Every call file requires some sort of action (what do you want to do when the caller answers?). 2. Next, we build a Call object, and specify the phone number to call in standard Asterisk format. This tells Asterisk who to call. Note: In this example we made a call out of a SIP trunk named flowroute, but you can specify any sort of dial string in its place. You can even tell Asterisk to call multiple phone numbers at once by separating your dial strings with the & character (eg: Local/1000@internal&SIP/flowroute/18002223333). 3. Then we build an Application object that tells Asterisk what to do when the caller answers our call. In this case, we tell Asterisk to run the Playback command, and pass the argument hello-world to it. Note: The name hello-world here refers to one of the default Asterisk sound files that comes with all Asterisk installations. This file can be found in the directory /var/lib/asterisk/sounds/en/ on most systems. 4. Finally, we create the actual CallFile object, and run its spool() method to have Asterisk make the call. Scheduling a Call in the Future Let s say you want to have Asterisk make a call at a certain time in the future no problem. The spool() method allows you to specify an optional datetime object to tell Asterisk when you want the magic to happen. In this example, we ll tell Asterisk to run the call in exactly 1 hour: import sys from datetime import datetime from datetime import timedelta from pycall import CallFile, Call, Application def call(number, time=none): c = Call('SIP/flowroute/%s' % number) a = Application('Playback', 'hello-world') cf = CallFile(c, a) cf.spool(time) if name == ' main ': call(sys.argv[1], datetime.now()+timedelta(hours=1)) Note: If you specify a value of None, the call file will be ran immediately. 8 Chapter 3. Usage

Just for the heck of it, let s look at one more code snippet. This time we ll tell Asterisk to run the call file at exactly 1:00 AM on December 1, 2010. import sys from datetime import datetime from pycall.callfile import CallFile def call(number, time=none): c = Call('SIP/flowroute/%s' % number) a = Application('Playback', 'hello-world') cf = CallFile(c, a) cf.spool(time) if name == ' main ': call(sys.argv[1], datetime(2010, 12, 1, 1, 0, 0)) Setting Call File Permissions In most environments, Asterisk is installed and ran as the user / group asterisk, which often poses a problem if your application doesn t run under the asterisk user account. pycall recognizes that this is a frustrating problem to deal with, and provides three mechanisms for helping make permissions as painless as possible: the user attribute, the NoUserError, and the NoUserPermissionError. The user attribute lets you specify a system username that your call file should be ran as. For example, if your application is running as root, you could say: cf = CallFile(c, a, user='asterisk') and pycall would chown the call file to the asterisk user before spooling. If you specify the user attribute, but the user doesn t exist, pycall will raise the NoUserError so you know what s wrong. Lastly, if your application doesn t have the proper permissions to change the ownership of your call file, pycall will raise the NoUserPermissionError. As an example, here we ll change the call file permissions so that Asterisk can actually read our call file: import sys from pycall import CallFile, Call, Application def call(number): c = Call('SIP/flowroute/%s' % number) a = Application('Playback', 'hello-world') cf = CallFile(c, a, user='asterisk') cf.spool(time) if name == ' main ': call(sys.argv[1]) Note: If you run this code on a system that doesn t have Asterisk installed, you will most likely get a NoUserError since pycall won t be able to find the asterisk user that it s trying to grant permissions to. 3.4. Setting Call File Permissions 9

Adding Complex Call Logic Most applications you write will probably be a bit more complex than hello world. In the example below, we ll harness the power of the Context class to instruct Asterisk to run some custom dial plan code after the caller answers. from pycall import CallFile, Call, Context c = Call('SIP/flowroute/18002223333') con = Context('survey', 's', '1') cf = CallFile(c, con) cf.spool() For example purposes, let s assume that somewhere in your Asterisk extensions.conf file there exists some dial plan in a context labeled survey. After the caller answers our call, Asterisk will immediately jump to the dial plan code we ve specified at survey,s,1 and start executing as much logic as desired. Setting a CallerID A lot of the time, you ll want to force Asterisk to assume a specific caller ID when making outbound calls. To do this, simply specify a value for the callerid attribute: c = Call('SIP/flowroute/18002223333', callerid="'test User' <5555555555>'") Now, when Asterisk makes your call, the person receiving the call (depending on their phone and service type) should see a call coming from Test User who s phone number is 555-555-5555! Passing Variables to Your Dial Plan Often times, when building complex applications, you ll want to pass specific data from your application to Asterisk, so that you can read the information later. The example below will pass some information to our Asterisk dial plan code, so that it can use the information in our call. from pycall import CallFile, Call, Context vars = {'greeting': 'tt-monkeys'} c = Call('SIP/flowroute/18882223333', variables=vars) x = Context('survey', 's', '1') cf = CallFile(c, x) cf.spool() And somewhere in our extensions.conf file... [survey] exten => s,1,playback(${greeting}) exten => s,n,hangup() As you can see, our dial plan code can now access the variable greeting and its value. 10 Chapter 3. Usage

Track Your Calls with Asterisk Account Codes Asterisk call files allow you to specify that a certain call should be associated with a certain account. This is mainly useful for logging purposes. This example logs the call with the randall account: c = Call('SIP/flowroute/18002223333', account='randall') Note: For more information on call logs, read the CDR documentation. Specify Call Timing Values pycall provides several ways to control the timing of your calls. 1. wait_time lets you specify the amount of time to wait (in seconds) for the caller to answer before we consider our call attempt unsuccessful. 2. retry_time lets you specify the amount of time to wait (in seconds) between retries. Let s say you try to call the number 1-800-222-3333 but they don t answer, Asterisk will wait for retry_time seconds before calling the person again. 3. max_retries lets you specify the maximum amount of retry attempts (you don t want to call someone forever, do you?). Using these attributes is simple: c = Call('SIP/flowroute/18002223333', wait_time=10, retry_time=60, max_retries=2) Archiving Call Files If, for some reason, you want to archive call files that have already been spooled with Asterisk, just set the archive attribute to True: cf = CallFile(..., archive=true) and Asterisk will copy the call file (with a status code) to the archive directory (typically /var/spool/asterisk/outgoing_done). Dealing with Non-Standard Asterisk Installs If your Asterisk server isn t installed with the defaults, chances are you need to make some changes. pycall provides a ton of flexibility in this regard, so you should have no problems getting things running. Specifying a Specific Name for Call Files If you need to name your call file something special, just specify a value for both the filename and tempdir attributes: 3.8. Track Your Calls with Asterisk Account Codes 11

cf = CallFile(..., filename='test.call', tempdir='/tmp') Note: By default, pycall will randomly generate a call file name. Specifing a Custom Spooling Directory If you re Asterisk install doesn t spool to the default /var/spool/asterisk/outgoing directory, you can override it with the spool_dir attribute: cf = CallFile(..., spool_dir='/tmp/outgoing') 12 Chapter 3. Usage

CHAPTER 4 Developing Contributing code to pycall is easy. The codebase is simple, clear, and tested. All ideas / patches / etc. are welcome. This section covers everything you need to know to contribute code, please give it a full read before submitting patches to the project. Project Goals The main goal of the pycall library is to provide a simple python wrapper for generating Asterisk call files. I d like to keep the project and codebase small and concise. It would be easy to add features to pycall that fall outside of its humble goals however, please resist that urge :) Before submitting patches that add functionality, please consider whether or not the patch contributes to the overall project goals, or takes away from them. Code Organization pycall is broken up into several python modules to keep the code organized. To start reading code, check out the callfile.py module. This contains the main CallFile class that most users interact with. Each CallFile requires an Action to be specified. The idea is that a CallFile should represent the physical call file, whereas an Action should represent the behavior of the call file (what actions should our call file perform if the call is answered?). Actions can be either applications (Application) or contexts (Context). Both applicatoins and contexts correspond to their Asterisk equivalents. Each CallFile must also specify a Call object. Call objects specify the actual call information what number to call, what callerid to use, etc. If there are errors, pycall will raise a custom PycallError exception. The errors are very descriptive, and always point to a solution. 13

Tests pycall is fully tested. The project currently makes use of a full unit test suite to ensure that code works as advertised. In order to run the test suite for yourself, you need to install the python-nose library, then run python setup.py nosetests. If you d like to see the coverage reports, you should also install the coverage.py library. All unit tests are broken up into python modules by topic. This is done to help keep separate test easy to work with. If you submit a patch, please ensure that it doesn t break any tests. If you submit tests with your patch, it makes it much easier for me to review patches and integrate them. Code Style When submitting patches, please make sure that your code follows pycall s style guidelines. The rules are pretty simple: just make your code fit in nicely with the current code! pycall uses tabs instead of spaces, and uses standard PEP8 formatting for everything else. If in doubt, just look at pre-existing code. Documentation One of pycall s goals is to be well documented. Users should be able to quickly see how to use the library, and integrate it into their project within a few minutes. If you d like to contribute documentation to the project, it is certainly welcome. All documentation is written using Sphinx and compiles to HTML and PDF formats. Development Tracker pycall is proudly hosted at Github. If you d like to view the source code, contribute to the project, file bug reports or feature requests, please do so there. Submitting Code The best way to submit code is to fork pycall on Github, make your changes, then submit a pull request. If you re unfamiliar with forking on Github, please read this awesome article. 14 Chapter 4. Developing