Created
May 5, 2011 18:30
-
-
Save mattwilliamson/957603 to your computer and use it in GitHub Desktop.
Freeswitch twisted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys, os.path | |
import eventsocket | |
from twisted.internet import defer, protocol | |
from twisted.application import service, internet | |
from models.user import User | |
class FreeswitchProtocol(eventsocket.EventProtocol): | |
@defer.inlineCallbacks | |
def connectionMade(self): | |
self.pin = [] | |
data = yield self.connect() | |
yield self.myevents() | |
yield self.answer() | |
soundfile = "/usr/local/freeswitch/sounds/alpha/sample2.aiff" | |
yield self.playback(soundfile) | |
@defer.inlineCallbacks | |
def onDtmf(self, ev): | |
self.pin.append(ev.DTMF_Digit) | |
if len(self.pin) == 10: | |
long_pin = ''.join(self.pin) | |
users = yield User.filter(User.long_pin == long_pin).execute() | |
if len(users): | |
soundfile = "/usr/local/freeswitch/sounds/alpha/correct.aiff" | |
yield self.playback(soundfile) | |
yield self.hangup() | |
else: | |
self.pin = [] | |
soundfile = "/usr/local/freeswitch/sounds/alpha/incorrect.aiff" | |
yield self.playback(soundfile) | |
def onChannelExecuteComplete(self, ev): | |
pass | |
def onChannelHangup(self, ev): | |
start_usec = float(ev.Caller_Channel_Answered_Time) | |
end_usec = float(ev.Event_Date_Timestamp) | |
duration = (end_usec - start_usec) / 1000000.0 | |
print "%s hung up: %s (call duration: %0.2f)" % \ | |
(ev.variable_presence_id, ev.Hangup_Cause, duration) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment