-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
155 lines (133 loc) · 5.22 KB
/
bot.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import threading
from errbot import BotPlugin, botcmd, arg_botcmd, webhook
from lyncbot import web, ucwa
def check_logged_in(func):
def wrap(self, message, args):
frm = str(message.frm)
if frm not in self.conns:
return """Sorry, you're not logged in yet.
Please log in at http://127.0.0.1:3141/lyncbot"""
return func(self, message, args)
return wrap
class Lyncbot(BotPlugin, web.WebInterface):
"""
Lync (Skype for Business) integration
"""
def activate(self):
super(Lyncbot, self).activate()
self.conns = {}
self.chats = {}
self.current_chat = {}
def deactivate(self):
super(Lyncbot, self).deactivate()
#def get_configuration_template(self):
# return {'EXAMPLE_KEY_1': "Example value",
# 'EXAMPLE_KEY_2': ["Example", "Value"]
# }
def check_configuration(self, configuration):
super(Lyncbot, self).check_configuration(configuration)
def callback_connect(self):
pass
def callback_message(self, message):
frm = str(message.frm)
if frm not in self.conns:
return
if message.body.startswith('!'):
return
message_text = message.body
if message.body.startswith('@'):
dest, message_text = message.body.split(None, 1)
other = self.conns[frm].normalize_contact(dest[1:])
chat = self.chats[frm].get(other)
else:
chat = self.current_chat.get(frm)
if chat is None:
self.send(message.frm, "Sorry - please open a chat first with "
"the !chat command.", in_reply_to=message)
return
chat.send(message_text)
def lync_login(self, chatname, email, password):
try:
u = ucwa.LyncUCWA(email, password)
except:
return False
self.conns[chatname] = u
self.chats[chatname] = {}
# be prepared to accept incoming chat invitations
u.set_invitation_callback(
lambda c: self.add_chat(c, chatname))
# make available
u.set_available()
# launch the event listener in a background thread
u.thread = threading.Thread(target=u.process_events)
u.thread.setDaemon(True)
u.thread.start()
return True
def add_chat(self, chat, to):
self.chats[to][chat.other[0]] = chat
self.current_chat[to] = chat
to_id = self.build_identifier(to)
self.send(to_id, "New conversation from %s:" % (", ".join(chat.other)))
#if chat.invite_message:
# self.send(to_id, chat.invite_message)
chat.set_inbound_callback(
lambda m: self.inbound_chat_message(m, to_id))
def get_from(self, message):
frm = str(message.frm)
if frm not in self.conns:
raise Exception("Sorry, you're not logged in yet. Please log in at http://127.0.0.1:3141/lyncbot")
return frm
@botcmd
def contacts(self, message, args):
"""Displays a list of people to contact."""
frm = self.get_from(message)
status = {
'Online': ':white_check_mark:',
'Offline': ':white_circle:',
'Away': ':large_blue_circle:',
'Busy': ':red_circle:',
'DoNotDisturb': ':no_entry:',
'IdleOnline': ':eight_spoked_asterisk:',
'IdleBusy': ':clock1030:'
}
for c in self.conns[frm].contacts(args or None):
yield "%s %s (%s)" % (status.get(c.contactPresence.availability,
':question:'),
c.name, c.emailAddresses[0])
else:
return "No contacts found" + (" under " + " ".join(args)
if args else "")
@botcmd
def chat_with(self, message, args):
"""Starts a chat session with the desired recipient."""
frm = self.get_from(message)
other = self.conns[frm].normalize_contact(args)
if other in self.chats[frm]:
self.current_chat[frm] = self.chats[frm][other]
return
chat = self.conns[frm].new_conversation([other])
chat.set_inbound_callback(
lambda m: self.inbound_chat_message(m, message.frm))
self.chats[frm][other] = chat
self.current_chat[frm] = chat
return "Go ahead!"
@botcmd
def chat_end(self, message, args):
"""Ends the current chat session or one specified."""
frm = self.get_from(message)
if args:
other = self.conns[frm].normalize_contact(args)
else:
chat = self.current_chat.get(frm)
try:
other = [k for k, v in self.conns[frm].items() if v == chat][0]
except IndexError:
return "No chat open..."
if self.conns[frm][other] == self.current_chat[frm]:
del(self.current_chat[frm])
self.conns[frm][other].close()
del(self.conns[frm][other])
return "Chat with %s closed." % other
def inbound_chat_message(self, message, to):
"""Posts an inbound chat message to the Errbot user."""
self.send(to, message)