简体   繁体   English

firebase function 和 Twilio 和 Nexmo 之间的问题

[英]Problem between firebase function and Twilio & Nexmo

I'm trying to deploy javascript function to Firebase that containing code to send SMS by Twilio.我正在尝试将 javascript function 部署到 Firebase,其中包含通过 Z9017AFDB6E5054E5B692E4D 发送 SMS 的代码。

Twilio js code run ok when testing it in stand alone separate file. Twilio js 代码在独立的单独文件中测试时运行正常。 When uploading complete code containing Twilio code to firebase function Error occur.将包含 Twilio 代码的完整代码上传到 firebase function 时出现错误。 I tried Nexmo and also face problem.我试过 Nexmo 也遇到了问题。 Seem that firebase preventing Twilio and Nexmo!似乎 firebase 阻止了 Twilio 和 Nexmo!

Any suggestions?有什么建议么?

Edited: Here is My Full Code编辑:这是我的完整代码

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;
});

and error Message: Error: Functions did not deploy properly.和错误消息:错误:功能未正确部署。

Here is an example that should work.这是一个应该工作的例子。 Let me know if this works for you, or if you still have the error.让我知道这是否适合您,或者您是否仍然有错误。 This example should allow you to text a number, which will respond to you with the text you sent.这个例子应该允许你给一个号码发短信,它会用你发送的短信回复你。

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']}`);
                }
            }
        })
    })
});

Are you on the free spark plan with Firebase?您是否参加了 Firebase 的免费 spark 计划? Firebase only allows external API access on the paid for "Blaze" plan see this question for the detailed answer. Firebase 仅允许外部 API 访问已付费的“Blaze”计划,请参阅此问题以获取详细答案。 HTTP request to an external API in Firebase Cloud Functions (Spark Tier) refused HTTP 对 Firebase 云函数(Spark 层)中的外部 API 的请求被拒绝

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM