Open ayrokid opened 6 years ago
check WAPI.js function thumb = IMAGE IN BASE64 window.WAPI.sendMessageWithThumb = function (thumb, url, title, description, chatId)
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
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; };
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
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
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
I hope that you can help me in order to find the solution to this problem
Thanks in advance for your help
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; };
Thanks very much, It's working OK!!!!
@dafner @bobaoapae, can you send a PR with this updates?
@dafner send you if you can.. i don't have python code here... i just use the javascript code in my own Java library
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
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;
};
i want sent URL message.
thanks,