Skip to content

Instantly share code, notes, and snippets.

@volodymyrsmirnov
Last active January 28, 2017 04:02
Show Gist options
  • Save volodymyrsmirnov/10264813 to your computer and use it in GitHub Desktop.
Save volodymyrsmirnov/10264813 to your computer and use it in GitHub Desktop.
SIP Wav Caller CLI
#/usr/bin/env python
import sys
from application.notification import NotificationCenter
from sipsimple.account import AccountManager
from sipsimple.application import SIPApplication
from sipsimple.core import SIPURI, ToHeader
from sipsimple.lookup import DNSLookup, DNSLookupError
from sipsimple.storage import FileStorage
from sipsimple.session import Session
from sipsimple.streams import AudioStream
from sipsimple.audio import WavePlayer
from sipsimple.threading.green import run_in_green_thread
from threading import Event
class WavCaller(SIPApplication):
def __init__(self, callee, wav_file):
SIPApplication.__init__(self)
print "Placing call to", callee
self.ended = Event()
self.callee = callee
self.session = None
self.wav_file = wav_file
notification_center = NotificationCenter()
notification_center.add_observer(self)
self.start(FileStorage("config"))
@run_in_green_thread
def _NH_SIPApplicationDidStart(self, notification):
callee = ToHeader(SIPURI.parse(self.callee))
routes = DNSLookup().lookup_sip_proxy(callee.uri, ("udp", "tcp", "tls")).wait()
account = AccountManager().default_account
self.session = Session(account)
self.session.connect(callee, routes, [AudioStream()])
def _NH_SIPSessionGotRingIndication(self, notification):
print "Connected, ringing in progress"
def _NH_SIPSessionDidStart(self, notification):
print "Phone being picked up, playing", self.wav_file
audio_stream = notification.data.streams[0]
audio = WavePlayer(SIPApplication.voice_audio_mixer, self.wav_file, loop_count=0)
audio_stream.bridge.add(audio)
audio.start()
def _NH_SIPSessionDidFail(self, notification):
self.stop()
def _NH_SIPSessionDidEnd(self, notification):
self.stop()
def _NH_SIPApplicationDidEnd(self, notification):
self.ended.set()
caller.session.end()
if __name__ == "__main__":
if len(sys.argv) >= 3:
target_uri = "sip:{0}@sip.zadarma.com".format(sys.argv[1])
caller = WavCaller(target_uri, format(sys.argv[2]))
caller.ended.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment