如何使用 twilio 和 svelte 从输入发送文本?
我看到了很多这方面的文档,但没有看到 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将
的值绑定到变量在你的脚本中。
这使得当您更改输入中的值时,它会更新
phoneNumber
变量。现在,在您的
handleSendText
函数中,您需要发送请求的body
内的所有参数。请注意,在以下代码中,body
如何包含要在一个 JSON 字符串化对象中发送的所有参数。然后,在 Twilio 函数中,您将能够访问在
event
对象中发送的参数。在本例中,您将拥有event.to
和event.from
。You'll need to bind the value of the
<input>
to a variable in your script.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 thebody
of the request. Note, in the following code how thebody
contains all the parameters you want to send in one JSON stringified object.Then, in your Twilio function you will be able to access the parameters you send in the
event
object. In this case, you will haveevent.to
andevent.from
.