Javascript 如何通过Javascript发送whatsapp消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47243154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send whatsapp message via Javascript
提问by Jiga Garcia
Hi I would like to know if there is something to send a whatsapp message using javascript or something I was searching but I did not find any new post. This is not for any special purpose. I found this but it only works if you have whatsapp web. I was thinking on clicking on a link to send a default message to a default number
嗨,我想知道是否有什么东西可以使用 javascript 或我正在搜索的东西发送 whatsapp 消息,但我没有找到任何新帖子。这不是为了任何特殊目的。我找到了这个,但它只有在你有 whatsapp 网络时才有效。我正在考虑单击链接将默认消息发送到默认号码
<a href="https://api.whatsapp.com/send?phone=1111111111">Send Message</a>
回答by Renan Coelho
Just make use of this function in web browser to get it running as you want.
只需在 Web 浏览器中使用此功能即可使其按需要运行。
Note: You need to run the code manually through browser console with an opened conversation in WhatsApp (web version).
注意:您需要通过浏览器控制台手动运行代码,并在 WhatsApp(网络版)中打开对话。
function sendMessage (message) {
window.InputEvent = window.Event || window.InputEvent;
var event = new InputEvent('input', {
bubbles: true
});
var textbox = document.querySelector('div._2S1VP');
textbox.textContent = message;
textbox.dispatchEvent(event);
document.querySelector("button._35EW6").click();
}
Thanks to Gabriel!
感谢加布里埃尔!
Important: If you're trying to send a message directly from URL (i.e using API), you cant. Its impossible to send WhatsApp messages without user interaction.
重要提示:如果您尝试直接从 URL(即使用 API)发送消息,则不能。没有用户交互就不可能发送 WhatsApp 消息。
回答by Jan Nitschke
Whatsapp dosen't support sending messages from the PC. Every message has to come from a Phone. Whatsapp web is just redirecting the messages to your phone wich is then sending the message. If you use their api you only can let users send a message to any number via Whatsapp using their phone. To predetermine the message use:
Whatsapp 不支持从 PC 发送消息。每条消息都必须来自电话。Whatsapp web 只是将消息重定向到您的手机,然后发送消息。如果您使用他们的 api,您只能让用户使用他们的手机通过 Whatsapp 向任何号码发送消息。要预先确定消息使用:
https://api.whatsapp.com/send?phone=whatsappphonenumber&text=urlencodedtext
https://api.whatsapp.com/send?phone=whatsappphonenumber&text=urlencodedtext
more: https://faq.whatsapp.com/en/android/26000030/?category=5245251
更多:https: //faq.whatsapp.com/en/android/26000030/?category=5245251
回答by camelsWriteInCamelCase
It's impossible to do it with HTML link. Whatsapp has no official API. But you can find (or write it by yourself) some script to emulate user actions on the site web.whatsapp.com. For example, this one(I did not test it).
用 HTML 链接是不可能做到的。Whatsapp 没有官方 API。但是您可以在网站 web.whatsapp.com 上找到(或自己编写)一些脚本来模拟用户操作。例如,这个(我没有测试过)。

