examples: add oob to httpupload example

many people ask how to do this, might as well do this by default
This commit is contained in:
mathieui 2021-02-24 20:43:21 +01:00
parent 7381460556
commit 5764ef7ff7

View file

@ -10,6 +10,7 @@ from getpass import getpass
from argparse import ArgumentParser from argparse import ArgumentParser
import slixmpp import slixmpp
from slixmpp.exceptions import IqTimeout
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -31,15 +32,22 @@ class HttpUpload(slixmpp.ClientXMPP):
async def start(self, event): async def start(self, event):
log.info('Uploading file %s...', self.filename) log.info('Uploading file %s...', self.filename)
def timeout_callback(arg): try:
raise TimeoutError("could not send message in time") url = await self['xep_0363'].upload_file(
url = await self['xep_0363'].upload_file( self.filename, domain=self.domain, timeout=10
self.filename, domain=self.domain, timeout=10, timeout_callback=timeout_callback) )
except IqTimeout:
raise TimeoutError('Could not send message in time')
log.info('Upload success!') log.info('Upload success!')
log.info('Sending file to %s', self.recipient) log.info('Sending file to %s', self.recipient)
html = '<body xmlns="http://www.w3.org/1999/xhtml"><a href="%s">%s</a></body>' % (url, url) html = (
self.send_message(self.recipient, url, mhtml=html) f'<body xmlns="http://www.w3.org/1999/xhtml">'
f'<a href="{url}">{url}</a></body>'
)
message = self.make_message(mto=self.recipient, mbody=url, mhtml=html)
message['oob']['url'] = url
message.send()
self.disconnect() self.disconnect()
@ -83,6 +91,7 @@ if __name__ == '__main__':
args.password = getpass("Password: ") args.password = getpass("Password: ")
xmpp = HttpUpload(args.jid, args.password, args.recipient, args.file, args.domain) xmpp = HttpUpload(args.jid, args.password, args.recipient, args.file, args.domain)
xmpp.register_plugin('xep_0066')
xmpp.register_plugin('xep_0071') xmpp.register_plugin('xep_0071')
xmpp.register_plugin('xep_0128') xmpp.register_plugin('xep_0128')
xmpp.register_plugin('xep_0363') xmpp.register_plugin('xep_0363')