Skip to content

Instantly share code, notes, and snippets.

@legastero
Last active October 10, 2021 16:12
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save legastero/4497613 to your computer and use it in GitHub Desktop.
Save legastero/4497613 to your computer and use it in GitHub Desktop.
Send an image in a chat message with SleekXMPP
import logging
import threading
import sleekxmpp
logging.basicConfig(level=logging.DEBUG)
class Bot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password):
super(Bot, self).__init__(jid, password)
self.ready = threading.Event()
self.register_plugin('xep_0030')
self.register_plugin('xep_0066') # OOB
self.register_plugin('xep_0231') # BOB
self.add_event_handler('session_start', self.session_start)
def session_start(self, event):
self.get_roster()
self.send_presence()
self.ready.set()
def send_image_html(self, jid, img_url):
m = self.Message()
m['to'] = jid
m['type'] = 'chat'
m['body'] = 'Tried sending an image using HTML-IM'
m['html']['body'] = '<img src="%s" />' % img_url
m.send()
def send_image_bob(self, jid, img_file_path):
m = self.Message()
m['to'] = jid
m['type'] = 'chat'
with open(img_file_path, 'rb') as img_file:
img = img_file.read()
if img:
cid = self['xep_0231'].set_bob(img, 'image/png')
m['body'] = 'Tried sending an image using HTML-IM + BOB'
m['html']['body'] = '<img src="cid:%s" />' % cid
m.send()
def send_image_oob(self, jid, img_url):
m = self.Message()
m['to'] = jid
m['type'] = 'chat'
m['body'] = 'Tried sending an image using OOB'
m['oob']['url'] = img_url
m.send()
if __name__ == '__main__':
b = Bot('bot@example.com', 'secret')
b.connect()
b.process(block=False)
b.ready.wait()
b.send_image_html('someone@example.com', 'http://example.com/image.png')
b.send_image_bob('someone@example.com', 'image.png')
b.send_image_oob('someone@example.com', 'http://example.com/image.png')
@vishnuc17
Copy link

How is the working of function 'send_image_bob' and what is the object cid it displays some jabber website name.Can you please explain what is happening in this function

@zingzinco
Copy link

The function, basically gets the 'cid' (Content ID) for the image. If you refer to the xep_0231 plugin of the sleekxmpp library, you will find the set_bob function which does just that.

When sent, the image source (src) will reflect said cid (in the following format - cid:sha1+(generated alphanumeric)@bob.xmpp.org) which can be used by the recipient to retrieve the cached data - in this case the image.

However, it would be important to note that certain clients and server do not support html which will thus lead to inability to deliver said image.

More information of the XEP-0231 plugin can be found at http://xmpp.org/extensions/xep-0231.html

@sbonnegent
Copy link

sbonnegent commented May 25, 2017

Hi,
Function b.send_image_bob('someone@example.com', 'image.png') doesn't work with python3. We obtain this error:

m['html']['body'] = '<img src="cid:%s" />' % cid
TypeError: 'str' object does not support item assignment

Do you know why ?

@s-nt-s
Copy link

s-nt-s commented May 31, 2017

@sbonnegent m['html'] is '' unless you do self.register_plugin('xep_0071') in __init__

@sbonnegent
Copy link

Oh, it works now, thank you @s-nt-s ! I didn't see it in doc.

@virdb
Copy link

virdb commented Aug 23, 2017

there are any android client that supports BOB?

@gsathish09
Copy link

is it possible to send image as an attachment ? because when i try 'sending image with OOB' jabber receiver receives the image but its coming as an unsupported image format .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment