내 시작점은 node.js, sendgrid 및 Twilio Functions를 사용하여 수신 SMS 메시지를 이메일로 전달하는 방법에 대한 답변입니다 : https://stackoverflow.com/a/50728459/
이 튜토리얼도 살펴 보았습니다 : https://support.twilio.com/hc/en-us/articles/223134287-Forwarding-SMS-messages-to-another-phone-number
하나의 Twilio 기능에서 두 작업을 결합 할 수 있습니까? 들어오는 메시지에 대한 응답으로 두 개의 개별 "메시징 작업"을 수행하도록 Twilio 번호를 구성하는 방법이 없기 때문에 하나의 기능으로 결합하는 것을 선호합니다. 실행할 기능은 하나만 선택할 수 있습니다.
위 의 답변에 몇 줄의 코드를 추가하기 만하면되기를 바랍니다 .
let twiml = new Twilio.twiml.MessagingResponse();
twiml.message(`${event.From}: ${event.Body}`, {
to: '+13105555555'
});
callback(null, twiml);
이 기능에서는 먼저 SMS를 다른 전화 번호로 전달한 다음 메시지를 이메일로 전달하고 싶습니다.
Twilio 함수 환경 변수가 설정됩니다.
내 코드는 다음과 같습니다.
const https = require('https');
exports.handler = function (context, event, callback) {
let sms_twiml = new Twilio.sms_twiml.MessagingResponse();
sms_twiml.message(`${event.From}: ${event.Body}`, {
to: context.TO_SMS_NUMBER
});
//executing a callback terminates the function
//callback(null, sms_twiml);
let postData = JSON.stringify({
personalizations: [{
to: [{
email: context.TO_EMAIL_ADDRESS
}]
}],
from: {
email: context.FROM_EMAIL_ADDRESS
},
subject: `New SMS message from: ${event.From}`,
content: [{
type: 'text/plain',
value: event.Body
}]
});
let postOptions = {
host: 'api.sendgrid.com',
port: '443',
path: '/v3/mail/send',
method: 'POST',
headers: {
'Authorization': 'Bearer ${context.SENDGRID_API_KEY}',
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
}
};
let req = https.request(postOptions, function (res) {
let email_twiml = new Twilio.email_twiml.MessagingResponse();
callback(null, email_twiml);
});
req.write(postData);
req.end();
};
오류-11200이 표시됩니다.
HTTP 검색 실패이 URL의 내용을 검색하는 데 실패했습니다. 11200 오류는 Twilio와 서비스 간의 연결 실패를 나타냅니다.
이 코드를 사용해보십시오 . 이 블로그 예제 에서 한 줄 추가되었습니다 .
(이 라인 : twiml.message({to: '+1555xxxxxxx'}, event.Body); // Forward SMS to this Number
)
const got = require('got');
exports.handler = function(context, event, callback) {
const requestBody = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: `New SMS message from: ${event.From}`,
content: [
{
type: 'text/plain',
value: event.Body
}
]
};
got
.post('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
"Content-Type": 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
console.log(response);
let twiml = new Twilio.twiml.MessagingResponse();
twiml.message({to: '+1555xxxxxxx'}, event.Body); // Forward SMS to this Number
callback(null, twiml);
})
.catch(err => {
console.log(err);
callback(err);
});
};
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다