forked from prehensile/skypebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skypebot.py
63 lines (51 loc) · 1.58 KB
/
skypebot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import Skype4Py
import time
from commands import drinkcommand, wetcommand
import datetime
class ChatHandler(object):
def __init__( self, Chat ):
self.chat = Chat
self.last_timestamp = datetime.datetime.now()
def update( self ):
ts = datetime.datetime.now()
new_messages = []
messages = self.chat.RecentMessages
for message in messages:
dt = message.Datetime
if dt > self.last_timestamp:
new_messages.append( message )
self.last_timestamp = dt
return new_messages
def main():
skype = Skype4Py.Skype(Transport='x11')
skype.Attach()
chat_handlers = {}
command_mappings = {}
command_mappings[ "drink" ] = drinkcommand.DrinkCommand()
command_mappings[ "w3t" ] = wetcommand.WetCommand()
while 1:
# maintain list of chats
chats = skype.Chats
for chat in chats:
chat_name = chat.Name
if chat_name not in chat_handlers:
print "New handler for chat: %s" % chat.FriendlyName
chat_handlers[chat_name] = ChatHandler(chat)
# TODO: clear defunct chats
# update chats
for chat_name in chat_handlers:
chat_handler = chat_handlers[ chat_name ]
new_messages = chat_handler.update()
if len(new_messages)> 0:
print "New messages in chat: %s" % chat_handler.chat.FriendlyName
for message in new_messages:
body = message.Body
print body
for commandstring in command_mappings:
if "!" + commandstring in body.lower():
command = command_mappings[ commandstring ]
message_out = command.execute( message )
if message_out is not None:
chat_handler.chat.SendMessage( message_out )
time.sleep( 1 )
main()