如何使用 twilio 和 svelte 从输入发送文本?

发布于 2025-01-16 04:01:30 字数 803 浏览 4 评论 0原文

我看到了很多这方面的文档,但没有看到 javascript。我的知识匮乏在这里发挥了作用,所以请原谅我。我想向输入中输入的电话号码发送简单的文本。我设置了一个无服务器功能,但我不知道如何将电话号码传递给它,除此之外,我尝试向它写入一个发布请求,但我肯定还很遥远。我正在做的事情可能吗?

let phoneNumber;

const handleSendText = () => {
    fetch("https://svelte-service-xxxx.twil.io/sms", {
      method: "post",
      body: "This is a test",
      headers: {
        "Content-Type": "application/json",
      },
      to: JSON.stringify(phoneNumber),
      from: "+xxxxxxxxxxx",
    })
      .then((response) => {
        if (!response.ok) {
          throw new Error("failed");
        }
      })
      .catch((err) => {
        console.log(err);
      });
  };

 <input type="text" placeholder="phone number" />
 <button on:click={handleSendText}>Send</button>

I see a lot of docs for this but nothing javascript. My lack of knowledge is coming into play here, so forgive me. I want to send a simple text to a phone number that is typed into an input. I have a serverless function set up but i don't know how to pass in the phone number to it, beyond that I have tried to write a post request to it but I must be way off. Is what I am doing possible?

let phoneNumber;

const handleSendText = () => {
    fetch("https://svelte-service-xxxx.twil.io/sms", {
      method: "post",
      body: "This is a test",
      headers: {
        "Content-Type": "application/json",
      },
      to: JSON.stringify(phoneNumber),
      from: "+xxxxxxxxxxx",
    })
      .then((response) => {
        if (!response.ok) {
          throw new Error("failed");
        }
      })
      .catch((err) => {
        console.log(err);
      });
  };

 <input type="text" placeholder="phone number" />
 <button on:click={handleSendText}>Send</button>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2025-01-23 04:01:30

您需要的值绑定到变量在你的脚本中

<script>
let phoneNumber = "";

const handleSendText = () => { // we'll come back to this };
</script>

<input type="text" placeholder="phone number" bind:value={phoneNumber} />
<button on:click={handleSendText}>Send</button>

这使得当您更改输入中的值时,它会更新 phoneNumber 变量。

现在,在您的 handleSendText 函数中,您需要发送请求的 body 内的所有参数。请注意,在以下代码中,body 如何包含要在一个 JSON 字符串化对象中发送的所有参数。

const handleSendText = () => {
  fetch("https://svelte-service-9106.twil.io/sms", {
    method: "POST",
    body: JSON.stringify({
      to: phoneNumber,
      from: "+18035298025"
    }),
    headers: {
      "Content-Type": "application/json",
    }
  })
  .then((response) => {
    if (!response.ok) {
      throw new Error("failed");
    } else {
      console.log("Success!");
    }
  })
  .catch((err) => {
    console.log(err);
  });
};

然后,在 Twilio 函数中,您将能够访问在 event 对象中发送的参数。在本例中,您将拥有 event.toevent.from

You'll need to bind the value of the <input> to a variable in your script.

<script>
let phoneNumber = "";

const handleSendText = () => { // we'll come back to this };
</script>

<input type="text" placeholder="phone number" bind:value={phoneNumber} />
<button on:click={handleSendText}>Send</button>

That makes it so that when you change the value in the input it updates the phoneNumber variable.

Now, in your handleSendText function you need to send all the parameters within the body of the request. Note, in the following code how the body contains all the parameters you want to send in one JSON stringified object.

const handleSendText = () => {
  fetch("https://svelte-service-9106.twil.io/sms", {
    method: "POST",
    body: JSON.stringify({
      to: phoneNumber,
      from: "+18035298025"
    }),
    headers: {
      "Content-Type": "application/json",
    }
  })
  .then((response) => {
    if (!response.ok) {
      throw new Error("failed");
    } else {
      console.log("Success!");
    }
  })
  .catch((err) => {
    console.log(err);
  });
};

Then, in your Twilio function you will be able to access the parameters you send in the event object. In this case, you will have event.to and event.from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文