Python Skype留守番電話

こんにちは このトピックは、Skype用の留守番電話を書くことに専念します。この留守番電話は、あなたに電話をかけ、挨拶をし、応答を録音します。



指定:Skype:5.0.0.156、WinXPSP3、ActiveState Python 2.6.2.2。



アイデアによると、* nixの下で動作するはずですが、テストされていません。 APIを介してSkypeとやり取りするには、Skype4pyを使用します。 ここからダウンロードでき、 ここでドキュメントを見ることもできます。



Skype4pyを使用すると、Skypeとの対話が非常に簡単になります。このタスクでは、さまざまなイベントのハンドラーを登録するだけです。 クラスでイベントを処理します。



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() #        .
      
      







OnCallハンドラーは、time2wait秒を超えて呼び出しに応答しない場合、着信呼び出しを受け入れます。 同時に追加のデバッグ情報を表示します。 call.Answer呼び出しはtry ..で行われました。 ただし 、コードを最小限に抑えるため、正直に言うと、さまざまな状況を処理する必要があります。たとえば、電話などをピックアップする前に発信者による呼び出しを終了する必要があります。 ファイルWavFileに保存され、すべての呼び出し元に対して再生されます。



  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()
      
      







OnInputStatusChangedで、ファイルの再生の完了をキャッチし、発信者の音声の録音を開始します。 すべてのエントリは、 IncomingCallsDirフォルダー内のユーザーのニックネームと一致する名前でサブフォルダーに追加されます。 call.InputDevice(Skype4Py.callIoDeviceTypeFile、None)を呼び出すと、ファイルの再生が停止します。そうしないと、ファイルが数回再生されます。



  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)
      
      







それだけです。 以下はコード全体です。



 #!/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 :ロジックの2つのバグを修正しました。留守番電話は発信通話でもオンになり、受信者がまだ人に拾われている場合はオンになりました。



All Articles