我正在尝试将javascript函数部署到Firebase,该函数包含通过Twilio发送SMS的代码。
Twilio-js代码在单独的文件中测试时运行良好。将包含Twilio代码的完整代码上传到firebase函数时发生错误。我试过Nexmo,也遇到了问题。看到阻止Twilio和Nexmo的火球了吗!
有什么建议吗?
编辑时间:这是我的完整代码
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
const accountSid = 'AC18bda2c8129eedc0c13fb4123761eb44';
const authToken = 'xyzxyzyxz';
const client = require('twilio')(accountSid, authToken);
exports.realtimefunction=functions.database.ref('/{X}/{Y}/{Z}').onCreate((snapshot,context)=>{
client.messages
.create({
body: 'Hi',
messagingServiceSid: 'MGf7sdf39d9f979ssdfeb9f16',
to: '+201011111111'
})
.then(message => console.log(message.sid))
.done();
return null;
});
和error消息:错误:函数未正确部署。
下面是一个应该有效的例子。让我知道这是否适用于你,或者你是否仍然有错误。这个例子应该允许你发短信给一个号码,它会用你发送的短信回复你。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Vonage = require('@vonage/server-sdk');
// Initialize Firebase app for database access
admin.initializeApp();
// get Firebase environment variables for Vonage
const {
api_key,
api_secret
} = functions.config().vonage;
// Initialize Vonage with application credentials
const vonage = new Vonage({
apiKey: api_key,
apiSecret: api_secret
});
// This function will serve as the webhook for incoming SMS messages,
// and will log the message into the Firebase Realtime Database
exports.inboundSMS = functions.https.onRequest(async (req, res) => {
await admin.database().ref('/msgq').push(req.body);
res.send(200);
});
// This function listens for updates to the Firebase Realtime Database
// and sends a message back to the original sender
exports.sendSMS = functions.database.ref('/msgq/{pushId}')
.onCreate((message) => {
const { msisdn, text, to } = message.val();
// the incoming object - 'msisdn' is the your phone number, and 'to' is the Vonage number
// vonage.message.sendSms(to, msisdn, text);
return new Promise((respond, reject) => {
vonage.message.sendSms(to, msisdn, `You sent the following text: ${text}`,
(err, res) => {
if (err) {
reject(err);
} else {
if (res.messages[0]['status'] === "0") {
respond("Message sent successfully.");
} else {
reject(`Message failed with error: ${res.messages[0]['error-text']}`);
}
}
})
})
});
你是否在使用Firebase的免费火花计划?Firebase只允许外部API访问付费的;Blaze";有关详细答案,请参阅此问题。Firebase云功能(Spark层(中对外部API的HTTP请求拒绝