0

I'm trying to deploy javascript function to Firebase that containing code to send SMS by Twilio.

Twilio js code run ok when testing it in stand alone separate file. When uploading complete code containing Twilio code to firebase function Error occur. I tried Nexmo and also face problem. Seem that firebase preventing Twilio and 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.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Can you share the code and the error message you are getting when you run the Twilio code? – philnash Apr 26 '21 at 10:08
  • Please add more details here - code you tried, errors from firebase. Anything that might help us find the solution. Otherwise, I'm afraid the question may get closed and unanswered :( – Kelly J Andrews Apr 26 '21 at 10:48
  • 1
    I have Edited the post with code and Error msg – Yomna Youssef Apr 26 '21 at 11:05
  • The error occur when adding even Twilio decaration. – Yomna Youssef Apr 26 '21 at 11:21
  • I have tested by sending email instead of Twilio SMS and it succeed so the problem when adding Twilio – Yomna Youssef Apr 26 '21 at 11:22
  • Thanks for the additional code. I'll get you an answer soon I think. – Kelly J Andrews Apr 26 '21 at 12:28
  • So, if I understood this correctly, the function is not deployed only if you use Twilio or Nexmo, correct? If you were to deploy a dummy function without access to these external services as a test, would it be deployed? – Ralemos Apr 27 '21 at 11:57
  • 2
    If that was your real auth token in that example, then I recommend you [change your auth token now](https://support.twilio.com/hc/en-us/articles/223136027-Auth-Tokens-and-How-to-Change-Them). – philnash Apr 28 '21 at 04:13
  • Also, what happens if you deploy with `firebase --debug deploy`? The debug flag should give you more information about what caused the error. – philnash Apr 28 '21 at 04:14

2 Answers2

0

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']}`);
                }
            }
        })
    })
});
Kelly J Andrews
  • 5,083
  • 1
  • 19
  • 32
-1

Are you on the free spark plan with Firebase? Firebase only allows external API access on the paid for "Blaze" plan see this question for the detailed answer. HTTP request to an external API in Firebase Cloud Functions (Spark Tier) refused

Sam Machin
  • 3,063
  • 5
  • 20
  • 18