如何解决firebase 函数和 Twilio & Nexmo 之间的问题
我正在尝试将 javascript 函数部署到 Firebase,其中包含通过 Twilio 发送短信的代码。
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;
});
解决方法
这是一个应该可以工作的例子。让我知道这是否适合您,或者您是否仍然有错误。这个例子应该允许你给一个号码发短信,它会用你发送的短信回复你。
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,`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 的免费 Spark 计划? Firebase 仅允许对“Blaze”付费计划进行外部 API 访问,请参阅此问题以获取详细答案。 HTTP request to an external API in Firebase Cloud Functions (Spark Tier) refused
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。