mukulhase / WebWhatsapp-Wrapper

An API for sending and receiving messages over web.whatsapp [Working as of 18th May 2018]
https://webwhatsapi.readthedocs.io/en/latest/
MIT License
2.02k stars 797 forks source link

How to sent URL text message? #278

Open ayrokid opened 6 years ago

ayrokid commented 6 years ago

i want sent URL message.

thanks,

bobaoapae commented 6 years ago

check WAPI.js function thumb = IMAGE IN BASE64 window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId)

dafner commented 6 years ago

I´m trying to sent URL text message using the following function based in the suggesting of bobaoapae :

**def shareLink(chatId, thumb, url, title, description):

if os.path.isfile(thumb):
    with open(thumb, "rb") as imageFile:
        encodedString = base64.b64encode(imageFile.read())
else:
    encodedString = ''

print(encodedString)
return driver.wapi_functions.sendMessageWithThumb(encodedString, url, title[0:65], description[0:155], chatId)**

but I get the following error : TypeError: 'hasLink' is a derived property, it can't be set directly

File "test.py", line 149, in shareLink return driver.wapi_functions.sendMessageWithThumb(encodedString, url, title[0:65], description[0:155], chatId) File "/home/administrador/workspace/whatsapp/WebWhatsapp-Wrapper/webwhatsapi/wapi_js_wrapper.py", line 134, in call raise JsException("Error in function {0} ({1}). Command: {2}".format(self.function_name, e.msg, command)) webwhatsapi.wapi_js_wrapper.JsException: Error in function sendMessageWithThumb (TypeError: 'hasLink' is a derived property, it can't be set directly.). Command: return WAPI.sendMessageWithThumb('/home/administrador/workspace/whatsapp/images/thumbail.jpeg','http://webdevelopmentscripts.com/64-how-to-change-the-address-bar-color-in-chrome-firefox-opera-safari','How to change the address bar color in Chrome, Firefox, Opera, Sa','How to change the address bar color in Chrome, Firefox, Opera, Safari','5491130994639@c.us', arguments[0])

Here is the javascript function where the error is produced window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId) { var chatSend = WAPI.getChat(chatId); if (chatSend === undefined) { return false; } var msgWithImg = chatSend.createMessageFromText("."); msgWithImg.hasLink = title; msgWithImg.isLink = title; msgWithImg.body = description + '\n ' + url; msgWithImg.description = description; msgWithImg.subtype = 'url'; msgWithImg.title = title; msgWithImg.thumbnail = thumb; return chatSend.addAndSendMsg(msgWithImg);

Somebody can provide an example about how to shared a link?

Any help will be appreciated

bobaoapae commented 6 years ago

replace function in WAPI.js

window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId) { var chatSend = WAPI.getChat(chatId); if (chatSend === undefined) { return false; } var msgWithImg = chatSend.createMessageFromText("."); msgWithImg.x_hasLink = title; msgWithImg.__x_body = description + '\n ' + url; msgWithImg.x_isLink = title; msgWithImg.x_description = description; msgWithImg.__x_subtype = 'url'; msgWithImg.x_title = title; msgWithImg.__x_thumbnail = thumb; chatSend.addAndSendMsg(msgWithImg); return true; };

dafner commented 6 years ago

thanks bobaoapae for your promp answer! I applied the changes that you suggested but I get a new error (see below) sending an Image in base64 format

Thanks in advance for your help

