Module chat

Source Code for Module chat

 1  print('****************************************************************************'); 
 2  print('SkypeKit Python Wrapper Tutorial: Sending and Receiving Chat Messages'); 
 3  print('****************************************************************************'); 
 4  
 
 5  # This example demonstrates, how to:
 
 6  # 1. Detect incoming text messages.
 
 7  # 2. Post text messages into a conversation.
 
 8  
 
 9  # NB! You will need to launch the SkypeKit runtime before running this example.
 
10  
 
11  #----------------------------------------------------------------------------------
 
12  # Importing necessary libraries. Note that you will need to set the keyFileName value
 
13  # in the keypair.py file.
 
14  
 
15  import sys; 
16  import keypair; 
17  from time import sleep; 
18  
 
19  sys.path.append(keypair.distroRoot + '/ipc/python'); 
20  sys.path.append(keypair.distroRoot + '/interfaces/skype/python'); 
21  
 
22  try: 
23          import Skype; 
24  except ImportError: 
25      raise SystemExit('Program requires Skype and skypekit modules'); 
26  
 
27  #----------------------------------------------------------------------------------
 
28  # Taking skypename and password arguments from command-line.
 
29  
 
30  if len(sys.argv) != 3: 
31          print('Usage: python chat.py <skypename> <password>'); 
32          sys.exit(); 
33  
 
34  accountName = sys.argv[1]; 
35  accountPsw  = sys.argv[2]; 
36  loggedIn        = False; 
37  
 
38  #----------------------------------------------------------------------------------
 
39  # To get the Skype instance to react to the incoming chat messages, we need to 
 
40  # assign The Skype class a custom OnMessage callback handler. The OnMessage callback
 
41  # is conveniently equipped with conversation argument, so we can use that to send
 
42  # out an automated reply from inside OnMessage immediately.
 
43  #
 
44  # To display the text of chat messages, we can use the message.body_xml property.
 
45  # In case of plain text messages, there is no actual XML in it. For special messages,
 
46  # such as conversation live status changes or file transfer notifications, your UI
 
47  # will need to parse the body_xml property. This also goes for incoming messages that 
 
48  # contain smileys (try it!) and flag icons.
 
49  
 
50 -def OnMessage(self, message, changesInboxTimestamp, supersedesHistoryMessage, conversation):
51 if message.author != accountName: 52 print(message.author_displayname + ': ' + message.body_xml); 53 conversation.PostText('Automated reply.', False);
54 55 Skype.Skype.OnMessage = OnMessage; 56 57 try: 58 MySkype = Skype.GetSkype(keypair.keyFileName); 59 MySkype.Start(); 60 except Exception: 61 raise SystemExit('Unable to create skype instance'); 62 63 64 #---------------------------------------------------------------------------------- 65 # Defining our own Account property change callback and assigning it to the 66 # Skype.Account class. 67
68 -def AccountOnChange (self, property_name):
69 global loggedIn; 70 if property_name == 'status': 71 if self.status == 'LOGGED_IN': 72 loggedIn = True; 73 print('Login complete.');
74 75 Skype.Account.OnPropertyChange = AccountOnChange; 76 77 #---------------------------------------------------------------------------------- 78 # Retrieving account and logging in with it. 79 80 account = MySkype.GetAccount(accountName); 81 82 print('Logging in with ' + accountName); 83 account.LoginWithPassword(accountPsw, False, False); 84 85 while loggedIn == False: 86 sleep(1); 87 88 print('Now accepting incoming chat messages.'); 89 print('Press ENTER to quit.'); 90 raw_input(''); 91 print('Exiting..'); 92 MySkype.stop(); 93