Add commands and support using name on telegram group name
This commit is contained in:
parent
b69960de08
commit
fa9c44b45c
123
jabbergram.py
123
jabbergram.py
|
@ -49,6 +49,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
self.groups = groups.split()
|
self.groups = groups.split()
|
||||||
self.bot = telegram.Bot(self.token)
|
self.bot = telegram.Bot(self.token)
|
||||||
self.telegram_users = {}
|
self.telegram_users = {}
|
||||||
|
self.group_name = {}
|
||||||
|
|
||||||
# initialize http upload on a thread since its needed to be connected
|
# initialize http upload on a thread since its needed to be connected
|
||||||
# to xmpp
|
# to xmpp
|
||||||
|
@ -85,6 +86,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
|
|
||||||
def read_tg(self):
|
def read_tg(self):
|
||||||
update_id = 0
|
update_id = 0
|
||||||
|
name = ''
|
||||||
|
|
||||||
# wait until http_upload has been tested
|
# wait until http_upload has been tested
|
||||||
sleep(5)
|
sleep(5)
|
||||||
|
@ -92,11 +94,11 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
try:
|
try:
|
||||||
for update in self.bot.getUpdates(offset=update_id,
|
for update in self.bot.getUpdates(offset=update_id,
|
||||||
timeout=10):
|
timeout=10):
|
||||||
|
if not self.group_name:
|
||||||
|
self.group_name = update.message.chat.username
|
||||||
if update.message.audio or update.message.document or \
|
if update.message.audio or update.message.document or \
|
||||||
update.message.photo or update.message.video \
|
update.message.photo or update.message.video \
|
||||||
or update.message.voice:
|
or update.message.voice or update.message.sticker:
|
||||||
|
|
||||||
# proceed only if http upload is available
|
# proceed only if http upload is available
|
||||||
if self.max_size is not None:
|
if self.max_size is not None:
|
||||||
if update.message.audio:
|
if update.message.audio:
|
||||||
|
@ -121,11 +123,11 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
size = d_file.file_size
|
size = d_file.file_size
|
||||||
if self.max_size >= size:
|
if self.max_size >= size:
|
||||||
t_file = self.bot.getFile(d_file.file_id)
|
t_file = self.bot.getFile(d_file.file_id)
|
||||||
name = '/tmp/' + d_file.file_id + ext
|
f_name = '/tmp/' + d_file.file_id + ext
|
||||||
t_file.download(name)
|
t_file.download(f_name)
|
||||||
url = self.http_upload.upload(
|
url = self.http_upload.upload(
|
||||||
self.component,
|
self.component,
|
||||||
'', name, size)
|
'', f_name, size)
|
||||||
|
|
||||||
if update.message.caption:
|
if update.message.caption:
|
||||||
message = update.message.caption + ' '
|
message = update.message.caption + ' '
|
||||||
|
@ -134,11 +136,12 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
|
|
||||||
message += url
|
message += url
|
||||||
else:
|
else:
|
||||||
message = 'A file has been uploaded to Telegra'
|
message = 'A file has been uploaded to Telegr'\
|
||||||
'm, but is too big.'
|
'am, but is too big.'
|
||||||
else:
|
else:
|
||||||
message = 'A file has been uploaderd to Telegram, '
|
message = 'A file has been uploaderd to Telegram,'\
|
||||||
'but the XMPP server doesn\'t support HTTP Upload.'
|
'but the XMPP server doesn\'t support H'\
|
||||||
|
'TTP Upload.'
|
||||||
|
|
||||||
elif update.message.new_chat_member:
|
elif update.message.new_chat_member:
|
||||||
message = 'This user has joined the group.'
|
message = 'This user has joined the group.'
|
||||||
|
@ -149,6 +152,16 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
update.message.new_chat_title
|
update.message.new_chat_title
|
||||||
elif update.message.new_chat_photo:
|
elif update.message.new_chat_photo:
|
||||||
message = 'The group\'s photo has changed.'
|
message = 'The group\'s photo has changed.'
|
||||||
|
else:
|
||||||
|
if update.message.reply_to_message:
|
||||||
|
name = update.message.reply_to_message.from_user\
|
||||||
|
.username
|
||||||
|
if name != self.bot.username:
|
||||||
|
message = name + ': ' + \
|
||||||
|
update.message.reply_to_message.text
|
||||||
|
else:
|
||||||
|
message = update.message.reply_to_message.text
|
||||||
|
message = message + ' <- ' + update.message.text
|
||||||
else:
|
else:
|
||||||
message = update.message.text
|
message = update.message.text
|
||||||
|
|
||||||
|
@ -163,23 +176,33 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
if not user:
|
if not user:
|
||||||
user = str(update.message.from_user.last_name)
|
user = str(update.message.from_user.last_name)
|
||||||
|
|
||||||
|
# Don't add name first if it's a reply
|
||||||
|
if name:
|
||||||
|
msg = message
|
||||||
|
else:
|
||||||
msg = user + ": " + message
|
msg = user + ": " + message
|
||||||
chat_id = update.message.chat_id
|
|
||||||
|
|
||||||
if message and str(chat_id) in self.groups:
|
chat = '@' + update.message.chat.username
|
||||||
index = self.groups.index(str(chat_id))
|
if chat not in self.groups:
|
||||||
|
chat = str(update.message.chat_id)
|
||||||
|
|
||||||
|
if message and chat in self.groups:
|
||||||
|
index = self.groups.index(chat)
|
||||||
receiver = self.muc_rooms[index]
|
receiver = self.muc_rooms[index]
|
||||||
|
|
||||||
if chat_id in self.telegram_users:
|
if chat in self.telegram_users:
|
||||||
if user not in self.telegram_users[chat_id]:
|
if user not in self.telegram_users[chat]:
|
||||||
self.telegram_users[chat_id] += ' ' + user
|
self.telegram_users[chat] += ' ' + user
|
||||||
else:
|
else:
|
||||||
self.telegram_users[chat_id] = ' ' + user
|
|
||||||
|
self.telegram_users[chat] = ' ' + user
|
||||||
|
|
||||||
if message == '.users':
|
if message == '.users':
|
||||||
index = self.groups.index(str(chat_id))
|
self.say_users('telegram', muc, chat)
|
||||||
muc = self.muc_rooms[index]
|
elif message == '.help':
|
||||||
self.say_users('telegram', muc, chat_id)
|
self.say_help('telegram', muc, chat)
|
||||||
|
elif message == '.where':
|
||||||
|
self.say_where('telegram', muc, chat)
|
||||||
else:
|
else:
|
||||||
self.send_message(mto=receiver, mbody=msg,
|
self.send_message(mto=receiver, mbody=msg,
|
||||||
mtype='groupchat')
|
mtype='groupchat')
|
||||||
|
@ -211,7 +234,10 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
|
|
||||||
if msg['body'] == '.users':
|
if msg['body'] == '.users':
|
||||||
self.say_users('xmpp', muc_room, tg_group)
|
self.say_users('xmpp', muc_room, tg_group)
|
||||||
|
elif msg['body'] == '.help':
|
||||||
|
self.say_help('xmpp', muc_room, tg_group)
|
||||||
|
elif msg['body'] == '.where':
|
||||||
|
self.say_where('xmpp', muc_room, tg_group)
|
||||||
elif msg['mucnick'] != self.nick:
|
elif msg['mucnick'] != self.nick:
|
||||||
message = str(msg['from']).split('/')[1] + ': ' + str(msg['body'])
|
message = str(msg['from']).split('/')[1] + ': ' + str(msg['body'])
|
||||||
self.bot.sendMessage(tg_group, text=message)
|
self.bot.sendMessage(tg_group, text=message)
|
||||||
|
@ -234,32 +260,46 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
self.xmpp_users[muc].remove(presence['muc']['nick'])
|
self.xmpp_users[muc].remove(presence['muc']['nick'])
|
||||||
|
|
||||||
def say_users(self, service, muc, group):
|
def say_users(self, service, muc, group):
|
||||||
xmpp_users = ""
|
if service == 'xmpp':
|
||||||
tg_users = ""
|
|
||||||
group = int(group)
|
|
||||||
|
|
||||||
if muc in self.xmpp_users:
|
|
||||||
for i in self.xmpp_users[muc]:
|
|
||||||
xmpp_users = xmpp_users + ' _' + i
|
|
||||||
else:
|
|
||||||
xmpp_users = ""
|
|
||||||
|
|
||||||
msg1 = 'XMPP Users:' + xmpp_users
|
|
||||||
|
|
||||||
if group in self.telegram_users:
|
if group in self.telegram_users:
|
||||||
tg_users = self.telegram_users[group]
|
tg_users = self.telegram_users[group]
|
||||||
else:
|
else:
|
||||||
tg_users = ""
|
tg_users = ""
|
||||||
|
|
||||||
msg2 = 'Telegram Users:' + tg_users
|
msg = 'Telegram Users:' + tg_users
|
||||||
|
|
||||||
message = msg1 + '\n' + msg2
|
self.send_message(mto=muc, mbody=msg, mtype='groupchat')
|
||||||
|
|
||||||
if service == 'xmpp':
|
|
||||||
self.send_message(mto=muc, mbody=message, mtype='groupchat')
|
|
||||||
|
|
||||||
elif service == 'telegram':
|
elif service == 'telegram':
|
||||||
self.bot.sendMessage(group, text=message)
|
xmpp_users = ""
|
||||||
|
if muc in self.xmpp_users:
|
||||||
|
for i in self.xmpp_users[muc]:
|
||||||
|
xmpp_users = xmpp_users + ' ' + i
|
||||||
|
else:
|
||||||
|
xmpp_users = ""
|
||||||
|
|
||||||
|
msg = 'XMPP Users:' + xmpp_users
|
||||||
|
self.bot.sendMessage(group, text=msg)
|
||||||
|
|
||||||
|
def say_help(self, service, muc, group):
|
||||||
|
msg = 'Hi, I\'m ' + self.bot.username + '. I have two commands : ".us'\
|
||||||
|
'ers" and ".where".'
|
||||||
|
if service == 'xmpp':
|
||||||
|
self.send_message(mto=muc, mbody=msg, mtype='groupchat')
|
||||||
|
elif service == 'telegram':
|
||||||
|
self.bot.sendMessage(group, text=msg)
|
||||||
|
|
||||||
|
def say_where(self, service, muc, group):
|
||||||
|
if service == 'xmpp':
|
||||||
|
if self.group_name:
|
||||||
|
msg = 'I\'m on http://telegram.me/' + self.group_name + '.'
|
||||||
|
else:
|
||||||
|
msg = 'Sorry, I\'m on a private group, you\'ll have to ask fo'\
|
||||||
|
'r an invitation.'
|
||||||
|
self.send_message(mto=muc, mbody=msg, mtype='groupchat')
|
||||||
|
elif service == 'telegram':
|
||||||
|
msg = 'I\'m on ' + muc + '.'
|
||||||
|
self.bot.sendMessage(group, text=msg)
|
||||||
|
|
||||||
class HttpUpload():
|
class HttpUpload():
|
||||||
def __init__(self, parent_self):
|
def __init__(self, parent_self):
|
||||||
|
@ -316,8 +356,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
|
||||||
xml = minidom.parseString(str(send))
|
xml = minidom.parseString(str(send))
|
||||||
put_url = xml.getElementsByTagName('put')[0].firstChild.data
|
put_url = xml.getElementsByTagName('put')[0].firstChild.data
|
||||||
|
|
||||||
verify_ssl = ''
|
if verify_ssl == '' or verif_ssl == 'False':
|
||||||
if verify_ssl == 'False':
|
|
||||||
req = requests.put(put_url, data=open(u_file, 'rb'),
|
req = requests.put(put_url, data=open(u_file, 'rb'),
|
||||||
verify=False)
|
verify=False)
|
||||||
else:
|
else:
|
||||||
|
@ -356,5 +395,3 @@ if __name__ == '__main__':
|
||||||
print("Done")
|
print("Done")
|
||||||
else:
|
else:
|
||||||
print("Unable to connect.")
|
print("Unable to connect.")
|
||||||
|
|
||||||
# Vols un gram nen?
|
|
||||||
|
|
Loading…
Reference in New Issue