Traceback (most recent call last): File "/home/administrador/workspace/whatsapp/WebWhatsapp-Wrapper/webwhatsapi/wapi_js_wrapper.py", line 126, in call return self.driver.execute_async_script(command) File "/home/administrador/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 652, in execute_async_script 'args': converted_args})['value'] File "/home/administrador/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute self.error_handler.check_response(response) File "/home/administrador/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.JavascriptException: Message: SyntaxError: missing ) after argument list

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "test.py", line 290, in check_unread() File "test.py", line 223, in check_unread "How to change the address bar color in Chrome") File "test.py", line 146, in shareLink return driver.wapi_functions.sendMessageWithThumb(encodedString, url, title[0:65], description[0:155], chatId) File "/home/administrador/workspace/whatsapp/WebWhatsapp-Wrapper/webwhatsapi/wapi_js_wrapper.py", line 134, in call raise JsException("Error in function {0} ({1}). Command: {2}".format(self.function_name, e.msg, command)) webwhatsapi.wapi_js_wrapper.JsException: Error in function sendMessageWithThumb (SyntaxError: missing ) after argument list). Command: return WAPI.sendMessageWithThumb(b'/9j/4AAQSkZJRgABAQAAAQABAAD//gA8Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gMTAwCv/bAEMAAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/bAEMBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAQoAyAMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABB .......... PFKCcrye/wD6CtAACBnBA9OR6t+XX3/Sjjn7vT1Xnp6r7f54wZPzc9j/ADamknnk/dXv/u0AOyM/w9Qc5X39uv8AnPU0ZGevf1H+z/h+hoJOep+8P/QmoJOevf8Aqn+J/OgA446dD3X0Ht9c9vXjiikyeOT0Pf8A2Af580UAf//Z','http://webdevelopmentscripts.com/64-how-to-change-the-address-bar-color-in-chrome-firefox-opera-safari','How to change the address bar color in Chrome','How to change the address bar color in Chrome','5491130994639@c.us', arguments[0])

bobaoapae commented 6 years ago

it's look like a error in python function... i don't use python here.. but i think it's error in first argument

it's start with b'/ <--- It's a byte array in python... need send String

dafner commented 6 years ago

Thanks bobaoapae for your suggestion!

I added decode('utf-8') to the function base64.b64encode and now I could send a preview link!

Here is my complete function (not yet finish) that I'm using for testing purposes:

def shareLink(chatId, thumb, url, title, description): if os.path.isfile(thumb): with open(thumb, "rb") as imageFile: encodedString = base64.b64encode(imageFile.read()).decode('utf-8') else: encodedString = ''

return driver.wapi_functions.sendMessageWithThumb(encodedString, url, title[0:65], description[0:155], chatId)

I'm calling the function with the following parameters in order to reproduce the example explained in this site (http://webdevelopmentscripts.com/80-whatsapp-share-link-with-image-in-websites):

                shareLink(contact.chat.id,
                    "/home/administrador/workspace/whatsapp/images/thumbail.jpeg",
                    "http://webdevelopmentscripts.com/64-how-to-change-the-address-bar-color-in-chrome-firefox-opera-safari",
                    "How to change the address bar color in Chrome",
                    "How to change the address bar color in Chrome")

And here is the javascript function modified:

webwhatsapi/js/wapi.js window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId) { var chatSend = WAPI.getChat(chatId); if (chatSend === undefined) { return false; } var msgWithImg = chatSend.createMessageFromText("."); msgWithImg.x_hasLink = title; msgWithImg.__x_body = description + '\n ' + url; msgWithImg.x_isLink = title; msgWithImg.x_description = description; msgWithImg.__x_subtype = 'url'; msgWithImg.x_title = title; msgWithImg.__x_thumbnail = thumb;

chatSend.addAndSendMsg(msgWithImg);

return true;

};

Now, the problem is that when finish the send of the preview the function driver.wapi_functions.sendMessageWithThumb not return the control to the main program and it's necesary to abort the execution.

Apparently, the problem is in the function self.driver.execute_async_script(command) in line 126 in webwhatsapi/wapi_js_wrapper.py module. The function remain in a loop waiting for some kind of answer and never return any value.

Below is the traceback when I abort the program:

Traceback (most recent call last): File "test.py", line 292, in check_unread() File "test.py", line 225, in check_unread "How to change the address bar color in Chrome") File "test.py", line 148, in shareLink driver.wapi_functions.sendMessageWithThumb(encodedString, url, title[0:65], description[0:155], chatId) File "/home/administrador/workspace/whatsapp/WebWhatsapp-Wrapper/webwhatsapi/wapi_js_wrapper.py", line 126, in call return self.driver.execute_async_script(command) File "/home/administrador/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 652, in execute_async_script 'args': converted_args})['value'] File "/home/administrador/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute response = self.command_executor.execute(driver_command, params) File "/home/administrador/.local/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 472, in execute return self._request(command_info[0], url, body=data) File "/home/administrador/.local/lib/python3.6/site-packages/selenium/webdriver/remote/remote_connection.py", line 496, in _request resp = self._conn.getresponse() File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse response.begin() File "/usr/lib/python3.6/http/client.py", line 297, in begin version, status, reason = self._read_status() File "/usr/lib/python3.6/http/client.py", line 258, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") File "/usr/lib/python3.6/socket.py", line 586, in readinto return self._sock.recv_into(b) KeyboardInterrupt

