【问题标题】:Getting data from input to send to whatsapp从输入中获取数据以发送到whatsapp
【发布时间】:2020-07-24 16:33:52
【问题描述】:

有一个脚本可以向whatsapp发送消息

<script>
  $(document).ready(function() {
    // URL for the POST /message request
    var url = 'https://eu43.chat-api.com/instance**********/message?token=**********';
    var data = {
      phone: '***********', // Recipient's phone number
      body: 'There should be a message here', // Message
    };
    // Send a request when the button is clicked
    $(".button").click(function() {
      $.ajax(url, {
        data: JSON.stringify(data),
        contentType: 'application/json',
        type: 'POST'
      });
    });
  });
</script>

还有一些输入由客户填写,以通过whatsapp中的电话号码将有关订单的信息发送给相关合作伙伴(咖啡馆、餐馆),以便形成和交付订单

<input name="name" class="t-input">
<input name="phone" class="t-input">
<input name="address" class="t-input">
<input type="hidden" name="recipient" value="78888888888"> //Phone number of the recipient of order information in whatsapp

现在的任务是将有关订单的信息发送到whatsapp,该信息包含在输入值(姓名,电话,地址)到收件人的号码(收件人)中,并将名称放在每个输入值之前(收件人除外),即姓名:、电话号码:、地址:换行。收件人值必须插入到电话中,其他输入值(姓名、电话、地址)必须插入到数据中。

收到的消息示例:

  • 姓名:亚历克斯
  • 电话:+79999999999
  • 地址:喀山市鲍马纳街20号房子,19号公寓
  • 订购:披萨“玛格丽塔”

【问题讨论】:

    标签: javascript jquery ajax input whatsapp


    【解决方案1】:

    您只需要从表单项中获取值,并组成一个正文值。

    示例:

    $(document).ready(function() {
      // Send a request when the button is clicked
      $(".button").click(function() {
        $.ajax('https://eu43.chat-api.com/instance**********/message?token=**********', {
          data: JSON.stringify({
            phone: $('input[name="recipient"]').val(),
            body: 
            `Name: ${$('input[name="name"]').val()}\n`+
            `Phone: ${$('input[name="phone"]').val()}\n`+
            `Address: ${$('input[name="address"]').val()}\n`+
            `Order: Pizza " Margarita"`
          }),
          contentType: 'application/json',
          type: 'POST'
        });
      });
    });
    

    超出问题范围:添加验证。

    【讨论】:

    • 我尝试了您的解决方案,谢谢,但它不起作用。连开发者控制台中的网络事件都不见了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多