如何通过Javascript发送WhatsApp消息

15

嗨,我想知道是否有使用JavaScript或其他方式发送WhatsApp消息的方法,我已经搜索过了但没有找到最新的帖子。这并不是为了任何特殊目的。我发现了这个,但它只能在您拥有WhatsApp Web时才有效。我在考虑通过点击链接向默认号码发送默认消息。

<a href="https://api.whatsapp.com/send?phone=1111111111">Send Message</a>
6个回答

15

只需在Web浏览器中使用此功能,即可按您所需的方式运行它。

注意:您需要通过浏览器控制台手动运行代码,并打开WhatsApp(Web版本)上的对话。

function sendMessage(message){
  const mainEl = document.querySelector('#main')
  const textareaEl = mainEl.querySelector('div[contenteditable="true"]')

  if(!textareaEl) {
    throw new Error('There is no opened conversation')
  }

  textareaEl.focus()
  document.execCommand('insertText', false, message)
  textareaEl.dispatchEvent(new Event('change', { bubbles: true }))

  setTimeout(() => {
    (mainEl.querySelector('[data-testid="send"]') || mainEl.querySelector('[data-icon="send"]')).click()
  }, 100)
}

参考: https://github.com/Matt-Fontes/SendScriptWhatsApp/blob/main/shrekSendScript.js


1
这个真的很有效。我会尝试使用Puppeteer自动化它。感谢提供代码... - Yohannes Kristiawan
@RenanCoelho,请查看您的电子邮件。 - user12733526
我们能否使用JavaScript将从HTML表单中获取的图像文件发送到WhatsApp? - Divyanshu Gupta

8

我认为这种方式是向不知道的WhatsApp号码发送消息的更好方式。

// github: omar-bakhsh
function send_handle(){

  let num=document.getElementById("number").value;

  let msg= document.getElementById("msg").value;

    let name= document.getElementById("name").value;
  
  var win = window.open(`https://wa.me/${num}?text=I%27m%20api%20msg%20hello%20${name}%20friend%20${msg}`, '_blank');
 // win.focus();
}
        <div>
          <h3>whatsapp send app </h3>
            <h6>add number without space like 17272912606 not <strike>+1 (727) 2912606 </strike></h6>
  <input id="number" type="numric" placeholder="phone 966506666666" >
   <input id="name" type="text" placeholder="name" >
   <input id="msg" type="text" placeholder="type msg" >
<button onclick="send_handle()">send</button>


演示:https://omar-bakhsh.github.io/Whatsapp_Bot_javascript/index.html - Omar bakhsh

4
你可以这样做:

<a href="https://api.whatsapp.com/send?phone=1111111111&text=Hi">Send Message</a>


3
WhatsApp 开发者中心,他们演示了如何在 Meta 上选择开发者账户和创建应用程序的详细步骤,以便如果您想要以编程方式使用 WhatsApp,则可以进行 API 调用。
简单版:
  • 注册成为 Meta 开发者。
  • 为您的帐户启用双重身份验证。
  • 创建一个 Meta 应用:转到 developers.facebook.com > 我的应用程序 > 创建应用程序。
  • 选择“业务”类型并按照屏幕提示操作。
  • 向下滚动以查找 “WhatsApp” 产品并单击“设置”。
  • 如果没有现有的 Business Manager,请选择一个,否则向导将指导您创建一个。
  • 下一个屏幕将向您展示演示。
  • 如果需要在您的号码接收消息或回复时添加监听器,则应注册 Webhooks。
  • 将您的电话号码添加到可编程使用,并且不应与 WhatsApp 相关联,既不是个人也不是商业使用。
要了解更多详细信息,请参阅入门指南:https://business.whatsapp.com/developers/developer-hub

3

0

使用HTML链接是不可能的。Whatsapp没有官方API。但是你可以找到(或自己编写)一些脚本来模拟用户在网站web.whatsapp.com上的操作。例如,这个(我没有测试过)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接