Upload stickers with http upload

And fix minor things
This commit is contained in:
drymer 2017-04-30 22:11:11 +02:00
parent ed6d8a142e
commit c5505f8eef
1 changed files with 21 additions and 8 deletions

View File

@ -5,6 +5,7 @@ try:
import requests import requests
except: except:
print("HTTP Upload support disabled.") print("HTTP Upload support disabled.")
import sleekxmpp import sleekxmpp
import telegram import telegram
import configparser import configparser
@ -26,8 +27,9 @@ class Request(ElementBase):
interfaces = set(('filename', 'size')) interfaces = set(('filename', 'size'))
sub_interfaces = interfaces sub_interfaces = interfaces
class Jabbergram(sleekxmpp.ClientXMPP): class Jabbergram(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, rooms, nick, token, groups): def __init__(self, jid, password, rooms, nick, token, groups, verify_ssl):
# XMPP # XMPP
super(Jabbergram, self).__init__(jid, password) super(Jabbergram, self).__init__(jid, password)
self.add_event_handler('session_start', self.start) self.add_event_handler('session_start', self.start)
@ -38,6 +40,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
self.token = token self.token = token
self.xmpp_users = {} self.xmpp_users = {}
self.jid = jid self.jid = jid
self.verify_ssl = verify_ssl
for muc in self.muc_rooms: for muc in self.muc_rooms:
self.add_event_handler("muc::%s::got_online" % muc, self.add_event_handler("muc::%s::got_online" % muc,
@ -85,6 +88,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
def read_tg(self): def read_tg(self):
update_id = 0 update_id = 0
# wait until http_upload has been tested # wait until http_upload has been tested
sleep(5) sleep(5)
while True: while True:
@ -110,6 +114,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
update.message.photo or update.message.video \ update.message.photo or update.message.video \
or update.message.voice or update.message.sticker: 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:
d_file = update.message.audio d_file = update.message.audio
@ -131,13 +136,18 @@ class Jabbergram(sleekxmpp.ClientXMPP):
d_file = update.message.voice d_file = update.message.voice
ext = '.ogg' ext = '.ogg'
size = d_file.file_size size = d_file.file_size
elif update.message.sticker:
d_file = update.message.sticker
ext = '.png'
size = d_file.file_size
if self.max_size >= int(size): if self.max_size >= int(size):
t_file = self.bot.getFile(d_file.file_id) t_file = self.bot.getFile(d_file.file_id)
f_name = '/tmp/' + d_file.file_id + ext f_name = '/tmp/' + d_file.file_id + ext
t_file.download(f_name) t_file.download(f_name)
url = self.http_upload.upload( url = self.http_upload.upload(
self.component, self.component,
'', f_name, size) self.verify_ssl,
f_name, size)
if update.message.caption: if update.message.caption:
message = update.message.caption + ' ' message = update.message.caption + ' '
@ -158,7 +168,7 @@ class Jabbergram(sleekxmpp.ClientXMPP):
elif update.message.left_chat_member: elif update.message.left_chat_member:
message = 'This user has left the group.' message = 'This user has left the group.'
elif update.message.new_chat_title: elif update.message.new_chat_title:
message = 'The group\'s title has changed: '+ \ message = 'The group\'s title has changed: ' + \
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.'
@ -175,7 +185,8 @@ class Jabbergram(sleekxmpp.ClientXMPP):
message = update.message.text message = update.message.text
if name: if name:
msg = message + ' <- ' + user + ": " + update.message.text msg = message + ' <- ' + user + ": " + \
update.message.text
else: else:
msg = user + ": " + message msg = user + ": " + message
@ -353,10 +364,10 @@ 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
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: elif verify_ssl == '':
req = requests.put(put_url, data=open(u_file, 'rb')) req = requests.put(put_url, data=open(u_file, 'rb'))
return put_url return put_url
@ -383,8 +394,10 @@ if __name__ == '__main__':
nick = config[3] nick = config[3]
token = config[4] token = config[4]
groups = config[5] groups = config[5]
verify_ssl = config[6]
xmpp = Jabbergram(jid, password, muc_rooms, nick, token, groups) xmpp = Jabbergram(jid, password, muc_rooms, nick, token, groups,
verify_ssl)
xmpp.register_plugin('xep_0045') xmpp.register_plugin('xep_0045')
if xmpp.connect(): if xmpp.connect():