Я настраиваю песочницу Twilio для WhatsApp потому что, когда приходит сообщение, я устанавливаю веб-хук на ссылку моего приложения.
Но для моего приложения требуется токен на предъявителя.
Как я могу настроить twilio для отправки нашего токена-носителя вместе с запросом, который он делает, на мой URL-адрес?
Спасибо
я делаю все тесты без токена носителя, и он отлично работает. но чтобы начать работу, нам нужна аутентификация этого токена из соображений безопасности.
Как вы правильно сказали, веб-хук просто запускает запрос GET или POST к зарегистрированному URL-адресу. Чтобы иметь возможность добавлять пользовательские параметры, такие как токен носителя, вам необходимо добавить промежуточный шаг между ними. Этого можно добиться, например, с помощью любой бессерверной функции.
Естественно, проще всего это сделать с помощью Twilio Serverless:
const axios = require('axios');
exports.handler = async (context, event, callback) => {
// Create a new voice response object
const twiml = new Twilio.twiml.VoiceResponse();
try {
// Open APIs From Space: http://open-notify.org
// Number of people in space
const response = await axios.request({
method: "POST",
url: `http://api.open-notify.org/astros.json`,
headers: {
"Authorization": `Bearer ${request.body.token}`,
"Content-Type": "application/json; charset=utf-8"
},
});
const { number, people } = response.data;
const names = people.map((astronaut) => astronaut.name).sort();
// Create a list formatter to join the names with commas and 'and'
// so that the played speech sounds more natural
const listFormatter = new Intl.ListFormat('en');
twiml.say(`There are ${number} people in space.`);
twiml.pause({ length: 1 });
twiml.say(`Their names are: ${listFormatter.format(names)}`);
// Return the final TwiML as the second argument to `callback`
// This will render the response as XML in reply to the webhook request
// and result in the message being played back to the user
return callback(null, twiml);
} catch (error) {
// In the event of an error, return a 500 error and the error message
console.error(error);
return callback(error);
}
};