Module autoanswer |

# Source Code for Module autoanswer

      1  print('****************************************************************************');
      2  print('SkypeKit Python Wrapper Tutorial: Picking Up Incoming Calls');
      3  print('****************************************************************************');
      4

      5  # This example demonstrates, how to:

      6  # 1. Pick up incoming voice calls

      7

      8  # NB! You will need to launch the SkypeKit runtime before running this example.

      9

     10  #----------------------------------------------------------------------------------

     11  # Importing necessary libraries. Note that you will need to set the keyFileName value

     12  # in the keypair.py file.

     13

     14  import sys;
     15  import keypair;
     16  from time import sleep;
     17

     18  sys.path.append(keypair.distroRoot + '/ipc/python');
     19  sys.path.append(keypair.distroRoot + '/interfaces/skype/python');
     20

     21  try:
     22          import Skype;
     23  except ImportError:
     24      raise SystemExit('Program requires Skype and skypekit modules');
     25

     26  #----------------------------------------------------------------------------------

     27  # Taking skypename and password arguments from command-line.

     28

     29  if len(sys.argv) != 3:
     30          print('Usage: python autoanswer.py <skypename> <password>');
     31          sys.exit();
     32

     33  accountName = sys.argv[1];
     34  accountPsw  = sys.argv[2];
     35  loggedIn        = False;
     36

     37  #----------------------------------------------------------------------------------

     38  # Defining our own Account property change callback and assigning it to the

     39  # Skype.Account class.

     40

     41 -def AccountOnChange (self, property_name):
     42          global loggedIn;
     43          if property_name == 'status':
     44                  if self.status == 'LOGGED_IN':
     45                          loggedIn = True;
     46                          print('Login complete.');
     47

     48  Skype.Account.OnPropertyChange = AccountOnChange;
     49

     50  #----------------------------------------------------------------------------------

     51  # The first thing to do here is actually related to our upcoming Skype class.

     52  # We will need some way to determine whether we already have a live conversation

     53  # up in the system. We also need to have a way to access that live conversation.

     54  # The most reasonable place for those is the Skype module, so we can reference

     55  # them later as MySkype.isLiveSessionUp and MySkype.liveSession (Conversation object).

     56  #

     57  # The main reason we need the liveSession variable is that we want to keep getting

     58  # OnPropertyChange events for this object even when the conversation goes non-live.

     59

     60  Skype.Skype.isLiveSessionUp = False;
     61  Skype.Skype.liveSession = 0;
     62

     63  #----------------------------------------------------------------------------------

     64  # The next thing will be to add our own Conversation method for picking up calls.

     65  # We will be picking up calls from two separate places:

     66  # 1. Conversation.OnPropertyChange when local_livestatus goes 'RINGING_FOR_ME'

     67  # 2. SkyLib.OnConversationListChange when local_livestatus goes 'RINGING_FOR_ME'

     68  # So it makes sense to have the code surrounding the standard Conversation.JoinLiveSession

     69  # all in one place.

     70

     71 -def PickUpCall(self):
     72          participants = self.GetParticipants('OTHER_CONSUMERS');
     73          partListStr = '';
     74          for p in participants:
     75                  partListStr = partListStr + ' ' + p.identity;
     76

     77          print('Incoming call from: ' + partListStr);
     78

     79          if MySkype.isLiveSessionUp:
     80                  print('We already have a live session. Cannot have two at the same time.');
     81          else:
     82                  self.JoinLiveSession('');
     83                  MySkype.liveSession = self;
     84

     85  Skype.Conversation.PickUpCall = PickUpCall;
     86

     87  #----------------------------------------------------------------------------------

     88  # Conversation.OnPropertyChange event will only fire when the corresponding

     89  # Conversation object is already created. This means that we will not get

     90  # this event when the object is created as a -result- of a property change.

     91  # The -first- Conversation.local_livestatus == 'RINGING_FOR_ME' will -not-

     92  # fire for us, because we only get the conversation object as a result of this

     93  # property change, in Skype.OnConversationListChange callback.

     94

     95 -def ConversationOnChange (self, property_name):
     96

     97          if property_name == 'local_livestatus':
     98

     99                  if self.local_livestatus == 'RINGING_FOR_ME':
    100                          print(self.displayname + ' RING! RING! (from Conversation.OnPropertyChange)');
    101                          self.PickUpCall();
    102

    103                  if self.local_livestatus == 'IM_LIVE':
    104                          MySkype.isLiveSessionUp = True;
    105                          MySkype.liveSession = self;
    106                          print(self.displayname + ' is now live.');
    107

    108                  if self.local_livestatus == 'RECENTLY_LIVE':
    109                          MySkype.isLiveSessionUp = False;
    110                          print(self.displayname + ' is no longer live.');
    111

    112  Skype.Conversation.OnPropertyChange = ConversationOnChange;
    113

    114  #----------------------------------------------------------------------------------

    115  # Skype.Skype (basically our Skype class)

    116  # Now, Skype.OnConversationListChange -will- fire on the first incoming call,

    117  # as we already have a Skype object and OnConversationListChange is a method of that

    118  # class. So the first incoming call in any given conversation we can pick up here,

    119  # when the local_livestatus goes 'RINGING_FOR_ME'.

    120  #

    121  # The problem here is that if the call drops and immediately after that, another call

    122  # comes in from the same conversation - the OnConversationListChange event will not

    123  # fire again. The reason is that conversations remain in the LIVE_CONVERSATIONS list

    124  # for several seconds after the call goes down. Solution is to keep the conversation

    125  # object somewhere (in our case the SkyLib.liveSession variable) so that OnPropertyChange

    126  # callback will fire, when the second call comes. Then we can pick the call up from there.

    127

    128 -def SkypeOnConversationListChange (self, conversation, type, added):
    129

    130          if type == 'LIVE_CONVERSATIONS':
    131

    132                  if conversation.local_livestatus == 'RINGING_FOR_ME':
    133                          print(conversation.displayname + ' RING! RING! (from Skype.OnConversationListChange).');
    134                          conversation.PickUpCall();
    135

    136                  if added == True:
    137                          MySkype.liveSession = conversation;
    138                          print(conversation.displayname + ' added to live list.');
    139

    140                  if added == False:
    141                          MySkype.liveSession = 0;
    142                          print(conversation.displayname + ' is no longer in live list.');
    143

    144  Skype.Skype.OnConversationListChange = SkypeOnConversationListChange;
    145

    146  #----------------------------------------------------------------------------------

    147  # Creating our main Skype object

    148

    149  try:
    150          MySkype = Skype.GetSkype(keypair.keyFileName);
    151          MySkype.Start();
    152  except Exception:
    153          raise SystemExit('Unable to create Skype instance');
    154

    155  #----------------------------------------------------------------------------------

    156  # Retrieving account and logging in with it. Then waiting in loop.

    157

    158  account = MySkype.GetAccount(accountName);
    159

    160  print('Logging in with ' + accountName);
    161  account.LoginWithPassword(accountPsw, False, False);
    162

    163  while loggedIn == False:
    164          sleep(1);
    165

    166  print('Now accepting incoming calls..');
    167  print('Press ENTER to exit');
    168  raw_input('');
    169

    170  if MySkype.liveSession != 0:
    171          # Leaving the live session before calling Skype.Stop() is mandatory.

    172          # Otherwise the runtime will exit with an error.

    173          print('There is a live conversation up, dropping it before exit.');
    174          MySkype.liveSession.LeaveLiveSession();
    175

    176  print('Exiting..');
    177  MySkype.stop();
    178