yowsup icon indicating copy to clipboard operation
yowsup copied to clipboard

How to upload image & send?

Open Wiku5 opened this issue 13 years ago • 32 comments

What is the way to upload an image?

I understand how to send it, but I need to be able to send the URL that is located on WhatsApp's server.

Is there any documentation to send via Uploader.py under media? For the life of me, i can't get it to work regardless of any workarounds.

Many thanks!

Wiku5 avatar Oct 02 '13 17:10 Wiku5

Media.uploader is here

CODeRUS avatar Oct 02 '13 17:10 CODeRUS

I know, but how do I use it? I've tried everything. Is it possible for you to please post an example?

Wiku5 avatar Oct 02 '13 17:10 Wiku5

I keep getting:

Traceback (most recent call last): File "/home/wikus/Dropbox/projects/whatsapp/Yowsup/Media/uploader.py", line 1, in from ..Common.Http.warequest import WARequest ValueError: Attempted relative import in non-package

in Media.Uploader

Wiku5 avatar Oct 02 '13 17:10 Wiku5

you can check wazapp sources

CODeRUS avatar Oct 02 '13 17:10 CODeRUS

I've tried that already.

Is there any way possible for you to pastebin what you have to uploading images? Or even a PHP script?

I've google'd for days trying to find a way to upload correctly.

Wiku5 avatar Oct 02 '13 17:10 Wiku5

Also, Wazapp uses this in their code: Yowsup.Media.downloader import MediaDownloader

But does not include the Yowsup fork in their source, so it's redundant to try and find a working implementation of it unfortunatley.

Wiku5 avatar Oct 02 '13 17:10 Wiku5

you should fill and send media upload request before, and then upload media :)

CODeRUS avatar Oct 02 '13 18:10 CODeRUS

Could you please post an example?

Wiku5 avatar Oct 02 '13 18:10 Wiku5

Here is the upload process: Step 1. Call 'media_requestUpload' method (in YowsupConnectionManager) to get the upload path. Step 2. Upload image to the path from step 1 via 'Media.uploader'.

Wazapp has the best example for uploader.

xiaojxiao avatar Oct 08 '13 08:10 xiaojxiao

how can we get upload path using media_requestUpload method?

siddharth7815 avatar Oct 26 '13 12:10 siddharth7815

It doesn't work for me and i have no clue how to debug it.

DEBUG:BinTreeNodeWriter:Outgoing
DEBUG:BinTreeNodeWriter:
<iq to="s.whatsapp.net" type="set" id="upload_1">
<media type="image" xmlns="w:m" hash="dOBuL1lRvwUeeGRTnkvG3vWITPMGLd0+egOtGU2gyyg=" size="116">
</media>
</iq>

DEBUG:BinTreeNodeReader:Incoming
DEBUG:BinTreeNodeReader:
<iq type="error" from="s.whatsapp.net" id="upload_1">
<error text="bad-request" code="400">
</error>
</iq>

Has anybody set uploading up successfully?

5263 avatar Dec 14 '13 12:12 5263

you are passing wrong parameter while calling message_imageSend. here type of file size is string.

self.methodsInterface.call("message_imageSend",self.jids[i],uploaded_url,self.filename,str(self.filesize),self.message))

siddharth7815 avatar Dec 14 '13 13:12 siddharth7815

It seems WA is rejecting media below a certain size. I bypassed this problem. But now i get File "./src/Yowsup/Media/uploader.py", line 77, in upload self._d(POST) AttributeError: 'MediaUploader' object has no attribute '_d'

5263 avatar Dec 14 '13 14:12 5263

Hello together,

I modified the Example/CmdClient.py so it can send and receive images. Here are the modifications:

  • incudes
from Yowsup.Media.downloader import MediaDownloader
from Yowsup.Media.uploader import MediaUploader
from sys import stdout
import os
import hashlib 
import base64
  • in def init
