我有一个应用程序,允许用户通过在输入字段中输入电话号码来发送短信。但是短信被设置为只是说"你好"。我怎样才能让用户输入他们想要的文字信息。继承人的代码
JS
app.get("/:data", function(req, resp){
var accountSid = '*******************'
var authToken = '*********************'
const client = require('twilio')(accountSid, authToken);
client.messages.create({
to: req.params.data,
from: '**********',
body: "Hello"
}, function(err, message) {
if(err) {
console.log(err);
} else {
console.log(message.sid);
}
});
HTML
<input type="text" placeholder="Enter your number" id="inputNum" />
<button id="submitNum">Enter</button>
<script>
submitNum.addEventListener("click", function(){
var inputNum = document.getElementById("inputNum");
var submitNum = document.getElementById("submitNum");
var phoneNumber = inputNum.value;
fetch(" https://*******************.com/"
+ phoneNumber).then((resp)=>{
console.log(resp);
});
});
答案 0 :(得分:0)
Twilio开发者传道者在这里。
要允许用户输入消息,您需要在HTML表单中添加一个额外字段,然后将该字段添加到API请求中。我还建议不要将电话号码添加到URL的路径,而是查询参数。因此,首先考虑您的HTML,我将其更改为:
<input type="text" placeholder="Enter your number" id="inputNum" />
<textarea name="inputMessage" id="inputMessage"></textarea>
<button id="submitNum">Enter</button>
<script>
var inputNum = document.getElementById("inputNum");
var inputMessage = document.getElementById("inputMessage");
var submitNum = document.getElementById("submitNum");
submitNum.addEventListener("click", function(){
var phoneNumber = inputNum.value;
var message = inputMessage.value;
var query = "phoneNumnber=" + encodeURIComponent(phoneNumber);
query += "&message=" + encodeURIComponent(message);
fetch(" https://*******************.com/sendMessage?" + query).then((resp)=>{
console.log(resp);
});
});
</script>
然后,您需要更新服务器以使用消息变量和sendMessage
端点。我还建议你回复一下,这样你的前端就不会挂了。像这样:
app.get("/sendMessage", function(req, resp){
var accountSid = '*******************'
var authToken = '*********************'
const client = require('twilio')(accountSid, authToken);
client.messages.create({
to: req.params.data,
from: '**********',
body: req.params.message
}, function(err, message) {
if(err) {
console.log(err);
resp.setStatus(200).send();
} else {
console.log(message.sid);
resp.setStatus(500).send();
}
});
});
让我知道这是否有帮助。