I hope that you can help me in order to find the solution to this problem

Thanks in advance for your help

bobaoapae commented 6 years ago

use this window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId, done) { var chatSend = WAPI.getChat(chatId); if (chatSend === undefined) { return false; } var msgWithImg = chatSend.createMessageFromText("."); msgWithImg.x_hasLink = title; msgWithImg.__x_body = description + '\n ' + url; msgWithImg.x_isLink = title; msgWithImg.x_description = description; msgWithImg.__x_subtype = 'url'; msgWithImg.x_title = title; msgWithImg.__x_thumbnail = thumb;

chatSend.addAndSendMsg(msgWithImg); if(done!==undefined){ done(true); } return true; };

dafner commented 6 years ago

Thanks very much, It's working OK!!!!

brunosoaresds commented 6 years ago

@dafner @bobaoapae, can you send a PR with this updates?

bobaoapae commented 6 years ago

@dafner send you if you can.. i don't have python code here... i just use the javascript code in my own Java library

dafner commented 6 years ago

As bobaoapae suggest me, the only modification was the replace the function sendMessageWithThumb at line 319 from file webwhatsapi/js/wapi.js

From webwhatsapi/js/wapi.js Line 319

window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId, done) { var chatSend = WAPI.getChat(chatId); if (chatSend === undefined) { return false; } var msgWithImg = chatSend.createMessageFromText("."); msgWithImg.x_hasLink = title; msgWithImg.__x_body = description + '\n ' + url; msgWithImg.x_isLink = title; msgWithImg.x_description = description; msgWithImg.__x_subtype = 'url'; msgWithImg.x_title = title; msgWithImg.__x_thumbnail = thumb;

chatSend.addAndSendMsg(msgWithImg);

if(done!==undefined){
    done(true);
}

return true;

};

To test the above function I'm using the following function in my application:

def shareLink(chatId, thumb, url, title, description): if os.path.isfile(thumb): with open(thumb, "rb") as imageFile: encodedString = base64.b64encode(imageFile.read()).decode('ascii') else: encodedString = ''

return driver.wapi_functions.sendMessageWithThumb(encodedString, url, title[0:65], description[0:155], chatId)

I found that it's importan to use .decode('utf-8') or .decode('ascii') with the function base64.b64encode in order to get and string. Without this, the parameter 'thumb' do not receive an string really.

And this is how I call the function in order to obtain the same output that is mention in this site (http://webdevelopmentscripts.com/80-whatsapp-share-link-with-image-in-websites)

                shareLink(contact.chat.id,
                    "/home/administrador/workspace/whatsapp/images/thumbail.jpeg",
                    "http://webdevelopmentscripts.com/64-how-to-change-the-address-bar-color-in-chrome-firefox-opera-safari",
                    "How to change the address bar color in Chrome, Firefox, Opera, Safar",
                    "How to change the address bar color in Chrome, Firefox, Opera, Safari and others similar browsers")

Regards

dafner commented 6 years ago

I'm posting again the modified code:

``window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId, done) { var chatSend = WAPI.getChat(chatId); if (chatSend === undefined) { return false; } var msgWithImg = chatSend.createMessageFromText("."); msgWithImg.x_hasLink = title; msgWithImg.__x_body = description + '\n ' + url; msgWithImg.x_isLink = title; msgWithImg.x_description = description; msgWithImg.__x_subtype = 'url'; msgWithImg.x_title = title; msgWithImg.__x_thumbnail = thumb;

chatSend.addAndSendMsg(msgWithImg);

if(done!==undefined){
    done(true);
}

return true;

};