简体   繁体   中英

Twilio not sending SMS inside firebase cloud function

i am trying to send sms using twilio api for node.js through a firebase cloud function but the sms is not sent. however if the same code i run as an independent java script code, then it works fine.can someone please help why this is not happening inside the firebase cloud function. the code is attached below:

 const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); exports.sendNotfication = functions.database.ref('/kakuh/{pushId}/firstName') .onCreate((snapshot, context) => { const original = snapshot.val(); const accountSid = 'ACb6b4820df073e63312382f95b0314d07'; const authTcoken = 'c60923ca097368662b39dfab470f2fd1'; const client = require('twilio')(accountSid, authToken); client.messages .create({ from: '+16304263296', body: original, to: '+918169813384' }); console.log('Uppercasing', context.params.pushId, original); const uppercase = original.toUpperCase(); return snapshot.ref.parent.child('firstName').set(uppercase); });

You'll need to wait for Twilio to respond, then return to Firebase.

Try this:


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotfication = functions.database.ref('/kakuh/{pushId}/firstName')
    .onCreate((snapshot, context) => {

        const original = snapshot.val();
        const accountSid = 'ACb6b4820df073e63312382f95b0314d07';
        const authToken = 'c60923ca097368662b39dfab470f2fd1';
        const client = require('twilio')(accountSid, authToken);

        client.messages
            .create({
                from: '+16304263296',
                body: original,
                to: '+918169813384'
            })
            .then((message) => {
                console.log(message.sid);
                console.log('Uppercasing', context.params.pushId, original);
                const uppercase = original.toUpperCase();
                return snapshot.ref.parent.child('firstName').set(uppercase);
            })
            .catch((err) => {
                throw (err);
            });

    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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