-
Notifications
You must be signed in to change notification settings - Fork 0
/
bender_skype.py
79 lines (58 loc) · 2.02 KB
/
bender_skype.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from Skype4Py import skype
from Skype4Py.enums import cmsReceived, chatTypeDialog, chatTypeLegacyDialog
from bender.decorators import backbone_start, backbone_shutdown
class BenderSkype(object):
def __init__(self):
self.on_message_received = None
self._skype = None
@backbone_start
def start(self):
self._skype = _skype = skype.Skype()
_skype.RegisterEventHandler(
'MessageStatus',
self._on_message_status,
)
_skype.RegisterEventHandler(
'UserAuthorizationRequestReceived',
self._on_authorization_request,
)
_skype.Attach()
for user in _skype.UsersWaitingAuthorization:
self._on_authorization_request(user)
@backbone_shutdown
def shutdown(self):
del self._skype
def _on_message_status(self, msg, status):
'''
Invoked whenever :meth:`Skype4Py.skype.SkypeEvents.MessageStatus`
is triggered.
Only received messages will be handled by :class:`Bot`.
.. seealso:: :class:`Skype4Py.utils.EventHandlingBase`
'''
# Handling only received messages.
if status != cmsReceived:
return
# Handling only 1-to-1 dialogs.
if msg.Chat.Type not in (chatTypeDialog, chatTypeLegacyDialog):
return
msg.MarkAsSeen()
msg = SkypeMessage(msg)
self.on_message_received(msg)
def _on_authorization_request(self, user):
'''
Invoked whenever
:meth:`Skype4Py.skype.SkypeEvents.UserAuthorizationRequestReceived`
is triggered.
Automatically accept friend request.
.. seealso:: :class:`Skype4Py.utils.EventHandlingBase`
'''
user.IsAuthorized = True
class SkypeMessage(object):
def __init__(self, msg):
self._msg = msg
def get_body(self):
return self._msg.Body
def reply(self, message):
self._msg.Chat.SendMessage(message)
def get_sender(self):
return self._msg.FromDisplayName