Add .users command

This commit is contained in:
drymer 2016-05-13 00:23:23 +02:00
parent 9a06e8ac68
commit 8c523a90d8
1 changed files with 61 additions and 8 deletions

View File

@ -18,13 +18,20 @@ class EchoBot(sleekxmpp.ClientXMPP):
super(EchoBot, self).__init__(jid, password) super(EchoBot, self).__init__(jid, password)
self.add_event_handler('session_start', self.start) self.add_event_handler('session_start', self.start)
self.add_event_handler('groupchat_message', self.muc_message) self.add_event_handler('groupchat_message', self.muc_message)
self.add_event_handler("muc::%s::got_online" % room,
self.muc_online)
self.add_event_handler("muc::%s::got_offline" % room,
self.muc_offline)
self.muc_room = room self.muc_room = room
self.nick = nick self.nick = nick
self.token = token self.token = token
self.xmpp_users = []
# Telegram # Telegram
self.group = group self.group = group
self.bot = telegram.Bot(self.token) self.bot = telegram.Bot(self.token)
self.telegram_users = []
# meter el conecto del tg en un hilo # meter el conecto del tg en un hilo
t = Thread(target=self.read_tg) t = Thread(target=self.read_tg)
@ -47,14 +54,22 @@ class EchoBot(sleekxmpp.ClientXMPP):
if not user: if not user:
user = str(update.message.from_user.first_name) user = str(update.message.from_user.first_name)
mensaje = user + ": " + message msg = user + ": " + message
chat_id = update.message.chat_id chat_id = update.message.chat_id
if message and chat_id == self.group: if message and chat_id == self.group:
self.send_message(mto=self.muc_room, if user not in self.telegram_users:
mbody=mensaje, self.telegram_users.append(user)
mtype='groupchat')
update_id = update.update_id + 1 if message == '.users':
self.say_users('telegram')
else:
self.send_message(mto=self.muc_room,
mbody=msg,
mtype='groupchat')
update_id = update.update_id + 1
except NetworkError: except NetworkError:
sleep(1) sleep(1)
@ -67,9 +82,47 @@ class EchoBot(sleekxmpp.ClientXMPP):
self.plugin['xep_0045'].joinMUC(self.muc_room, self.nick, wait=True) self.plugin['xep_0045'].joinMUC(self.muc_room, self.nick, wait=True)
def muc_message(self, msg): def muc_message(self, msg):
if msg['mucnick'] != self.nick: if msg['body'] == '.users':
mensaje = str(msg['from']).split('/')[1] + ': ' + str(msg['body']) self.say_users('xmpp')
self.bot.sendMessage(self.group, text=mensaje)
elif msg['mucnick'] != self.nick:
message = str(msg['from']).split('/')[1] + ': ' + str(msg['body'])
self.bot.sendMessage(self.group, text=message)
def muc_online(self, presence):
if presence['muc']['nick'] != self.nick:
self.xmpp_users.append(presence['muc']['nick'])
def muc_offline(self, presence):
if presence['muc']['nick'] != self.nick:
self.xmpp_users.remove(presence['muc']['nick'])
def say_users(self, service):
xmpp_users = ""
tg_users = ""
for i in self.xmpp_users:
xmpp_users = xmpp_users + ' _' + i
msg1 = 'XMPP Users:' + xmpp_users
for i in self.telegram_users:
tg_users = tg_users + ' ' + i
if not tg_users:
tg_users = ""
msg2 = 'Telegram Users:' + tg_users
message = msg1 + '\n' + msg2
if service == 'xmpp':
self.send_message(mto=self.muc_room,
mbody=message,
mtype='groupchat')
elif service == 'telegram':
self.bot.sendMessage(self.group, text=message)
if __name__ == '__main__': if __name__ == '__main__':