jabbergram/jabbergram.py

189 lines
5.6 KiB
Python
Raw Normal View History

2016-05-08 16:46:24 +00:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sleekxmpp
import telegram
import configparser
from threading import Thread
from queue import Queue
from telegram.error import NetworkError, Unauthorized
from time import sleep
from sys import argv
from sys import exit
2016-05-08 16:46:24 +00:00
class EchoBot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, rooms, nick, token, groups):
2016-05-08 16:46:24 +00:00
# XMPP
super(EchoBot, self).__init__(jid, password)
self.add_event_handler('session_start', self.start)
self.add_event_handler('groupchat_message', self.muc_message)
2016-05-12 22:23:23 +00:00
self.muc_rooms = rooms.split()
2016-05-08 16:46:24 +00:00
self.nick = nick
self.token = token
self.xmpp_users = {}
for muc in self.muc_rooms:
self.add_event_handler("muc::%s::got_online" % muc,
self.muc_online)
self.add_event_handler("muc::%s::got_offline" % muc,
self.muc_offline)
2016-05-08 16:46:24 +00:00
# Telegram
self.groups = groups.split()
2016-05-08 16:46:24 +00:00
self.bot = telegram.Bot(self.token)
self.telegram_users = {}
2016-05-08 16:46:24 +00:00
# meter el conector del tg en un hilo
2016-05-08 16:46:24 +00:00
t = Thread(target=self.read_tg)
t.daemon = True
t.start()
print('Please wait a couple of minutes until it\'s correctly '
'connected')
2016-05-08 16:46:24 +00:00
def read_tg(self):
update_id = 0
while True:
try:
for update in self.bot.getUpdates(offset=update_id,
timeout=10):
message = update.message.text
user = str(update.message.from_user.username)
2016-05-08 21:12:04 +00:00
# sometimes there's no user. weird, but it happens
2016-05-08 16:46:24 +00:00
if not user:
user = str(update.message.from_user.first_name)
2016-05-08 21:12:04 +00:00
2016-05-12 22:23:23 +00:00
msg = user + ": " + message
2016-05-08 16:46:24 +00:00
chat_id = update.message.chat_id
2016-05-23 10:48:58 +00:00
if message and str(chat_id) in self.groups:
index = self.groups.index(str(chat_id))
receiver = self.muc_rooms[index]
2016-05-12 22:23:23 +00:00
if chat_id in self.telegram_users:
if user not in self.telegram_users[chat_id]:
self.telegram_users[chat_id] += ' ' + user
else:
self.telegram_users[chat_id] = ' ' + user
2016-05-12 22:23:23 +00:00
if message == '.users':
index = self.groups.index(str(chat_id))
muc = self.muc_rooms[index]
self.say_users('telegram', muc, chat_id)
2016-05-12 22:23:23 +00:00
else:
self.send_message(mto=receiver, mbody=msg,
2016-05-12 22:23:23 +00:00
mtype='groupchat')
update_id = update.update_id + 1
2016-05-08 21:12:04 +00:00
except NetworkError as e:
2016-05-08 16:46:24 +00:00
sleep(1)
2016-05-08 16:46:24 +00:00
except Unauthorized:
2016-05-08 21:12:04 +00:00
sleep(1)
2016-05-08 16:46:24 +00:00
except Exception as e:
print(e)
2016-05-08 16:46:24 +00:00
def start(self, event):
self.get_roster()
self.send_presence()
for muc in self.muc_rooms:
self.plugin['xep_0045'].joinMUC(muc, self.nick, wait=True)
2016-05-08 16:46:24 +00:00
def muc_message(self, msg):
muc_room = str(msg['from']).split('/')[0]
index = self.muc_rooms.index(muc_room)
tg_group = self.groups[index]
2016-05-12 22:23:23 +00:00
if msg['body'] == '.users':
self.say_users('xmpp', muc_room, tg_group)
2016-05-12 22:23:23 +00:00
elif msg['mucnick'] != self.nick:
message = str(msg['from']).split('/')[1] + ': ' + str(msg['body'])
self.bot.sendMessage(tg_group, text=message)
2016-05-12 22:23:23 +00:00
def muc_online(self, presence):
user = presence['muc']['nick']
muc = presence['from'].bare
if user != self.nick:
if muc in self.xmpp_users:
self.xmpp_users[muc].append(presence['muc']['nick'])
else:
self.xmpp_users[muc] = [presence['muc']['nick']]
2016-05-12 22:23:23 +00:00
def muc_offline(self, presence):
user = presence['muc']['nick']
muc = presence['from'].bare
2016-05-12 22:23:23 +00:00
if user != self.nick:
self.xmpp_users[muc].remove(presence['muc']['nick'])
def say_users(self, service, muc, group):
2016-05-12 22:23:23 +00:00
xmpp_users = ""
tg_users = ""
group = int(group)
2016-05-12 22:23:23 +00:00
if muc in self.xmpp_users:
for i in self.xmpp_users[muc]:
xmpp_users = xmpp_users + ' _' + i
else:
xmpp_users = ""
2016-05-12 22:23:23 +00:00
msg1 = 'XMPP Users:' + xmpp_users
if group in self.telegram_users:
tg_users = self.telegram_users[group]
else:
2016-05-12 22:23:23 +00:00
tg_users = ""
msg2 = 'Telegram Users:' + tg_users
message = msg1 + '\n' + msg2
if service == 'xmpp':
self.send_message(mto=muc, mbody=message, mtype='groupchat')
# arreglar el .users por el lado de tg
2016-05-12 22:23:23 +00:00
elif service == 'telegram':
self.bot.sendMessage(group, text=message)
2016-05-08 16:46:24 +00:00
if __name__ == '__main__':
# parsear config
config = []
parser = configparser.SafeConfigParser()
if len(argv) == 2:
parser.read(argv[1])
else:
parser.read('config.ini')
for name, value in parser.items('config'):
config.append(value)
# asignar valores para el bot
jid = config[0]
password = config[1]
muc_rooms = config[2]
2016-05-08 16:46:24 +00:00
nick = config[3]
token = config[4]
groups = config[5]
2016-05-08 16:46:24 +00:00
xmpp = EchoBot(jid, password, muc_rooms, nick, token, groups)
2016-05-08 16:46:24 +00:00
xmpp.register_plugin('xep_0045')
if xmpp.connect():
xmpp.process(block=True)
print("Done")
else:
print("Unable to connect.")
# Vols un gram nen?