Upload and send Media

Necdet Can Atesman edited this page Sep 27, 2017 · 3 revisions

You can send Image, Video, Audio to your WhatsApp contacts (at the moment yowsup supports image only, rest are WIP). Here is how to do so:

In short

  • Request upload URL from WhatsApp
  • Use MediaUploaded to upload data to this URL
  • Send media to contact


Request upload URL from WhatsApp

First step is request a URL to upload the media to. To do so you need to send an Iq message to WhatsApp via RequestUploadIqProtocolEntity.

You need pass at least for its constructor the mediaType (image in this case), and filePath, and the rest of data will be detected. The response will be a ResultRequestUploadIqProtocolEntity (see here how to listen for the response) and then you can get the url from it and upload the media to that url.

Note that if it's a duplicate upload (same media to same contact), WhatsApp recognized it and informs us about it. Use resultRequestUploadIqProtocolEntity.isDuplicate() to determine that. In that case, you can skip step 2 (upload step) and jump to Sending the media to contact.

Use MediaUploaded to upload data to this URL

Once you have the url, you can use MediaUploader for uploading it asynchronously. You can specify success, error, progress callbacks.

Send the media to contact

After uploading the media, send it to contact via the associate DownloadableMediaProtocolEntity. In case of an image, it's ImageDownloadableMediaProtocolEntity.

Quick example:

class MyYowsupApp(YowInterfaceLayer):
    def requestImageUpload(self, imagePath):
        self.demoContactJid = "abc@s.whatsapp.net" #only for the sake of simplicity of example, shoudn't do this
        self.filePath = imagePath #only for the sake of simplicity of example, shoudn't do this
        requestUploadEntity = RequestUploadIqProtocolEntity("image", filePath = imagePath)
        self._sendIq(requestUploadEntity, self.onRequestUploadResult, self.onRequestUploadError)
    def onRequestUploadResult(self, resultRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity):
        mediaUploader = MediaUploader(self.demoContactJid, self.getOwnJid(), self.filePath,
                                      self.onUploadSuccess, self.onUploadError, self.onUploadProgress)

    def onRequestUploadError(self, errorRequestUploadIqProtocolEntity, requestUploadIqProtocolEntity):
        print("Error requesting upload url")
    def onUploadSuccess(self, filePath, jid, url):
        #convenience method to detect file/image attributes for sending, requires existence of 'pillow' library
        entity = ImageDownloadableMediaMessageProtocolEntity.fromFilePath(filePath, url, None, to)

    def onUploadError(self, filePath, jid, url):
        print("Upload file failed!")

A full working example can be found in cli demo (image_send method)

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.