self.signalsInterface.registerListener("media_uploadRequestSuccess", self.onmedia_uploadRequestSuccess)
self.signalsInterface.registerListener("media_uploadRequestFailed", self.onmedia_uploadRequestFailed)
self.signalsInterface.registerListener("media_uploadRequestDuplicate", self.onmedia_uploadRequestDuplicate)
self.path = ""
self.gotMediaReceipt = False
self.done = False
  • in runCommand
     def runCommand(self, command):
                splitstr = command.split(' ')
        if splitstr[0] == "/pic" and len(splitstr) == 2:
            self.path = splitstr[1]

            if not os.path.isfile(splitstr[1]):
                print("File %s does not exists" % splitstr[1])
                return 1


            statinfo = os.stat(self.path)
            name=os.path.basename(self.path)
            print("Sending picture %s of size %s with name %s" %(self.path, statinfo.st_size, name))
            mtype = "image"

            sha1 = hashlib.sha256()
            fp = open(self.path, 'rb')
            try:
                sha1.update(fp.read())
                hsh = base64.b64encode(sha1.digest())
                print("Sending media_requestUpload")
                self.methodsInterface.call("media_requestUpload", (hsh, mtype, os.path.getsize(self.path)))
            finally:
                fp.close()

            timeout = 100
            t = 0;
            while t < timeout and not self.gotMediaReceipt:
                time.sleep(0.5)
                t+=1

            if not self.gotMediaReceipt:
                print("MediaReceipt print timedout!")
            else:
                print("Got request MediaReceipt")

            return 1
        elif command[0] == "/":
  • new functions:

    def onImageReceived(self, messageId, jid, preview, url, size, wantsReceipt, isBroadcast):
        print("Image received: Id:%s Jid:%s Url:%s size:%s" %(messageId, jid, url, size))
        downloader = MediaDownloader(self.onDlsuccess, self.onDlerror, self.onDlprogress)
        downloader.download(url)
        if wantsReceipt and self.sendReceipts:
            self.methodsInterface.call("message_ack", (jid, messageId))

        timeout = 10
        t = 0;
        while t < timeout:
            time.sleep(0.5)
            t+=1

    def onDlsuccess(self, path):
        stdout.write("\n")
        stdout.flush()
        print("Image downloded to %s"%path)
        print(self.getPrompt())

    def onDlerror(self):
        stdout.write("\n")
        stdout.flush()
        print("Download Error")
        print(self.getPrompt())

    def onDlprogress(self, progress):
        stdout.write("\r Progress: %s" % progress)
        stdout.flush()

    def onmedia_uploadRequestSuccess(self,_hash, url, resumeFrom):
        print("Request Succ: hash: %s url: %s resume: %s"%(_hash, url, resumeFrom))
        self.uploadImage(url)
        self.gotMediaReceipt = True

    def onmedia_uploadRequestFailed(self,_hash):
        print("Request Fail: hash: %s"%(_hash))
        self.gotReceipt = True

    def onmedia_uploadRequestDuplicate(self,_hash, url):
            print("Request Dublicate: hash: %s url: %s "%(_hash, url))
            self.doSendImage(url)
            self.gotMediaReceipt = True

    def uploadImage(self, url):
        uploader = MediaUploader(self.jid, self.username, self.onUploadSuccess, self.onError, self.onProgressUpdated)
        uploader.upload(self.path,url)

    def onUploadSuccess(self, url):
        stdout.write("\n")
        stdout.flush()
        print("Upload Succ: url: %s "%( url))
        self.doSendImage(url)

    def onError(self):
        stdout.write("\n")
        stdout.flush()
        print("Upload Fail:")

    def onProgressUpdated(self, progress):
        stdout.write("\r Progress: %s" % progress)
        stdout.flush()

    def doSendImage(self, url):
          print("Sending message_image")
          statinfo = os.stat(self.path)
          name=os.path.basename(self.path)
          msgId = self.methodsInterface.call("message_imageSend", (self.jid, url, name,str(statinfo.st_size), "yes"))
          self.sentCache[msgId] = [int(time.time()), self.path]

hope that helps

SamsonBox avatar Jan 24 '14 21:01 SamsonBox

Great SamsonBox! I'm begginer in python developer (I use java), can I use these code to send an image? is it possible to do this in a windows command line? (if not, please, tell me how in linux, eclipse + python, etc)

Edit: I've managed to send a picture, but on reaching the target image does not show the preview, and forcé closes . someone have some solution ?

dhthuerta avatar Feb 01 '14 13:02 dhthuerta

SamsonBox! This code isnt sending preview... its a bug of yowsup?

thvvieira avatar Apr 24 '14 11:04 thvvieira

Hahaha sorry this made me laugh

msgId = self.methodsInterface.call("message_imageSend", (self.jid, url, name,str(statinfo.st_size), "yes"))

Image thumbnail is not a boolean, it's a raw image. You need to take the original image and resize it to max 100px to create a thumbnail: https://github.com/venomous0x/WhatsAPI/blob/master/src/php/func.php#L95

shirioko avatar Apr 25 '14 07:04 shirioko

Ive replaced the whole runCommand part with the one listed up. im getting this error: def onMessageReceived(self, messageId, jid, messageContent, timestamp, wantsReceipt, pushName, isBroadcast): ^ IndentationError: expected an indented block

anyone have a clue?

itsari avatar Aug 12 '14 14:08 itsari

same indentationError with me. )-: Any change someone could help out here with SamsonBox example? Maybe a tiny 'noob' howto python send / receive picture? Would be greatly appreciated :+1:

Creamers158 avatar Aug 17 '14 19:08 Creamers158

IndentationError: expected an indented block its because the spaces/tabs. Use google for that, its not an API related error

mgp25 avatar Aug 18 '14 10:08 mgp25