📜 ⬆️ ⬇️

Answering machine for calls to Skype for Python

Hello! This topic will be devoted to writing an answering machine for Skype, which will receive calls for you, play the greeting and record the answer.

Given: Skype: 5.0.0.156, WinXPSP3, ActiveState Python 2.6.2.2.

According to the idea, it should work under * nix, but was not tested. We will use Skype4py to interact with Skype through its API. You can download it here , and see the documentation here .

Using Skype4py, you just need to interact with Skype, for our task it is only necessary to register your handlers for various events. We will handle events in our class:
')
class AnsweringMachine: def __init__(self): self.FilesPath = os.getcwd() + '\\' #      self.WavFile = self.FilesPath + 'outofoffice.wav' #   self.IncomingCallsDir = self.FilesPath + 'incoming\\' #   self.time2wait = 30 #   self.callsanswered = dict() #        . 


The OnCall handler accepts incoming calls, in the event that we do not answer the call more than time2wait seconds. At the same time displays additional debugging information. A call to call.Answer is taken in try..except to minimize the code, if honestly, there you need to handle various situations, for example, ending the call to the callers before picking up the handset and others. A file is stored in WavFile , which we will play to all callers:

  def OnCall(self, call, status): if status == Skype4Py.clsRinging and call.Type.startswith('INCOMING'): print 'Incoming call from:', call.PartnerHandle time.sleep(self.time2wait) if call.Duration == 0: try: call.Answer() except: pass self.callsanswered[call.Id] = 'Answered' else: self.callsanswered[call.Id] = 'HumanAnswered' if status == Skype4Py.clsInProgress and self.callsanswered[call.Id] == 'Answered': self.callsanswered[call.Id] = 'Playing' print ' playing ' + self.WavFile call.InputDevice(Skype4Py.callIoDeviceTypeFile, self.WavFile) if status in CallIsFinished: print 'call ',status.lower() 


In OnInputStatusChanged we will catch the completion of the playback of our file and start recording the voice of the caller. All entries will be added to a subfolder with the same name as the user's nickname in the folder IncomingCallsDir . Calling call.InputDevice (Skype4Py.callIoDeviceTypeFile, None) stops playing our file, otherwise the file will be played several times.

  def OnInputStatusChanged(self, call, status): if not status and call.InputDevice().has_key('FILE') and call.InputDevice()['FILE'] == self.WavFile and self.callsanswered[call.Id] == 'Playing': self.callsanswered[call.Id] = 'Recording' print ' play finished' if not os.path.exists(self.IncomingCallsDir + call.PartnerHandle): os.mkdir(self.IncomingCallsDir + call.PartnerHandle) OutFile = self.IncomingCallsDir + call.PartnerHandle + '\\' + time.strftime("%Y-%m-%d_%H-%M-%S") + '.wav' call.InputDevice(Skype4Py.callIoDeviceTypeFile, None) print ' recording ' + OutFile call.OutputDevice(Skype4Py.callIoDeviceTypeFile, OutFile) 


That's all. Below is the entire code:

 #!/usr/bin/python # -*- coding: cp1251 -*- import Skype4Py import sys, time, os CallIsFinished = set ([Skype4Py.clsFailed, Skype4Py.clsFinished, Skype4Py.clsMissed, Skype4Py.clsRefused, Skype4Py.clsBusy, Skype4Py.clsCancelled]); class AnsweringMachine: def __init__(self): self.FilesPath = os.getcwd() + '\\' #      self.WavFile = self.FilesPath + 'outofoffice.wav' #   self.IncomingCallsDir = self.FilesPath + 'incoming\\' #   self.time2wait = 30 #   self.callsanswered = dict() #        . if not os.path.exists(self.IncomingCallsDir): os.mkdir(self.IncomingCallsDir) def OnCall(self, call, status): if status == Skype4Py.clsRinging and call.Type.startswith('INCOMING'): print 'Incoming call from:', call.PartnerHandle time.sleep(self.time2wait) if call.Duration == 0: try: call.Answer() except: pass self.callsanswered[call.Id] = 'Answered' else: self.callsanswered[call.Id] = 'HumanAnswered' if status == Skype4Py.clsInProgress and self.callsanswered[call.Id] == 'Answered': self.callsanswered[call.Id] = 'Playing' print ' playing ' + self.WavFile call.InputDevice(Skype4Py.callIoDeviceTypeFile, self.WavFile) if status in CallIsFinished: print 'call ',status.lower() def OnInputStatusChanged(self, call, status): if not status and call.InputDevice().has_key('FILE') and call.InputDevice()['FILE'] == self.WavFile and self.callsanswered[call.Id] == 'Playing': self.callsanswered[call.Id] = 'Recording' print ' play finished' if not os.path.exists(self.IncomingCallsDir + call.PartnerHandle): os.mkdir(self.IncomingCallsDir + call.PartnerHandle) OutFile = self.IncomingCallsDir + call.PartnerHandle + '\\' + time.strftime("%Y-%m-%d_%H-%M-%S") + '.wav' call.InputDevice(Skype4Py.callIoDeviceTypeFile, None) print ' recording ' + OutFile call.OutputDevice(Skype4Py.callIoDeviceTypeFile, OutFile) def OnAttach(status): global skype print 'Attachment status: ' + skype.Convert.AttachmentStatusToText(status) if status == Skype4Py.apiAttachAvailable: skype.Attach() def main(): global skype am = AnsweringMachine() skype = Skype4Py.Skype() skype.OnAttachmentStatus = OnAttach skype.OnCallStatus = am.OnCall skype.OnCallInputStatusChanged = am.OnInputStatusChanged if not skype.Client.IsRunning: print 'Starting Skype..' skype.Client.Start() print 'Connecting to Skype process..' skype.Attach() print 'Waiting for incoming calls' try: while True: time.sleep(1) except: pass if __name__ == '__main__': main() 


UPD : fixed two bugs in logic, the answering machine was also activated for outgoing calls and turned on if the receiver was picked up by a person.

Source: https://habr.com/ru/post/111404/


All Articles